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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "keymap.h"
  15. #include "host.h"
  16. #include "debug.h"
  17. #include "timer.h"
  18. #include "usb_keycodes.h"
  19. #include "layer.h"
  20. /*
  21. * Parameters:
  22. * ENTER_DELAY |=======|
  23. * SEND_FN_TERM |================|
  24. *
  25. * Fn key processing cases:
  26. * 1. release Fn after SEND_FN_TERM.
  27. * Layer sw ___________|~~~~~~~~~~~|___
  28. * Fn press ___|~~~~~~~~~~~~~~~~~~~|___
  29. * Fn send ___________________________
  30. *
  31. * 2. release Fn during SEND_FN_TERM.(not layer used)
  32. * Layer sw ___________|~~~~~~|________
  33. * Fn press ___|~~~~~~~~~~~~~~|________
  34. * Fn key send __________________|~|______
  35. * other key press ___________________________
  36. * other key send ___________________________
  37. *
  38. * 3. release Fn during SEND_FN_TERM.(layer used)
  39. * Layer sw ___________|~~~~~~|________
  40. * Fn press ___|~~~~~~~~~~~~~~|________
  41. * Fn key send ___________________________
  42. * Fn send ___________________________
  43. * other key press _____________|~~|__________
  44. * other key send _____________|~~|__________
  45. *
  46. * 4. press other key during ENTER_DELAY.
  47. * Layer sw ___________________________
  48. * Fn key press ___|~~~~~~~~~|_____________
  49. * Fn key send ______|~~~~~~|_____________
  50. * other key press ______|~~~|________________
  51. * other key send _______|~~|________________
  52. *
  53. * 5. press Fn while press other key.
  54. * Layer sw ___________________________
  55. * Fn key press ___|~~~~~~~~~|_____________
  56. * Fn key send ___|~~~~~~~~~|_____________
  57. * other key press ~~~~~~~|___________________
  58. * other key send ~~~~~~~|___________________
  59. *
  60. * 6. press Fn twice quickly and keep holding down.(repeat)
  61. * Layer sw ___________________________
  62. * Fn key press ___|~|____|~~~~~~~~~~~~~~~~
  63. * Fn key send _____|~|__|~~~~~~~~~~~~~~~~
  64. */
  65. // LAYER_ENTER_DELAY: prevent from moving new layer
  66. #define LAYER_ENTER_DELAY 150
  67. // LAYER_SEND_FN_TERM: send keycode if release key in this term
  68. #define LAYER_SEND_FN_TERM 500
  69. uint8_t default_layer = 0;
  70. uint8_t current_layer = 0;
  71. static bool layer_used = false;
  72. static uint8_t new_layer(uint8_t fn_bits);
  73. uint8_t layer_get_keycode(uint8_t row, uint8_t col)
  74. {
  75. uint8_t code = keymap_get_keycode(current_layer, row, col);
  76. // normal key or mouse key
  77. if ((IS_KEY(code) || IS_MOUSEKEY(code))) {
  78. layer_used = true;
  79. }
  80. return code;
  81. }
  82. // bit substract b from a
  83. #define BIT_SUBST(a, b) (a&(a^b))
  84. void layer_switching(uint8_t fn_bits)
  85. {
  86. // layer switching
  87. static uint8_t last_fn = 0;
  88. static uint8_t last_mods = 0;
  89. static uint16_t last_timer = 0;
  90. static uint8_t sent_fn = 0;
  91. if (fn_bits == last_fn) { // Fn state is not changed
  92. if (fn_bits == 0) {
  93. // do nothing
  94. } else {
  95. if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
  96. uint8_t _layer_to_switch = new_layer(BIT_SUBST(fn_bits, sent_fn));
  97. if (current_layer != _layer_to_switch) { // not switch layer yet
  98. debug("Fn case: 1,2,3(LAYER_ENTER_DELAY passed)\n");
  99. debug("Switch Layer: "); debug_hex(current_layer);
  100. current_layer = _layer_to_switch;
  101. layer_used = false;
  102. debug(" -> "); debug_hex(current_layer); debug("\n");
  103. }
  104. } else {
  105. if (host_has_anykey()) { // other keys is pressed
  106. uint8_t _fn_to_send = BIT_SUBST(fn_bits, sent_fn);
  107. if (_fn_to_send) {
  108. debug("Fn case: 4(send Fn before other key pressed)\n");
  109. // send only Fn key first
  110. host_swap_keyboard_report();
  111. host_clear_keyboard_report();
  112. host_set_mods(last_mods);
  113. host_add_code(keymap_fn_keycode(_fn_to_send)); // TODO: do all Fn keys
  114. host_send_keyboard_report();
  115. host_swap_keyboard_report();
  116. sent_fn |= _fn_to_send;
  117. }
  118. }
  119. }
  120. // add Fn keys to send
  121. //host_add_code(keymap_fn_keycode(fn_bits&sent_fn)); // TODO: do all Fn keys
  122. }
  123. } else { // Fn state is changed(edge)
  124. uint8_t fn_changed = 0;
  125. debug("fn_bits: "); debug_bin(fn_bits); debug("\n");
  126. debug("sent_fn: "); debug_bin(sent_fn); debug("\n");
  127. debug("last_fn: "); debug_bin(last_fn); debug("\n");
  128. debug("last_mods: "); debug_hex(last_mods); debug("\n");
  129. debug("last_timer: "); debug_hex16(last_timer); debug("\n");
  130. // pressed Fn
  131. if ((fn_changed = BIT_SUBST(fn_bits, last_fn))) {
  132. debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
  133. if (host_has_anykey()) {
  134. debug("Fn case: 5(pressed Fn with other key)\n");
  135. sent_fn |= fn_changed;
  136. } else if (fn_changed & sent_fn) { // pressed same Fn in a row
  137. if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
  138. debug("Fn case: 6(not repeat)\n");
  139. // time passed: not repeate
  140. sent_fn &= ~fn_changed;
  141. } else {
  142. debug("Fn case: 6(repeat)\n");
  143. }
  144. }
  145. }
  146. // released Fn
  147. if ((fn_changed = BIT_SUBST(last_fn, fn_bits))) {
  148. debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
  149. if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) {
  150. if (!layer_used && BIT_SUBST(fn_changed, sent_fn)) {
  151. debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n");
  152. // send only Fn key first
  153. host_swap_keyboard_report();
  154. host_clear_keyboard_report();
  155. host_set_mods(last_mods);
  156. host_add_code(keymap_fn_keycode(fn_changed)); // TODO: do all Fn keys
  157. host_send_keyboard_report();
  158. host_swap_keyboard_report();
  159. sent_fn |= fn_changed;
  160. }
  161. }
  162. debug("Switch Layer(released Fn): "); debug_hex(current_layer);
  163. current_layer = new_layer(BIT_SUBST(fn_bits, sent_fn));
  164. debug(" -> "); debug_hex(current_layer); debug("\n");
  165. }
  166. layer_used = false;
  167. last_fn = fn_bits;
  168. last_mods = keyboard_report->mods;
  169. last_timer = timer_read();
  170. }
  171. // send Fn keys
  172. for (uint8_t i = 0; i < 8; i++) {
  173. if ((sent_fn & fn_bits) & (1<<i)) {
  174. host_add_code(keymap_fn_keycode(1<<i));
  175. }
  176. }
  177. }
  178. inline
  179. static uint8_t new_layer(uint8_t fn_bits)
  180. {
  181. return (fn_bits ? keymap_fn_layer(fn_bits) : default_layer);
  182. }