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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "keymap_skel.h"
  2. #include "usb_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 in 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 in 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 in 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 8
  53. // LAYER_SEND_FN_TERM: send keycode if release key in this term
  54. #define LAYER_SEND_FN_TERM 40
  55. static uint8_t current_layer = 0;
  56. static bool layer_used = false;
  57. uint8_t layer_get_keycode(uint8_t row, uint8_t col)
  58. {
  59. uint8_t code = keymap_get_keycode(current_layer, row, col);
  60. // normal key or mouse key
  61. if ((IS_KEY(code) || IS_MOUSEKEY(code))) {
  62. layer_used = true;
  63. }
  64. return code;
  65. }
  66. void layer_switching(uint8_t fn_bits)
  67. {
  68. // layer switching
  69. static uint8_t new_layer = 0;
  70. static uint8_t last_fn = 0;
  71. static uint8_t last_mods = 0;
  72. static uint16_t last_timer = 0;
  73. static uint8_t sent_fn = 0;
  74. if (fn_bits == last_fn) { // Fn key is not changed
  75. if (current_layer != new_layer) {
  76. // not switch layer yet
  77. if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
  78. debug("Fn case: 1,2,3(switch layer)\n");
  79. // case: 1,2,3
  80. current_layer = new_layer;
  81. debug("timer_elapsed: "); debug_hex16(timer_elapsed(last_timer)); debug("\n");
  82. debug("switch layer: "); debug_hex(current_layer); debug("\n");
  83. } else if (usb_keyboard_has_key()) {
  84. debug("Fn case: 4(send Fn first, then add Fn to report)\n");
  85. // case: 4
  86. // send only Fn key first
  87. usb_keyboard_swap_report();
  88. usb_keyboard_clear_report();
  89. usb_keyboard_add_code(keymap_fn_keycode(last_fn));
  90. usb_keyboard_set_mods(last_mods);
  91. usb_keyboard_send();
  92. usb_keyboard_swap_report();
  93. // add Fn key to send with other keys
  94. usb_keyboard_add_code(keymap_fn_keycode(last_fn));
  95. new_layer = 0;
  96. sent_fn = last_fn;
  97. }
  98. } else {
  99. if (fn_bits && new_layer == 0) {
  100. // case: 4,5,6
  101. usb_keyboard_add_code(keymap_fn_keycode(last_fn));
  102. sent_fn = last_fn;
  103. }
  104. }
  105. } else { // Fn key is changed
  106. if (fn_bits == 0) { // Fn key is released(falling edge)
  107. if (!layer_used && timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) {
  108. debug("Fn case: 2(send Fn)\n");
  109. // case: 2
  110. usb_keyboard_swap_report();
  111. usb_keyboard_clear_report();
  112. usb_keyboard_add_code(keymap_fn_keycode(last_fn));
  113. usb_keyboard_set_mods(last_mods);
  114. usb_keyboard_send();
  115. usb_keyboard_swap_report();
  116. sent_fn = last_fn;
  117. }
  118. debug("Fn case: 1,2,3,4,5,6(return to default layer)\n");
  119. new_layer = 0;
  120. current_layer = 0;
  121. } else { // Fn Key is pressed(rising edge)
  122. if (usb_keyboard_has_key() ||
  123. (fn_bits == sent_fn &&timer_elapsed(last_timer) < LAYER_ENTER_DELAY)) {
  124. debug("Fn case: 5,6(add Fn to repeat)\n");
  125. usb_keyboard_add_code(keymap_fn_keycode(fn_bits));
  126. sent_fn = fn_bits;
  127. } else {
  128. debug("Fn case: 1,2,3,4(ready for switching layer)\n");
  129. // ready for switching layer(case: 1,2,3,4)
  130. new_layer = keymap_fn_layer(fn_bits);
  131. sent_fn = 0;
  132. }
  133. }
  134. layer_used = false;
  135. last_fn = fn_bits;
  136. last_mods = usb_keyboard_mods;
  137. last_timer = timer_read();
  138. debug("new_layer: "); debug_hex(new_layer); debug("\n");
  139. debug("last_fn: "); debug_bin(last_fn); debug("\n");
  140. debug("last_mods: "); debug_hex(last_mods); debug("\n");
  141. debug("last_timer: "); debug_hex16(last_timer); debug("\n");
  142. }
  143. }