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.

layer.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "keymap_skel.h"
  2. #include "keyboard.h"
  3. #include "debug.h"
  4. #include "timer.h"
  5. #include "layer.h"
  6. /*
  7. * Parameters:
  8. * ENTER_DELAY |=======|
  9. * SEND_FN_TERM |================|
  10. *
  11. * Fn key processing cases:
  12. * 1. release Fn after SEND_FN_TERM.
  13. * Layer sw ___________|~~~~~~~~~~~|___
  14. * Fn press ___|~~~~~~~~~~~~~~~~~~~|___
  15. * Fn send ___________________________
  16. *
  17. * 2. release Fn during SEND_FN_TERM.(not layer used)
  18. * Layer sw ___________|~~~~~~|________
  19. * Fn press ___|~~~~~~~~~~~~~~|________
  20. * Fn key send __________________|~|______
  21. * other key press ___________________________
  22. * other key send ___________________________
  23. *
  24. * 3. release Fn during SEND_FN_TERM.(layer used)
  25. * Layer sw ___________|~~~~~~|________
  26. * Fn press ___|~~~~~~~~~~~~~~|________
  27. * Fn key send ___________________________
  28. * Fn send ___________________________
  29. * other key press _____________|~~|__________
  30. * other key send _____________|~~|__________
  31. *
  32. * 4. press other key during ENTER_DELAY.
  33. * Layer sw ___________________________
  34. * Fn key press ___|~~~~~~~~~|_____________
  35. * Fn key send ______|~~~~~~|_____________
  36. * other key press ______|~~~|________________
  37. * other key send _______|~~|________________
  38. *
  39. * 5. press Fn while press other key.
  40. * Layer sw ___________________________
  41. * Fn key press ___|~~~~~~~~~|_____________
  42. * Fn key send ___|~~~~~~~~~|_____________
  43. * other key press ~~~~~~~|___________________
  44. * other key send ~~~~~~~|___________________
  45. *
  46. * 6. press Fn twice quickly and keep holding down.(repeat)
  47. * Layer sw ___________________________
  48. * Fn key press ___|~|____|~~~~~~~~~~~~~~~~
  49. * Fn key send _____|~|__|~~~~~~~~~~~~~~~~
  50. */
  51. // LAYER_ENTER_DELAY: prevent from moving new layer
  52. #define LAYER_ENTER_DELAY 10
  53. // LAYER_SEND_FN_TERM: send keycode if release key in this term
  54. #define LAYER_SEND_FN_TERM 40
  55. uint8_t default_layer = 0;
  56. uint8_t current_layer = 0;
  57. static bool layer_used = false;
  58. static uint8_t new_layer(uint8_t fn_bits);
  59. uint8_t layer_get_keycode(uint8_t row, uint8_t col)
  60. {
  61. uint8_t code = keymap_get_keycode(current_layer, row, col);
  62. // normal key or mouse key
  63. if ((IS_KEY(code) || IS_MOUSEKEY(code))) {
  64. layer_used = true;
  65. }
  66. return code;
  67. }
  68. // bit substract b from a
  69. #define BIT_SUBT(a, b) (a&(a^b))
  70. void layer_switching(uint8_t fn_bits)
  71. {
  72. // layer switching
  73. static uint8_t last_fn = 0;
  74. static uint8_t last_mods = 0;
  75. static uint16_t last_timer = 0;
  76. static uint8_t sent_fn = 0;
  77. if (fn_bits == last_fn) { // Fn state is not changed
  78. if (fn_bits == 0) {
  79. // do nothing
  80. } else {
  81. if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
  82. uint8_t _layer_to_switch = new_layer(BIT_SUBT(fn_bits, sent_fn));
  83. if (current_layer != _layer_to_switch) { // not switch layer yet
  84. debug("Fn case: 1,2,3(LAYER_ENTER_DELAY passed)\n");
  85. debug("Switch Layer: "); debug_hex(current_layer);
  86. current_layer = _layer_to_switch;
  87. layer_used = false;
  88. debug(" -> "); debug_hex(current_layer); debug("\n");
  89. }
  90. } else {
  91. if (keyboard_has_key()) { // other keys is pressed
  92. uint8_t _fn_to_send = BIT_SUBT(fn_bits, sent_fn);
  93. if (_fn_to_send) {
  94. debug("Fn case: 4(send Fn before other key pressed)\n");
  95. // send only Fn key first
  96. keyboard_swap_report();
  97. keyboard_clear_report();
  98. keyboard_add_code(keymap_fn_keycode(_fn_to_send)); // TODO: do all Fn keys
  99. keyboard_add_mod(last_mods);
  100. keyboard_send();
  101. keyboard_swap_report();
  102. sent_fn |= _fn_to_send;
  103. }
  104. }
  105. }
  106. // add Fn keys to send
  107. //keyboard_add_code(keymap_fn_keycode(fn_bits&sent_fn)); // TODO: do all Fn keys
  108. }
  109. } else { // Fn state is changed(edge)
  110. uint8_t fn_changed = 0;
  111. debug("fn_bits: "); debug_bin(fn_bits); debug("\n");
  112. debug("sent_fn: "); debug_bin(sent_fn); debug("\n");
  113. debug("last_fn: "); debug_bin(last_fn); debug("\n");
  114. debug("last_mods: "); debug_hex(last_mods); debug("\n");
  115. debug("last_timer: "); debug_hex16(last_timer); debug("\n");
  116. // pressed Fn
  117. if ((fn_changed = BIT_SUBT(fn_bits, last_fn))) {
  118. debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
  119. if (keyboard_has_key()) {
  120. debug("Fn case: 5(pressed Fn with other key)\n");
  121. sent_fn |= fn_changed;
  122. } else if (fn_changed & sent_fn) { // pressed same Fn in a row
  123. if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
  124. debug("Fn case: 6(repate2)\n");
  125. // time passed: not repeate
  126. sent_fn &= ~fn_changed;
  127. } else {
  128. debug("Fn case: 6(repate)\n");
  129. }
  130. }
  131. }
  132. // released Fn
  133. if ((fn_changed = BIT_SUBT(last_fn, fn_bits))) {
  134. debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
  135. if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) {
  136. //if (!layer_used && BIT_SUBT(fn_changed, sent_fn)) { // layer not used && Fn not sent
  137. if (BIT_SUBT(fn_changed, sent_fn)) { // layer not used && Fn not sent
  138. debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n");
  139. // send only Fn key first
  140. keyboard_swap_report();
  141. keyboard_clear_report();
  142. keyboard_add_code(keymap_fn_keycode(fn_changed)); // TODO: do all Fn keys
  143. keyboard_add_mod(last_mods);
  144. keyboard_send();
  145. keyboard_swap_report();
  146. sent_fn |= fn_changed;
  147. }
  148. }
  149. debug("Switch Layer(released Fn): "); debug_hex(current_layer);
  150. current_layer = new_layer(BIT_SUBT(fn_bits, sent_fn));
  151. layer_used = false;
  152. debug(" -> "); debug_hex(current_layer); debug("\n");
  153. }
  154. last_fn = fn_bits;
  155. last_mods = keyboard_report()->mods;
  156. last_timer = timer_read();
  157. }
  158. // send Fn keys
  159. for (uint8_t i = 0; i < 8; i++) {
  160. if ((sent_fn & fn_bits) & (1<<i)) {
  161. keyboard_add_code(keymap_fn_keycode(1<<i));
  162. }
  163. }
  164. }
  165. inline
  166. static uint8_t new_layer(uint8_t fn_bits)
  167. {
  168. return (fn_bits ? keymap_fn_layer(fn_bits) : default_layer);
  169. }