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

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