5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
23 #define KEY_LONG_DELAY 32 // long key press minimum duration (x10ms), must be less than KEY_REPEAT_DELAY
24 #define KEY_REPEAT_DELAY 40 // press longer than this enables repeat (but does not fire it yet)
25 #define KEY_REPEAT_TRIGGER 48 // repeat trigger, used in combination with m_state to produce decreasing times between repeat events
26 #define KEY_REPEAT_PAUSE_DELAY 64
29 #define FILTERBITS 1 // defines how many bits are used for debounce
31 #define FILTERBITS 4 // defines how many bits are used for debounce
35 #define KSTATE_RPTDELAY 95
36 #define KSTATE_START 97
37 #define KSTATE_PAUSE 98
38 #define KSTATE_KILLED 99
42 struct t_inactivity inactivity
= {0};
46 event_t
getEvent(bool trim
)
49 int8_t k
= EVT_KEY_MASK(s_evt
) - TRM_BASE
;
50 bool trim_evt
= (k
>=0 && k
<TRM_LAST
-TRM_BASE
+1);
52 if (trim
== trim_evt
) {
69 void Key::input(bool val
)
71 // store new value in the bits that hold the key state history (used for debounce)
72 uint8_t t_vals
= m_vals
;
79 if (m_state
&& m_vals
== 0) {
81 if (m_state
!= KSTATE_KILLED
) {
82 // TRACE("key %d BREAK", key());
83 putEvent(EVT_KEY_BREAK(key()));
92 if (m_vals
== ((1<<FILTERBITS
)-1)) {
93 m_state
= KSTATE_START
;
98 // TRACE("key %d FIRST", key());
99 putEvent(EVT_KEY_FIRST(key()));
100 inactivity
.counter
= 0;
101 m_state
= KSTATE_RPTDELAY
;
105 case KSTATE_RPTDELAY
: // gruvin: delay state before first key repeat
106 if (m_cnt
== KEY_LONG_DELAY
) {
107 // generate long key press
108 // TRACE("key %d LONG", key());
109 putEvent(EVT_KEY_LONG(key()));
111 if (m_cnt
== KEY_REPEAT_DELAY
) {
121 if (m_cnt
>= KEY_REPEAT_TRIGGER
) { //3 6 12 24 48 pulses in every 480ms
127 if ((m_cnt
& (m_state
-1)) == 0) {
128 // this produces repeat events that at first repeat slowly and then increase in speed
129 // TRACE("key %d REPEAT", key());
130 if (!IS_SHIFT_KEY(key()))
131 putEvent(EVT_KEY_REPT(key()));
135 case KSTATE_PAUSE
: //pause repeat events
136 if (m_cnt
>= KEY_REPEAT_PAUSE_DELAY
) {
142 case KSTATE_KILLED
: //killed
147 void Key::pauseEvents()
149 m_state
= KSTATE_PAUSE
;
153 void Key::killEvents()
155 // TRACE("key %d killed", key());
156 m_state
= KSTATE_KILLED
;
160 uint8_t Key::key() const
162 return (this - keys
);
165 // Introduce a slight delay in the key repeat sequence
166 void pauseEvents(event_t event
)
168 event
= EVT_KEY_MASK(event
);
169 if (event
< (int)DIM(keys
)) keys
[event
].pauseEvents();
172 // Disables any further event generation (BREAK and REPEAT) for this key, until the key is released
173 void killEvents(event_t event
)
175 #if defined(ROTARY_ENCODERS)
176 if (event
== EVT_ROTARY_LONG
) {
177 killEvents(BTN_REa
+ g_eeGeneral
.reNavigation
- 1);
182 event
= EVT_KEY_MASK(event
);
183 if (event
< (int)DIM(keys
)) {
184 keys
[event
].killEvents();
189 bool clearKeyEvents()
191 #if defined(PCBSKY9X)
192 CoTickDelay(100); // 200ms
195 // loop until all keys are up
197 tmr10ms_t start
= get_tmr10ms();
203 SIMU_SLEEP_NORET(1/*ms*/);
209 if ((get_tmr10ms() - start
) >= 300) { // wait no more than 3 seconds
210 //timeout expired, at least one key stuck
216 memclear(keys
, sizeof(keys
));
221 void clearKeyEvents()
223 // loop until all keys are up
232 #if defined(PCBSTD) && defined(ROTARY_ENCODER_NAVIGATION) && !defined(TELEMETREZ)
237 memclear(keys
, sizeof(keys
));
240 #endif // #if defined(CPUARM)