1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-26 01:35:21 +03:00

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

View file

@ -2,7 +2,7 @@
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
@ -20,8 +20,27 @@
#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;
struct t_inactivity inactivity = {0};
Key keys[NUM_KEYS];
#if defined(CPUARM)
event_t getEvent(bool trim)
@ -47,11 +66,9 @@ event_t getEvent()
}
#endif
#define KEY_LONG_DELAY 32
Key keys[NUM_KEYS];
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 ;
t_vals <<= 1 ;
if (val) t_vals |= 1;
@ -59,32 +76,39 @@ void Key::input(bool val)
m_cnt++;
if (m_state && m_vals==0) {
if (m_state && m_vals == 0) {
// key is released
if (m_state != KSTATE_KILLED) {
// TRACE("key %d BREAK", key());
putEvent(EVT_KEY_BREAK(key()));
}
m_cnt = 0;
m_state = KSTATE_OFF;
m_cnt = 0;
return;
}
switch (m_state) {
case KSTATE_OFF:
if (m_vals == FFVAL) {
if (m_vals == ((1<<FILTERBITS)-1)) {
m_state = KSTATE_START;
m_cnt = 0;
m_cnt = 0;
}
break;
case KSTATE_START:
// TRACE("key %d FIRST", key());
putEvent(EVT_KEY_FIRST(key()));
inactivity.counter = 0;
m_state = KSTATE_RPTDELAY;
m_cnt = 0;
m_state = KSTATE_RPTDELAY;
m_cnt = 0;
break;
case KSTATE_RPTDELAY: // gruvin: delay state before first key repeat
if (m_cnt == KEY_LONG_DELAY) {
// generate long key press
// TRACE("key %d LONG", key());
putEvent(EVT_KEY_LONG(key()));
}
if (m_cnt == 40) {
if (m_cnt == KEY_REPEAT_DELAY) {
m_state = 16;
m_cnt = 0;
}
@ -94,21 +118,23 @@ void Key::input(bool val)
case 8:
case 4:
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_cnt = 0;
m_cnt = 0;
}
// no break
case 1:
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()));
}
break;
case KSTATE_PAUSE: //pause
if (m_cnt >= 64) {
case KSTATE_PAUSE: //pause repeat events
if (m_cnt >= KEY_REPEAT_PAUSE_DELAY) {
m_state = 8;
m_cnt = 0;
m_cnt = 0;
}
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
{
return (this - keys);
}
// Introduce a slight delay in the key repeat sequence
void pauseEvents(event_t event)
{
event = EVT_KEY_MASK(event);
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)
{
#if defined(ROTARY_ENCODERS)