背景
- RT-Thread 的开发过程中,难免会遇到较多的【宏】配置。
- RT-Thread ENV工具,有menuconfig,类似于Linux开发的menuconfig,可以图形菜单配置【宏】。
- C、C++开发中,【宏】作用强大,给编程带来很大的便利。
Kconfig
- 【宏】的具体内容,一般为:字符串、数值。
- Kconfig,可以实现:
#define 一个宏
#define 宏并设置内容为字符串
#define 宏并设置内容为数值
多选一功能
实践应用
- RT-Thread menuconfig,并保存退出后,会更新到rtconfig.h文件。
- 想设置【宏】,建议不要直接改rtconfig.h文件,否则,menuconfig后,又被重新更改了。
- rtconfig.h的【宏】,参与scons 工程构建,可以作为功能、文件的编译开关
- define 一个宏,bool类型
config JSFW_USING_HWS_CMD
bool "HWS : Enable hws test cmd"
default n
选择后效果:
#define JSFW_USING_HWS_CMD
- define 一个宏,并设置字符串
config HWS_DEV_LCD_NAME
string "the device name for lcd"
default "lcd"
选择后效果:
#define HWS_DEV_LCD_NAME "lcd"
- define 一个宏,并设置数值
config HWS_KEY_PIN
int "key pin number"
default 18
选择后的效果
#define HWS_KEY_PIN 18
- define一个多选一的宏
choice
prompt "active level"
default HWS_VIBRATE_ACTIVE_HIGH
help
The vibrate active level.
config HWS_VIBRATE_ACTIVE_HIGH
bool "Active High"
config HWS_VIBRATE_ACTIVE_LOW
bool "Active Low"
endchoice
选择后的效果
#define HWS_VIBRATE_ACTIVE_HIGH
/* 注意这里是二选一,两个选项中,只有一个生效,这里选择的HIGH */
Kconfig 语法
- menuconfig HWS_USING_VIBRATE
效果:
- config HWS_KEY_DEBOUNCE_TIME
效果:
- Kconfig 中,可以使用 if endif
menuconfig HWS_USING_KEY
bool "Enable key"
default y
if HWS_USING_KEY
config HWS_KEY_PIN
int "key pin number"
default 18
config HWS_KEY_DEBOUNCE_TIME
int "key debounce press time"
default 20
config HWS_KEY_LONG_PRESS_TIME
int "key long press time"
default 1500
endif
效果
- select RT_USING_DEVICE
select 的作用,类似于选择(定义)了这个宏。用于依赖项的选择。如你使能某个功能,需要其他的功能开启后才可以使用,依赖其他的功能。
总结
- RT-Thread ENV 工具还是比较易用的。
- Kconfig,多用,这样,让设计更快捷