【嵌入式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)静态函数被存储在内存的静态存储区里面,避免了函数调用时进栈出栈,速度提升很多

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值