Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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