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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 USB_EXTRA_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 USB_EXTRA_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. #ifdef USB_EXTRA_ENABLE
  73. // System Control
  74. else if (code == KB_SYSTEM_POWER) {
  75. #ifdef HOST_PJRC
  76. if (suspend && remote_wakeup) {
  77. usb_remote_wakeup();
  78. } else {
  79. host_system_send(SYSTEM_POWER_DOWN);
  80. }
  81. #else
  82. host_system_send(SYSTEM_POWER_DOWN);
  83. #endif
  84. host_system_send(0);
  85. _delay_ms(500);
  86. } else if (code == KB_SYSTEM_SLEEP) {
  87. host_system_send(SYSTEM_SLEEP);
  88. host_system_send(0);
  89. _delay_ms(500);
  90. } else if (code == KB_SYSTEM_WAKE) {
  91. host_system_send(SYSTEM_WAKE_UP);
  92. host_system_send(0);
  93. _delay_ms(500);
  94. }
  95. // Consumer Page
  96. else if (code == KB_AUDIO_MUTE) {
  97. consumer_code = AUDIO_MUTE;
  98. } else if (code == KB_AUDIO_VOL_UP) {
  99. consumer_code = AUDIO_VOL_UP;
  100. } else if (code == KB_AUDIO_VOL_DOWN) {
  101. consumer_code = AUDIO_VOL_DOWN;
  102. }
  103. else if (code == KB_MEDIA_NEXT_TRACK) {
  104. consumer_code = TRANSPORT_NEXT_TRACK;
  105. } else if (code == KB_MEDIA_PREV_TRACK) {
  106. consumer_code = TRANSPORT_PREV_TRACK;
  107. } else if (code == KB_MEDIA_STOP) {
  108. consumer_code = TRANSPORT_STOP;
  109. } else if (code == KB_MEDIA_PLAY_PAUSE) {
  110. consumer_code = TRANSPORT_PLAY_PAUSE;
  111. } else if (code == KB_MEDIA_SELECT) {
  112. consumer_code = AL_CC_CONFIG;
  113. }
  114. else if (code == KB_MAIL) {
  115. consumer_code = AL_EMAIL;
  116. } else if (code == KB_CALCULATOR) {
  117. consumer_code = AL_CALCULATOR;
  118. } else if (code == KB_MY_COMPUTER) {
  119. consumer_code = AL_LOCAL_BROWSER;
  120. }
  121. else if (code == KB_WWW_SEARCH) {
  122. consumer_code = AC_SEARCH;
  123. } else if (code == KB_WWW_HOME) {
  124. consumer_code = AC_HOME;
  125. } else if (code == KB_WWW_BACK) {
  126. consumer_code = AC_BACK;
  127. } else if (code == KB_WWW_FORWARD) {
  128. consumer_code = AC_FORWARD;
  129. } else if (code == KB_WWW_STOP) {
  130. consumer_code = AC_STOP;
  131. } else if (code == KB_WWW_REFRESH) {
  132. consumer_code = AC_REFRESH;
  133. } else if (code == KB_WWW_FAVORITES) {
  134. consumer_code = AC_BOOKMARKS;
  135. }
  136. #endif
  137. else if (IS_KEY(code)) {
  138. host_add_key(code);
  139. }
  140. #ifdef MOUSEKEY_ENABLE
  141. else if (IS_MOUSEKEY(code)) {
  142. mousekey_decode(code);
  143. }
  144. #endif
  145. else {
  146. debug("ignore keycode: "); debug_hex(code); debug("\n");
  147. }
  148. }
  149. }
  150. layer_switching(fn_bits);
  151. if (command_proc()) {
  152. return;
  153. }
  154. // TODO: should send only when changed from last report
  155. if (matrix_is_modified()) {
  156. host_send_keyboard_report();
  157. #ifdef USB_EXTRA_ENABLE
  158. host_consumer_send(consumer_code);
  159. #endif
  160. #ifdef DEBUG_LED
  161. // LED flash for debug
  162. DEBUG_LED_CONFIG;
  163. DEBUG_LED_OFF;
  164. #endif
  165. }
  166. #ifdef MOUSEKEY_ENABLE
  167. mousekey_send();
  168. #endif
  169. #ifdef PS2_MOUSE_ENABLE
  170. // TODO: should comform new API
  171. if (ps2_mouse_read() == 0)
  172. ps2_mouse_usb_send();
  173. #endif
  174. if (last_leds != host_keyboard_leds()) {
  175. keyboard_set_leds(host_keyboard_leds());
  176. last_leds = host_keyboard_leds();
  177. }
  178. }
  179. void keyboard_set_leds(uint8_t leds)
  180. {
  181. led_set(leds);
  182. }