1 Star 0 Fork 107

sunzigang/key_board

forked from wei/key_board 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
key_board_sample.c 3.37 KB
一键复制 编辑 原始数据 按行查看 历史
wei 提交于 2021-08-17 21:43 . 优化一处细节
#include "key_board/key_board_sample.h"
struct key_pin_t {
GPIO_TypeDef *port;
uint16_t pin;
GPIO_PinState valid;
GPIO_PinState invalid;
};
const struct key_pin_t key_pin_sig[] = {
{
.port = KEY_PORT_J12,
.pin = KEY_PIN_J12,
.valid = KEY_PRESS_LEVEL_J12,
.invalid = KEY_RELEASE_LEVEL_J12
},
{
.port = KEY_PORT_J34,
.pin = KEY_PIN_J34,
.valid = KEY_PRESS_LEVEL_J34,
.invalid = KEY_RELEASE_LEVEL_J34
},
};
#if (USER_KEY_BOARD_MATRIX)
const struct key_pin_t key_pin_ctrl[] = {
{
.port = KEY_PORT_J135,
.pin = KEY_PIN_J135,
.valid = KEY_CTL_LINE_ENABLE,
.invalid = KEY_CTL_LINE_DISABLE
},
{
.port = KEY_PORT_J246,
.pin = KEY_PIN_J246,
.valid = KEY_CTL_LINE_ENABLE,
.invalid = KEY_CTL_LINE_DISABLE
},
};
#endif
static inline bool pin_level_get(const void *desc)
{
struct key_pin_t *pdesc;
pdesc = (struct key_pin_t *)desc;
return HAL_GPIO_ReadPin(pdesc->port, pdesc->pin) == pdesc->valid;
}
#if (USER_KEY_BOARD_MATRIX)
static inline void pin_level_set(const void *desc, bool flag)
{
struct key_pin_t *pdesc;
pdesc = (struct key_pin_t *)desc;
HAL_GPIO_WritePin(pdesc->port, pdesc->pin, flag ? pdesc->valid : pdesc->invalid);
}
#endif
const struct key_public_sig_t key_public_sig[] = {
KEY_PUBLIC_SIG_DEF(KEY_UP, &key_pin_sig[0], pin_level_get, KEY_FLAG_PRESS_CONTINUOUS | KEY_FLAG_PRESS_LONG | KEY_FLAG_RELEASE_LONG | KEY_FLAG_PRESS_MULTI | KEY_FLAG_RELEASE_MULTI),
KEY_PUBLIC_SIG_DEF(KEY_DOWN, &key_pin_sig[1], pin_level_get, KEY_FLAG_PRESS_CONTINUOUS | KEY_FLAG_PRESS_LONG | KEY_FLAG_RELEASE_LONG | KEY_FLAG_PRESS_MULTI | KEY_FLAG_RELEASE_MULTI),
#if (USER_KEY_BOARD_MATRIX)
KEY_PUBLIC_SIG_DEF(KEY_LEFT, &key_pin_sig[0], pin_level_get, KEY_FLAG_PRESS_CONTINUOUS | KEY_FLAG_PRESS_LONG | KEY_FLAG_RELEASE_LONG | KEY_FLAG_PRESS_MULTI | KEY_FLAG_RELEASE_MULTI),
KEY_PUBLIC_SIG_DEF(KEY_RIGHT, &key_pin_sig[1], pin_level_get, KEY_FLAG_PRESS_CONTINUOUS | KEY_FLAG_PRESS_LONG | KEY_FLAG_RELEASE_LONG | KEY_FLAG_PRESS_MULTI | KEY_FLAG_RELEASE_MULTI)
#endif
};
#if (USER_KEY_BOARD_MATRIX)
const struct key_public_ctrl_t key_public_ctrl[] = {
KEY_PUBLIC_CTRL_DEF(&key_pin_ctrl[0], pin_level_set),
KEY_PUBLIC_CTRL_DEF(&key_pin_ctrl[1], pin_level_set),
};
#endif
/*按键 GPIO 初始化*/
void GPIO_Key_Board_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
unsigned int i;
RCC_KEY_BOARD_CLK_ENABLE();
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
for(i = 0;i < GET_ARRAY_SIZE(key_pin_sig);i++)
{
GPIO_InitStruct.Pin = key_pin_sig[i].pin;
HAL_GPIO_Init(key_pin_sig[i].port, &GPIO_InitStruct);
}
#if (USER_KEY_BOARD_MATRIX)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
for(i = 0;i < GET_ARRAY_SIZE(key_pin_ctrl);i++)
{
GPIO_InitStruct.Pin = key_pin_ctrl[i].pin;
HAL_GPIO_Init(key_pin_ctrl[i].port, &GPIO_InitStruct);
}
#endif
key_board_init();
#if (USER_KEY_BOARD_MATRIX)
key_board_register(KEY_BOARD_MATRIX, key_public_sig, GET_ARRAY_SIZE(key_public_sig), key_public_ctrl, GET_ARRAY_SIZE(key_public_ctrl));
#else
key_board_register(KEY_BOARD_NORMAL, key_public_sig, GET_ARRAY_SIZE(key_public_sig), NULL, 0);
#endif
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/sunzigang/key_board.git
[email protected]:sunzigang/key_board.git
sunzigang
key_board
key_board
master

搜索帮助