2 changed files with 143 additions and 0 deletions
@ -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]); |
||||
|
} |
||||
|
} |
@ -0,0 +1,67 @@ |
|||||
|
#pragma once
|
||||
|
#include <stdint.h>
|
||||
|
|
||||
|
#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<void(Key* key, KeyEvent_t event, KeyEventContext* context)> 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
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue