Keys code cleanup and documentation (#4692)

This commit is contained in:
Damjan Adamic
2017-03-26 20:11:06 +02:00
committed by Bertrand Songis
parent 36a6ba24b8
commit 29018fae9c
2 changed files with 67 additions and 40 deletions
+56 -15
View File
@@ -20,8 +20,27 @@
#include "opentx.h" #include "opentx.h"
#define KEY_LONG_DELAY 32 // long key press minimum duration (x10ms), must be less than KEY_REPEAT_DELAY
#define KEY_REPEAT_DELAY 40 // press longer than this enables repeat (but does not fire it yet)
#define KEY_REPEAT_TRIGGER 48 // repeat trigger, used in combination with m_state to produce decreasing times between repeat events
#define KEY_REPEAT_PAUSE_DELAY 64
#ifdef SIMU
#define FILTERBITS 1 // defines how many bits are used for debounce
#else
#define FILTERBITS 4 // defines how many bits are used for debounce
#endif
#define KSTATE_OFF 0
#define KSTATE_RPTDELAY 95
#define KSTATE_START 97
#define KSTATE_PAUSE 98
#define KSTATE_KILLED 99
event_t s_evt; event_t s_evt;
struct t_inactivity inactivity = {0}; struct t_inactivity inactivity = {0};
Key keys[NUM_KEYS];
#if defined(CPUARM) #if defined(CPUARM)
event_t getEvent(bool trim) event_t getEvent(bool trim)
@@ -47,11 +66,9 @@ event_t getEvent()
} }
#endif #endif
#define KEY_LONG_DELAY 32
Key keys[NUM_KEYS];
void Key::input(bool val) void Key::input(bool val)
{ {
// store new value in the bits that hold the key state history (used for debounce)
uint8_t t_vals = m_vals ; uint8_t t_vals = m_vals ;
t_vals <<= 1 ; t_vals <<= 1 ;
if (val) t_vals |= 1; if (val) t_vals |= 1;
@@ -59,32 +76,39 @@ void Key::input(bool val)
m_cnt++; m_cnt++;
if (m_state && m_vals==0) { if (m_state && m_vals == 0) {
// key is released
if (m_state != KSTATE_KILLED) { if (m_state != KSTATE_KILLED) {
// TRACE("key %d BREAK", key());
putEvent(EVT_KEY_BREAK(key())); putEvent(EVT_KEY_BREAK(key()));
} }
m_cnt = 0;
m_state = KSTATE_OFF; m_state = KSTATE_OFF;
m_cnt = 0;
return;
} }
switch (m_state) { switch (m_state) {
case KSTATE_OFF: case KSTATE_OFF:
if (m_vals == FFVAL) { if (m_vals == ((1<<FILTERBITS)-1)) {
m_state = KSTATE_START; m_state = KSTATE_START;
m_cnt = 0; m_cnt = 0;
} }
break; break;
case KSTATE_START: case KSTATE_START:
// TRACE("key %d FIRST", key());
putEvent(EVT_KEY_FIRST(key())); putEvent(EVT_KEY_FIRST(key()));
inactivity.counter = 0; inactivity.counter = 0;
m_state = KSTATE_RPTDELAY; m_state = KSTATE_RPTDELAY;
m_cnt = 0; m_cnt = 0;
break; break;
case KSTATE_RPTDELAY: // gruvin: delay state before first key repeat case KSTATE_RPTDELAY: // gruvin: delay state before first key repeat
if (m_cnt == KEY_LONG_DELAY) { if (m_cnt == KEY_LONG_DELAY) {
// generate long key press
// TRACE("key %d LONG", key());
putEvent(EVT_KEY_LONG(key())); putEvent(EVT_KEY_LONG(key()));
} }
if (m_cnt == 40) { if (m_cnt == KEY_REPEAT_DELAY) {
m_state = 16; m_state = 16;
m_cnt = 0; m_cnt = 0;
} }
@@ -94,21 +118,23 @@ void Key::input(bool val)
case 8: case 8:
case 4: case 4:
case 2: case 2:
if (m_cnt >= 48) { //3 6 12 24 48 pulses in every 480ms if (m_cnt >= KEY_REPEAT_TRIGGER) { //3 6 12 24 48 pulses in every 480ms
m_state >>= 1; m_state >>= 1;
m_cnt = 0; m_cnt = 0;
} }
// no break // no break
case 1: case 1:
if ((m_cnt & (m_state-1)) == 0) { if ((m_cnt & (m_state-1)) == 0) {
// this produces repeat events that at first repeat slowly and then increase in speed
// TRACE("key %d REPEAT", key());
putEvent(EVT_KEY_REPT(key())); putEvent(EVT_KEY_REPT(key()));
} }
break; break;
case KSTATE_PAUSE: //pause case KSTATE_PAUSE: //pause repeat events
if (m_cnt >= 64) { if (m_cnt >= KEY_REPEAT_PAUSE_DELAY) {
m_state = 8; m_state = 8;
m_cnt = 0; m_cnt = 0;
} }
break; break;
@@ -117,17 +143,32 @@ void Key::input(bool val)
} }
} }
void Key::pauseEvents()
{
m_state = KSTATE_PAUSE;
m_cnt = 0;
}
void Key::killEvents()
{
// TRACE("key %d killed", key());
m_state = KSTATE_KILLED;
}
uint8_t Key::key() const uint8_t Key::key() const
{ {
return (this - keys); return (this - keys);
} }
// Introduce a slight delay in the key repeat sequence
void pauseEvents(event_t event) void pauseEvents(event_t event)
{ {
event = EVT_KEY_MASK(event); event = EVT_KEY_MASK(event);
if (event < (int)DIM(keys)) keys[event].pauseEvents(); if (event < (int)DIM(keys)) keys[event].pauseEvents();
} }
// Disables any further event generation (BREAK and REPEAT) for this key, until the key is released
void killEvents(event_t event) void killEvents(event_t event)
{ {
#if defined(ROTARY_ENCODERS) #if defined(ROTARY_ENCODERS)
+10 -24
View File
@@ -41,15 +41,16 @@
#define EVT_ENTRY_UP 0xbe #define EVT_ENTRY_UP 0xbe
#endif #endif
#define EVT_KEY_BREAK(key) ((key)|_MSK_KEY_BREAK) // normal order of events is: FIRST, LONG, REPEAT, REPEAT, ..., BREAK
#define EVT_KEY_FIRST(key) ((key)|_MSK_KEY_FIRST) #define EVT_KEY_FIRST(key) ((key)|_MSK_KEY_FIRST) // fired when key is pressed
#define EVT_KEY_REPT(key) ((key)|_MSK_KEY_REPT) #define EVT_KEY_LONG(key) ((key)|_MSK_KEY_LONG) // fired when key is held pressed for a while
#define EVT_KEY_LONG(key) ((key)|_MSK_KEY_LONG) #define EVT_KEY_REPT(key) ((key)|_MSK_KEY_REPT) // fired when key is held pressed long enough, fires multiple times with increasing speed
#define EVT_KEY_BREAK(key) ((key)|_MSK_KEY_BREAK) // fired when key is released (short or long), but only if the event was not killed
#define IS_KEY_BREAK(evt) (((evt) & _MSK_KEY_FLAGS) == _MSK_KEY_BREAK)
#define IS_KEY_FIRST(evt) (((evt) & _MSK_KEY_FLAGS) == _MSK_KEY_FIRST) #define IS_KEY_FIRST(evt) (((evt) & _MSK_KEY_FLAGS) == _MSK_KEY_FIRST)
#define IS_KEY_LONG(evt) (((evt) & _MSK_KEY_FLAGS) == _MSK_KEY_LONG) #define IS_KEY_LONG(evt) (((evt) & _MSK_KEY_FLAGS) == _MSK_KEY_LONG)
#define IS_KEY_REPT(evt) (((evt) & _MSK_KEY_FLAGS) == _MSK_KEY_REPT) #define IS_KEY_REPT(evt) (((evt) & _MSK_KEY_FLAGS) == _MSK_KEY_REPT)
#define IS_KEY_BREAK(evt) (((evt) & _MSK_KEY_FLAGS) == _MSK_KEY_BREAK)
#if (defined(PCBHORUS) || defined(PCBFLAMENCO) || defined(PCBTARANIS)) && defined(ROTARY_ENCODER_NAVIGATION) #if (defined(PCBHORUS) || defined(PCBFLAMENCO) || defined(PCBTARANIS)) && defined(ROTARY_ENCODER_NAVIGATION)
typedef uint16_t event_t; typedef uint16_t event_t;
@@ -79,34 +80,19 @@
class Key class Key
{ {
#define FILTERBITS 4
#ifdef SIMU
#define FFVAL 1
#else
#define FFVAL ((1<<FILTERBITS)-1)
#endif
#define KSTATE_OFF 0
#define KSTATE_RPTDELAY 95 // gruvin: delay state before key repeating starts
#define KSTATE_START 97
#define KSTATE_PAUSE 98
#define KSTATE_KILLED 99
private: private:
uint8_t m_vals; // key debounce? 4 = 40ms uint8_t m_vals;
uint8_t m_cnt; uint8_t m_cnt;
uint8_t m_state; uint8_t m_state;
public: public:
void input(bool val); void input(bool val);
bool state() { return m_vals > 0; } bool state() const { return m_vals > 0; }
void pauseEvents() { m_state = KSTATE_PAUSE; m_cnt = 0; } void pauseEvents();
void killEvents() { m_state = KSTATE_KILLED; } void killEvents();
uint8_t key() const; uint8_t key() const;
}; };
extern Key keys[NUM_KEYS]; extern Key keys[NUM_KEYS];
extern event_t s_evt; extern event_t s_evt;
#define putEvent(evt) s_evt = evt #define putEvent(evt) s_evt = evt