【嵌入式C语言】静态函数的用法和优点
嵌入式C语言中的静态函数
·
从FreeRTOS和LVGL源码中学习C语言,大家好,我是石头墩子
看到下面这段代码,我脑子里第一时间想到的是这段代码结构和修改后的代码结构有什么不同
lv_port_index.c
static void touchpad_init(void);
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static bool touchpad_is_pressed(void);
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);
void lv_port_indev_init(void)
{
static lv_indev_drv_t indev_drv;
/*Initialize your touchpad if you have*/
touchpad_init();
/*Register a touchpad input device*/
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = touchpad_read;
indev_touchpad = lv_indev_drv_register(&indev_drv);
}
/*Initialize your touchpad*/
static void touchpad_init(void)
{
/**/
}
/*Will be called by the library to read the touchpad*/
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
/**/
}
/*Return true is the touchpad is pressed*/
static bool touchpad_is_pressed(void)
{
/**/
}
/*Get the x and y coordinates if the touchpad is pressed*/
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
{
/**/
}
lv_port_index.h
#ifndef LV_PORT_INDEV_H
#define LV_PORT_INDEV_H
#include "lvgl/lvgl.h"
void lv_port_indev_init(void);
修改后的 lv_port_index.c
void lv_port_indev_init(void)
{
static lv_indev_drv_t indev_drv;
/*Initialize your touchpad if you have*/
touchpad_init();
/*Register a touchpad input device*/
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = touchpad_read;
indev_touchpad = lv_indev_drv_register(&indev_drv);
}
/*Initialize your touchpad*/
void touchpad_init(void)
{
/**/
}
/*Will be called by the library to read the touchpad*/
void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
/**/
}
/*Return true is the touchpad is pressed*/
bool touchpad_is_pressed(void)
{
/**/
}
/*Get the x and y coordinates if the touchpad is pressed*/
void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
{
/**/
}
修改后的 lv_port_index.h
#ifndef LV_PORT_INDEV_H
#define LV_PORT_INDEV_H
#include "lvgl/lvgl.h"
void lv_port_indev_init(void);
void touchpad_init(void);
void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
bool touchpad_is_pressed(void);
void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);
lv_port_index.c中声明以下static静态函数
static void touchpad_init(void);
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static bool touchpad_is_pressed(void);
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);
然后同一个文件lv_port_index.c中的void lv_port_indev_init(void);调用上述static静态函数。
而修改后的文件不在修改后的 lv_port_index.c中声明函数,而是在修改后的 lv_port_index.h中声明函数。
引出:这个就是我们今天的主角---静态函数
(1)函数的声明和定义默认是extern的,全局可以调用,而static静态函数,只是在声明的文件中可以调用,不能被其他文件调用。
(2)静态函数被存储在内存的静态存储区里面,避免了函数调用时进栈出栈,速度提升很多
更多推荐
所有评论(0)