From 1172ea79bb5f2f1a6f371e0ddbaea5c93ae63e66 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sun, 6 Aug 2023 23:14:40 +0800 Subject: [PATCH] add key service --- components/key_monitor/key_service.cpp | 76 ++++++++++++++++++++++++++++++++++ components/key_monitor/key_service.hpp | 67 ++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 components/key_monitor/key_service.cpp create mode 100644 components/key_monitor/key_service.hpp diff --git a/components/key_monitor/key_service.cpp b/components/key_monitor/key_service.cpp new file mode 100644 index 0000000..dc3624b --- /dev/null +++ b/components/key_monitor/key_service.cpp @@ -0,0 +1,76 @@ +#include "key_service.hpp" +using namespace iflytop; +#define TAG "KeyService" +void KeyService::initialize(int keyBufferSize, KeyListener_t listener) { + m_keys = (Key*)malloc(sizeof(Key) * keyBufferSize); + ZASSERT(m_keys != NULL); + this->keyBufferSize = keyBufferSize; + m_listener = listener; + m_lastcallticket = 0; + ZHALCORE::getInstance()->regPeriodJob([this](ZHALCORE::Context& ct) { periodicJob(); }, 20); +} + +KeyService::KeyService() {} +KeyService::~KeyService() {} +void KeyService::pushKey(const char* name, ZGPIO* gpio) { + ZASSERT(m_numKey < keyBufferSize); + Key* key = &m_keys[m_numKey]; + key->name = name; + key->gpio = gpio; + key->last_io_state = key->gpio->getState(); + key->keep_state_count = 0; + key->hasProcessed = false; + key->after_filter_state = false; + key->currentstatekeep_count = 0; + m_numKey++; +} + +void KeyService::processEachAfterFilter(Key* each, bool now_io_state) { + KeyEventContext context; + if (now_io_state != each->last_after_filter_io_state) { + if (now_io_state) { + each->keep_state_count = 0; + each->hasProcessed = false; + // each->cur_state = zks_rising_edge; + context.key = each; + context.gpio = each->gpio; + context.event = zks_rising_edge; + + m_listener(each, zks_rising_edge, &context); + } else { + m_listener(each, zks_falling_edge, &context); + each->keep_state_count = 0; + } + each->last_after_filter_io_state = now_io_state; + } else { + if (now_io_state) { + m_listener(each, zks_keep, &context); + } + } +} + +void KeyService::processKey(Key* each) { + /** + * @brief zkey_process_each + */ + each->keep_state_count++; + + bool now_io_state = each->gpio->getState(); + if (each->currentstatekeep_count < 1000) { + each->currentstatekeep_count++; + } + if (now_io_state != each->last_io_state) { + each->currentstatekeep_count = 0; + } + if (each->currentstatekeep_count >= 1) { + each->after_filter_state = now_io_state; + } + each->last_io_state = now_io_state; + processEachAfterFilter(each, each->after_filter_state); +} + +void KeyService::periodicJob() { + for (int i = 0; i < m_numKey; i++) { + processKey(&m_keys[i]); + } +} \ No newline at end of file diff --git a/components/key_monitor/key_service.hpp b/components/key_monitor/key_service.hpp new file mode 100644 index 0000000..9133521 --- /dev/null +++ b/components/key_monitor/key_service.hpp @@ -0,0 +1,67 @@ +#pragma once +#include + +#include "sdk\hal\zhal.hpp" +namespace iflytop { +using namespace std; +class KeyService; +typedef enum { + // mode1 + zks_keep, + zks_rising_edge, + zks_falling_edge, + + // mode2 + zks_trigger_event, + zks_longtime_trigger_event, +} KeyEvent_t; + +typedef struct Key_s { + public: + const char* name; + ZGPIO* gpio; + bool last_io_state; + bool last_after_filter_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; // +} Key; + +class KeyEventContext { + public: + KeyEvent_t event; + ZGPIO* gpio; + Key* key; +}; + +class KeyService { + public: + typedef function KeyListener_t; + + private: + /* data */ + KeyListener_t m_listener; + Key* m_keys; + int keyBufferSize; + int m_numKey; + uint32_t m_lastcallticket; + + public: + KeyService(); + ~KeyService(); + + void initialize(int keyBufferSize, KeyListener_t listener); + void pushKey(const char* name, ZGPIO* gpio); + + private: + void periodicJob(); + void processKey(Key* each); + void processEachAfterFilter(Key* each, bool now_io_state); +}; + +} // namespace iflytop \ No newline at end of file