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.

keyboard.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "keyboard.h"
  15. #include "host.h"
  16. #include "layer.h"
  17. #include "matrix.h"
  18. #include "led.h"
  19. #include "usb_keycodes.h"
  20. #include "timer.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "command.h"
  24. #ifdef MOUSEKEY_ENABLE
  25. #include "mousekey.h"
  26. #endif
  27. #ifdef EXTRAKEY_ENABLE
  28. #include <util/delay.h>
  29. #endif
  30. static uint8_t last_leds = 0;
  31. void keyboard_init(void)
  32. {
  33. timer_init();
  34. matrix_init();
  35. #ifdef PS2_MOUSE_ENABLE
  36. ps2_mouse_init();
  37. #endif
  38. }
  39. void keyboard_proc(void)
  40. {
  41. uint8_t fn_bits = 0;
  42. #ifdef EXTRAKEY_ENABLE
  43. uint16_t consumer_code = 0;
  44. uint16_t system_code = 0;
  45. #endif
  46. matrix_scan();
  47. if (matrix_is_modified()) {
  48. if (debug_matrix) matrix_print();
  49. #ifdef DEBUG_LED
  50. // LED flash for debug
  51. DEBUG_LED_CONFIG;
  52. DEBUG_LED_ON;
  53. #endif
  54. }
  55. if (matrix_has_ghost()) {
  56. // should send error?
  57. debug("matrix has ghost!!\n");
  58. return;
  59. }
  60. host_swap_keyboard_report();
  61. host_clear_keyboard_report();
  62. for (int row = 0; row < matrix_rows(); row++) {
  63. for (int col = 0; col < matrix_cols(); col++) {
  64. if (!matrix_is_on(row, col)) continue;
  65. uint8_t code = layer_get_keycode(row, col);
  66. if (code == KB_NO) {
  67. // do nothing
  68. } else if (IS_MOD(code)) {
  69. host_add_mod_bit(MOD_BIT(code));
  70. } else if (IS_FN(code)) {
  71. fn_bits |= FN_BIT(code);
  72. }
  73. // TODO: use table or something
  74. #ifdef EXTRAKEY_ENABLE
  75. // System Control
  76. else if (code == KB_SYSTEM_POWER) {
  77. #ifdef HOST_PJRC
  78. if (suspend && remote_wakeup) {
  79. usb_remote_wakeup();
  80. }
  81. #endif
  82. system_code = SYSTEM_POWER_DOWN;
  83. } else if (code == KB_SYSTEM_SLEEP) {
  84. system_code = SYSTEM_SLEEP;
  85. } else if (code == KB_SYSTEM_WAKE) {
  86. system_code = SYSTEM_WAKE_UP;
  87. }
  88. // Consumer Page
  89. else if (code == KB_AUDIO_MUTE) {
  90. consumer_code = AUDIO_MUTE;
  91. } else if (code == KB_AUDIO_VOL_UP) {
  92. consumer_code = AUDIO_VOL_UP;
  93. } else if (code == KB_AUDIO_VOL_DOWN) {
  94. consumer_code = AUDIO_VOL_DOWN;
  95. }
  96. else if (code == KB_MEDIA_NEXT_TRACK) {
  97. consumer_code = TRANSPORT_NEXT_TRACK;
  98. } else if (code == KB_MEDIA_PREV_TRACK) {
  99. consumer_code = TRANSPORT_PREV_TRACK;
  100. } else if (code == KB_MEDIA_STOP) {
  101. consumer_code = TRANSPORT_STOP;
  102. } else if (code == KB_MEDIA_PLAY_PAUSE) {
  103. consumer_code = TRANSPORT_PLAY_PAUSE;
  104. } else if (code == KB_MEDIA_SELECT) {
  105. consumer_code = AL_CC_CONFIG;
  106. }
  107. else if (code == KB_MAIL) {
  108. consumer_code = AL_EMAIL;
  109. } else if (code == KB_CALCULATOR) {
  110. consumer_code = AL_CALCULATOR;
  111. } else if (code == KB_MY_COMPUTER) {
  112. consumer_code = AL_LOCAL_BROWSER;
  113. }
  114. else if (code == KB_WWW_SEARCH) {
  115. consumer_code = AC_SEARCH;
  116. } else if (code == KB_WWW_HOME) {
  117. consumer_code = AC_HOME;
  118. } else if (code == KB_WWW_BACK) {
  119. consumer_code = AC_BACK;
  120. } else if (code == KB_WWW_FORWARD) {
  121. consumer_code = AC_FORWARD;
  122. } else if (code == KB_WWW_STOP) {
  123. consumer_code = AC_STOP;
  124. } else if (code == KB_WWW_REFRESH) {
  125. consumer_code = AC_REFRESH;
  126. } else if (code == KB_WWW_FAVORITES) {
  127. consumer_code = AC_BOOKMARKS;
  128. }
  129. #endif
  130. else if (IS_KEY(code)) {
  131. host_add_key(code);
  132. }
  133. #ifdef MOUSEKEY_ENABLE
  134. else if (IS_MOUSEKEY(code)) {
  135. mousekey_decode(code);
  136. }
  137. #endif
  138. else {
  139. debug("ignore keycode: "); debug_hex(code); debug("\n");
  140. }
  141. }
  142. }
  143. layer_switching(fn_bits);
  144. if (command_proc()) {
  145. return;
  146. }
  147. // TODO: should send only when changed from last report
  148. if (matrix_is_modified()) {
  149. host_send_keyboard_report();
  150. #ifdef EXTRAKEY_ENABLE
  151. host_consumer_send(consumer_code);
  152. host_system_send(system_code);
  153. #endif
  154. #ifdef DEBUG_LED
  155. // LED flash for debug
  156. DEBUG_LED_CONFIG;
  157. DEBUG_LED_OFF;
  158. #endif
  159. }
  160. #ifdef MOUSEKEY_ENABLE
  161. mousekey_send();
  162. #endif
  163. #ifdef PS2_MOUSE_ENABLE
  164. // TODO: should comform new API
  165. if (ps2_mouse_read() == 0)
  166. ps2_mouse_usb_send();
  167. #endif
  168. if (last_leds != host_keyboard_leds()) {
  169. keyboard_set_leds(host_keyboard_leds());
  170. last_leds = host_keyboard_leds();
  171. }
  172. }
  173. void keyboard_set_leds(uint8_t leds)
  174. {
  175. led_set(leds);
  176. }