Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

action_tapping.c 11KB

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