Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

action_tapping.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "action.h"
  4. #include "action_tapping.h"
  5. #include "timer.h"
  6. #ifdef DEBUG_ACTION
  7. #include "debug.h"
  8. #else
  9. #include "nodebug.h"
  10. #endif
  11. #ifndef NO_ACTION_TAPPING
  12. #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
  13. #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
  14. #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
  15. #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
  16. #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
  17. static keyrecord_t tapping_key = {};
  18. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  19. static uint8_t waiting_buffer_head = 0;
  20. static uint8_t waiting_buffer_tail = 0;
  21. static bool process_tapping(keyrecord_t *record);
  22. static bool waiting_buffer_enq(keyrecord_t record);
  23. static void waiting_buffer_clear(void);
  24. #if TAPPING_TERM >= 500
  25. static bool waiting_buffer_typed(keyevent_t event);
  26. #endif
  27. static bool waiting_buffer_has_anykey_pressed(void);
  28. static void waiting_buffer_scan_tap(void);
  29. static void debug_tapping_key(void);
  30. static void debug_waiting_buffer(void);
  31. void action_tapping_process(keyrecord_t record)
  32. {
  33. if (process_tapping(&record)) {
  34. if (!IS_NOEVENT(record.event)) {
  35. debug("processed: "); debug_record(record); debug("\n");
  36. }
  37. } else {
  38. if (!waiting_buffer_enq(record)) {
  39. // clear all in case of overflow.
  40. debug("OVERFLOW: CLEAR ALL STATES\n");
  41. clear_keyboard();
  42. waiting_buffer_clear();
  43. tapping_key = (keyrecord_t){};
  44. }
  45. }
  46. // process waiting_buffer
  47. if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
  48. debug("---- action_exec: process waiting_buffer -----\n");
  49. }
  50. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  51. if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
  52. debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
  53. debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
  54. } else {
  55. break;
  56. }
  57. }
  58. if (!IS_NOEVENT(record.event)) {
  59. debug("\n");
  60. }
  61. }
  62. /* Tapping
  63. *
  64. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  65. * (without interfering by typing other key)
  66. */
  67. /* return true when key event is processed or consumed. */
  68. bool process_tapping(keyrecord_t *keyp)
  69. {
  70. keyevent_t event = keyp->event;
  71. // if tapping
  72. if (IS_TAPPING_PRESSED()) {
  73. if (WITHIN_TAPPING_TERM(event)) {
  74. if (tapping_key.tap.count == 0) {
  75. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  76. // first tap!
  77. debug("Tapping: First tap(0->1).\n");
  78. tapping_key.tap.count = 1;
  79. tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
  80. debug_tapping_key();
  81. process_action(&tapping_key);
  82. // enqueue
  83. keyp->tap = tapping_key.tap;
  84. return false;
  85. }
  86. #if TAPPING_TERM >= 500
  87. /* This can prevent from typing some tap keys in a row at a time. */
  88. else if (!event.pressed && waiting_buffer_typed(event)) {
  89. // other key typed. not tap.
  90. debug("Tapping: End. No tap. Interfered by typing key\n");
  91. process_action(&tapping_key);
  92. tapping_key = (keyrecord_t){};
  93. debug_tapping_key();
  94. // enqueue
  95. return false;
  96. }
  97. #endif
  98. else {
  99. // other key events shall be enq'd till tapping state settles.
  100. return false;
  101. }
  102. }
  103. // tap_count > 0
  104. else {
  105. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  106. debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
  107. keyp->tap = tapping_key.tap;
  108. process_action(keyp);
  109. tapping_key = *keyp;
  110. debug_tapping_key();
  111. return true;
  112. }
  113. else if (is_tap_key(keyp->event.key) && event.pressed) {
  114. if (tapping_key.tap.count > 1) {
  115. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  116. // unregister key
  117. process_action(&(keyrecord_t){
  118. .tap = tapping_key.tap,
  119. .event.key = tapping_key.event.key,
  120. .event.time = event.time,
  121. .event.pressed = false
  122. });
  123. } else {
  124. debug("Tapping: Start while last tap(1).\n");
  125. }
  126. tapping_key = *keyp;
  127. waiting_buffer_scan_tap();
  128. debug_tapping_key();
  129. return true;
  130. }
  131. else {
  132. if (!IS_NOEVENT(keyp->event)) {
  133. debug("Tapping: key event while last tap(>0).\n");
  134. }
  135. process_action(keyp);
  136. return true;
  137. }
  138. }
  139. }
  140. // after TAPPING_TERM
  141. else {
  142. if (tapping_key.tap.count == 0) {
  143. debug("Tapping: End. Timeout. Not tap(0): ");
  144. debug_event(event); debug("\n");
  145. process_action(&tapping_key);
  146. tapping_key = (keyrecord_t){};
  147. debug_tapping_key();
  148. return false;
  149. } else {
  150. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  151. debug("Tapping: End. last timeout tap release(>0).");
  152. keyp->tap = tapping_key.tap;
  153. process_action(keyp);
  154. tapping_key = (keyrecord_t){};
  155. return true;
  156. }
  157. else if (is_tap_key(keyp->event.key) && event.pressed) {
  158. if (tapping_key.tap.count > 1) {
  159. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  160. // unregister key
  161. process_action(&(keyrecord_t){
  162. .tap = tapping_key.tap,
  163. .event.key = tapping_key.event.key,
  164. .event.time = event.time,
  165. .event.pressed = false
  166. });
  167. } else {
  168. debug("Tapping: Start while last timeout tap(1).\n");
  169. }
  170. tapping_key = *keyp;
  171. waiting_buffer_scan_tap();
  172. debug_tapping_key();
  173. return true;
  174. }
  175. else {
  176. if (!IS_NOEVENT(keyp->event)) {
  177. debug("Tapping: key event while last timeout tap(>0).\n");
  178. }
  179. process_action(keyp);
  180. return true;
  181. }
  182. }
  183. }
  184. } else if (IS_TAPPING_RELEASED()) {
  185. if (WITHIN_TAPPING_TERM(event)) {
  186. if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  187. // sequential tap.
  188. keyp->tap = tapping_key.tap;
  189. keyp->tap.count += 1;
  190. debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
  191. process_action(keyp);
  192. tapping_key = *keyp;
  193. debug_tapping_key();
  194. return true;
  195. } else if (event.pressed && is_tap_key(event.key)) {
  196. // Sequential tap can be interfered with other tap key.
  197. debug("Tapping: Start with interfering other tap.\n");
  198. tapping_key = *keyp;
  199. waiting_buffer_scan_tap();
  200. debug_tapping_key();
  201. return true;
  202. } else {
  203. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  204. process_action(keyp);
  205. return true;
  206. }
  207. } else {
  208. // timeout. no sequential tap.
  209. debug("Tapping: End(Timeout after releasing last tap): ");
  210. debug_event(event); debug("\n");
  211. tapping_key = (keyrecord_t){};
  212. debug_tapping_key();
  213. return false;
  214. }
  215. }
  216. // not tapping satate
  217. else {
  218. if (event.pressed && is_tap_key(event.key)) {
  219. debug("Tapping: Start(Press tap key).\n");
  220. tapping_key = *keyp;
  221. waiting_buffer_scan_tap();
  222. debug_tapping_key();
  223. return true;
  224. } else {
  225. process_action(keyp);
  226. return true;
  227. }
  228. }
  229. }
  230. /*
  231. * Waiting buffer
  232. */
  233. bool waiting_buffer_enq(keyrecord_t record)
  234. {
  235. if (IS_NOEVENT(record.event)) {
  236. return true;
  237. }
  238. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  239. debug("waiting_buffer_enq: Over flow.\n");
  240. return false;
  241. }
  242. waiting_buffer[waiting_buffer_head] = record;
  243. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  244. debug("waiting_buffer_enq: "); debug_waiting_buffer();
  245. return true;
  246. }
  247. void waiting_buffer_clear(void)
  248. {
  249. waiting_buffer_head = 0;
  250. waiting_buffer_tail = 0;
  251. }
  252. #if TAPPING_TERM >= 500
  253. bool waiting_buffer_typed(keyevent_t event)
  254. {
  255. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  256. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. #endif
  263. bool waiting_buffer_has_anykey_pressed(void)
  264. {
  265. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  266. if (waiting_buffer[i].event.pressed) return true;
  267. }
  268. return false;
  269. }
  270. /* scan buffer for tapping */
  271. void waiting_buffer_scan_tap(void)
  272. {
  273. // tapping already is settled
  274. if (tapping_key.tap.count > 0) return;
  275. // invalid state: tapping_key released && tap.count == 0
  276. if (!tapping_key.event.pressed) return;
  277. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  278. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
  279. !waiting_buffer[i].event.pressed &&
  280. WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  281. tapping_key.tap.count = 1;
  282. waiting_buffer[i].tap.count = 1;
  283. process_action(&tapping_key);
  284. debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
  285. debug_waiting_buffer();
  286. return;
  287. }
  288. }
  289. }
  290. /*
  291. * debug print
  292. */
  293. static void debug_tapping_key(void)
  294. {
  295. debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
  296. }
  297. static void debug_waiting_buffer(void)
  298. {
  299. debug("{ ");
  300. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  301. debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
  302. }
  303. debug("}\n");
  304. }
  305. #endif