本帖最后由 liklon 于 2020-3-9 22:55 编辑
STM32CUBE增加4个按键按键名 | 引脚 | KEY_1 | PC4 | KEY_2 | PB10 | KEY_3 | PC13 | WAKEUP | PA0 |
配置b_config.h 本次实验测试b_button功能,使能Thirdparty里面的FlexibleButton 添加Flexiblebutton代码 添加BabyOS\bos\core\thirdparty\FlexibleButton\目录所有文件,添加头文件路径 设置按键ID及按下时的逻辑电平
//b_button.h内添加
typedef enum
{
USER_BUTTON_1,
USER_BUTTON_2,
USER_BUTTON_3,
USER_BUTTON_WAKEUP,
USER_BUTTON_MAX
}bBUTTON_ID_t;
//b_button.c内添加按下后的逻辑电平
static flex_button_t bButtonList[USER_BUTTON_MAX] = {
[USER_BUTTON_1] = {
.pressed_logic_level = 0,
},
[USER_BUTTON_2] = {
.pressed_logic_level = 0,
},
[USER_BUTTON_3] = {
.pressed_logic_level = 0,
},
[USER_BUTTON_WAKEUP] = {
.pressed_logic_level = 0,
},
};
添加读取按键值和回调
//main.c添加
/* USER CODE BEGIN 0 */
uint8_t bButtonRead(void *p)
{
uint8_t tmp = 1;
flex_button_t *btn = (flex_button_t *)p;
switch(btn->id)
{
case USER_BUTTON_1:
tmp = HAL_GPIO_ReadPin(KEY_1_GPIO_Port, KEY_1_Pin);
break;
case USER_BUTTON_2:
tmp = HAL_GPIO_ReadPin(KEY_2_GPIO_Port, KEY_2_Pin);
break;
case USER_BUTTON_3:
tmp = HAL_GPIO_ReadPin(KEY_3_GPIO_Port, KEY_3_Pin);
break;
case USER_BUTTON_WAKEUP:
tmp = HAL_GPIO_ReadPin(WAKEUP_GPIO_Port, WAKEUP_Pin);
break;
}
return tmp;
}
void bButtonCallback(void *p)
{
flex_button_t *btn = (flex_button_t *)p;
switch(btn->id)
{
case USER_BUTTON_1:
b_log("key_1 event: [%d]\r\n", btn->event);
break;
case USER_BUTTON_2:
b_log("key_2 event: [%d]\r\n", btn->event);
break;
case USER_BUTTON_3:
b_log("key_3 event: [%d]\r\n", btn->event);
break;
case USER_BUTTON_WAKEUP:
b_log("wakeup event: [%d]\r\n", btn->event);
break;
}
}
/* USER CODE END 0 */
调用bButtonInit
/* USER CODE BEGIN WHILE */
bInit();
bButtonInit();
while (1)
{
bExec();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
开始测试1.按键各按一次 2.key_1长按
测试结果:
子模块采用BabyOS master分支 https://gitee.com/notrynohigh/BabyOS
|