From 7164b0c5fb6c05776933d767b92b0596068b4057 Mon Sep 17 00:00:00 2001 From: tianjialong Date: Wed, 1 Mar 2023 16:10:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E6=A4=8D=E6=8C=89=E9=94=AE=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usersrc/zkey.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ usersrc/zkey.h | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 usersrc/zkey.c create mode 100644 usersrc/zkey.h diff --git a/usersrc/zkey.c b/usersrc/zkey.c new file mode 100644 index 0000000..2cecbc8 --- /dev/null +++ b/usersrc/zkey.c @@ -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(); + } +} \ No newline at end of file diff --git a/usersrc/zkey.h b/usersrc/zkey.h new file mode 100644 index 0000000..331f53b --- /dev/null +++ b/usersrc/zkey.h @@ -0,0 +1,65 @@ +#pragma once +#include +#include + +#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 /**/ \ + } \ No newline at end of file