You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.3 KiB
75 lines
2.3 KiB
#include "key.h"
|
|
static zkey_module_t *s_module;
|
|
static bool s_inited;
|
|
|
|
static void prv_zkey_process_each_after_filter(zkey_t *each, bool now_io_state) {
|
|
if (now_io_state != each->last_io_state) {
|
|
if (now_io_state) {
|
|
each->keep_state_count = 0;
|
|
each->hasProcessed = false;
|
|
each->cur_state = zks_rising_edge;
|
|
s_module->onkey(each, each->cur_state);
|
|
} else {
|
|
each->cur_state = zks_falling_edge;
|
|
s_module->onkey(each, each->cur_state);
|
|
each->keep_state_count = 0;
|
|
}
|
|
each->last_io_state = now_io_state;
|
|
} else {
|
|
each->cur_state = zks_keep;
|
|
if (now_io_state) {
|
|
s_module->onkey(each, each->cur_state);
|
|
}
|
|
}
|
|
}
|
|
|
|
void zkey_process_each(zkey_t *each);
|
|
|
|
void zkey_init(zkey_module_t *module) {
|
|
/**
|
|
* @brief 按键初始化,三个状态的初始化,之前的,现在的,之后的
|
|
*
|
|
*/
|
|
s_module = module;
|
|
s_inited = true; //标志位说明按键初始化完成
|
|
|
|
for (int i = 0; i < s_module->nkey; i++) {
|
|
s_module->keys[i].cur_state = zks_keep;
|
|
s_module->keys[i].last_io_state = s_module->keys[i].get_key_state();
|
|
s_module->keys[i].after_filter_state = s_module->keys[i].get_key_state();
|
|
}
|
|
}
|
|
void zkey_process_each(zkey_t *each) {
|
|
/**
|
|
* @brief 按键是否发生变换,没变换的时候进行时间记录
|
|
*
|
|
* @param each
|
|
*/
|
|
each->keep_state_count++;
|
|
|
|
bool now_io_state = each->get_key_state(); //获取现在的按键io状态
|
|
if (each->currentstatekeep_count < 1000) {
|
|
each->currentstatekeep_count++;
|
|
}
|
|
if (now_io_state != each->last_real_state) //发生变换
|
|
{
|
|
each->currentstatekeep_count = 0;
|
|
}
|
|
if (each->currentstatekeep_count >= 1) //按键状态保持时间大于20ms,更新按键状态
|
|
{
|
|
each->after_filter_state = now_io_state;
|
|
}
|
|
each->last_real_state = now_io_state;
|
|
prv_zkey_process_each_after_filter(each, each->after_filter_state);
|
|
}
|
|
|
|
void zkey_do_loop_in_each_period(void *_null) { /**
|
|
* @brief 循环查询按键的状态
|
|
*
|
|
*/
|
|
if (!s_inited) return;
|
|
for (int i = 0; i < s_module->nkey; i++) //开始对每个按键进行查询
|
|
{
|
|
zkey_process_each(&s_module->keys[i]);
|
|
}
|
|
}
|