1 Star 0 Fork 107

林杰强/key_board

forked from wei/key_board 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
key_board_sample.c 3.94 KB
一键复制 编辑 原始数据 按行查看 历史
#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
},
{
.port = KEY_PORT_J56,
.pin = KEY_PIN_J56,
.valid = KEY_PRESS_LEVEL_J56,
.invalid = KEY_RELEASE_LEVEL_J56
},
};
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
},
};
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;
}
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);
}
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_LEFT, &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),
KEY_PUBLIC_SIG_DEF(KEY_DOWN, &key_pin_sig[2], 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_ENTER, &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),
KEY_PUBLIC_SIG_DEF(KEY_EXIT, &key_pin_sig[2], pin_level_get, KEY_FLAG_PRESS_CONTINUOUS | KEY_FLAG_PRESS_LONG | KEY_FLAG_RELEASE_LONG | KEY_FLAG_PRESS_MULTI | KEY_FLAG_RELEASE_MULTI),
};
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),
};
/*按键 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 < ARRAY_SIZE(key_pin_sig);i++)
{
GPIO_InitStruct.Pin = key_pin_sig[i].pin;
HAL_GPIO_Init(key_pin_sig[i].port, &GPIO_InitStruct);
}
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
for(i = 0;i < ARRAY_SIZE(key_pin_ctrl);i++)
{
GPIO_InitStruct.Pin = key_pin_ctrl[i].pin;
HAL_GPIO_Init(key_pin_ctrl[i].port, &GPIO_InitStruct);
}
key_board_init();
key_board_register(KEY_BOARD_MATRIX, key_public_sig, ARRAY_SIZE(key_public_sig), key_public_ctrl, ARRAY_SIZE(key_public_ctrl));
}
/*使能按键*/
void KeyEnable(void)
{
HAL_GPIO_WritePin(KEY_PORT_J135, KEY_PIN_J135, KEY_CTL_LINE_ENABLE);
HAL_GPIO_WritePin(KEY_PORT_J246, KEY_PIN_J246, KEY_CTL_LINE_ENABLE);
}
/*失能按键*/
void KeyDisable(void)
{
HAL_GPIO_WritePin(KEY_PORT_J135, KEY_PIN_J135, KEY_CTL_LINE_DISABLE);
HAL_GPIO_WritePin(KEY_PORT_J246, KEY_PIN_J246, KEY_CTL_LINE_DISABLE);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/linjieqiang/key_board.git
git@gitee.com:linjieqiang/key_board.git
linjieqiang
key_board
key_board
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385