2 changed files with 150 additions and 0 deletions
-
85usersrc/zkey.c
-
65usersrc/zkey.h
@ -0,0 +1,85 @@ |
|||||
|
#include "zkey.h" |
||||
|
#define TAG "zkey" |
||||
|
static zkey_module_t *s_module; |
||||
|
static bool s_inited; |
||||
|
|
||||
|
void 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) |
||||
|
{ |
||||
|
/** |
||||
|
* @brief zkey_process_each |
||||
|
*/ |
||||
|
each->keep_state_count++; |
||||
|
|
||||
|
bool now_io_state = each->get_key_state(); |
||||
|
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) |
||||
|
{ |
||||
|
each->after_filter_state = now_io_state; |
||||
|
} |
||||
|
each->last_real_state = now_io_state; |
||||
|
zkey_process_each_after_filter(each, each->after_filter_state); |
||||
|
|
||||
|
/** |
||||
|
* @brief 上报按键状态 |
||||
|
*/ |
||||
|
} |
||||
|
void zkey_do_loop_in_each_period(void *_null) |
||||
|
{ |
||||
|
if (!s_inited) |
||||
|
return; |
||||
|
for (int i = 0; i < s_module->nkey; i++) |
||||
|
{ |
||||
|
zkey_process_each(&s_module->keys[i]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void zkey_init(zkey_module_t *module) |
||||
|
{ |
||||
|
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(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,65 @@ |
|||||
|
#pragma once |
||||
|
#include <stdbool.h> |
||||
|
#include <stdint.h> |
||||
|
|
||||
|
#ifndef ARRAY_SIZE |
||||
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
||||
|
#endif |
||||
|
|
||||
|
typedef enum |
||||
|
{ |
||||
|
// mode1 |
||||
|
zks_keep, |
||||
|
zks_rising_edge, |
||||
|
zks_falling_edge, |
||||
|
|
||||
|
// mode2 |
||||
|
zks_trigger_event, |
||||
|
zks_longtime_trigger_event, |
||||
|
} zkey_event_t; |
||||
|
|
||||
|
typedef void (*zkey_listener_t)(void *keyhandler, zkey_event_t zkey_event, |
||||
|
uint32_t keycode); |
||||
|
typedef void (*zkey_reg_listener_t)(zkey_listener_t listener); |
||||
|
|
||||
|
typedef bool (*get_key_state_t)(void); |
||||
|
typedef zkey_event_t zkey_state_t; |
||||
|
|
||||
|
typedef struct |
||||
|
{ |
||||
|
const char *name; |
||||
|
get_key_state_t get_key_state; |
||||
|
bool last_io_state; |
||||
|
|
||||
|
zkey_state_t cur_state; /**/ |
||||
|
uint32_t keep_state_count; /*按键当前状态保持了多久*/ |
||||
|
|
||||
|
bool hasProcessed; /*useful for user*/ |
||||
|
bool after_filter_state; |
||||
|
|
||||
|
uint32_t currentstatekeep_count; // 消抖使用 |
||||
|
bool last_real_state; // 消抖使用 |
||||
|
|
||||
|
} zkey_t; |
||||
|
|
||||
|
typedef struct |
||||
|
{ |
||||
|
zkey_t *keys; |
||||
|
int nkey; |
||||
|
void (*onkey)(zkey_t *key, zkey_state_t key_state); |
||||
|
} zkey_module_t; |
||||
|
|
||||
|
void zkey_init(zkey_module_t *module); |
||||
|
void zkey_do_loop_in_each_period(void *_null); |
||||
|
|
||||
|
#define ZKEY_INIT(_name, _get_key_state) \ |
||||
|
{ \ |
||||
|
.name = _name, .get_key_state = _get_key_state \ |
||||
|
} |
||||
|
|
||||
|
#define ZMODULE_INIT(_keys, _onkey) /**/ \ |
||||
|
{ /**/ \ |
||||
|
.keys = _keys, /**/ \ |
||||
|
.nkey = ARRAY_SIZE(_keys), /**/ \ |
||||
|
.onkey = _onkey /**/ \ |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue