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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "keyboard.h"
  2. #include "host.h"
  3. #include "layer.h"
  4. #include "matrix_skel.h"
  5. #include "led.h"
  6. #include "usb_keycodes.h"
  7. #include "timer.h"
  8. #include "print.h"
  9. #include "debug.h"
  10. #include "command.h"
  11. #ifdef MOUSEKEY_ENABLE
  12. #include "mousekey.h"
  13. #endif
  14. static uint8_t last_leds = 0;
  15. void keyboard_init(void)
  16. {
  17. timer_init();
  18. matrix_init();
  19. #ifdef PS2_MOUSE_ENABLE
  20. ps2_mouse_init();
  21. #endif
  22. }
  23. void keyboard_proc(void)
  24. {
  25. uint8_t fn_bits = 0;
  26. matrix_scan();
  27. if (matrix_is_modified()) {
  28. if (debug_matrix) matrix_print();
  29. #ifdef DEBUG_LED
  30. // LED flash for debug
  31. DEBUG_LED_CONFIG;
  32. DEBUG_LED_ON;
  33. #endif
  34. }
  35. if (matrix_has_ghost()) {
  36. // should send error?
  37. debug("matrix has ghost!!\n");
  38. return;
  39. }
  40. host_swap_keyboard_report();
  41. host_clear_keyboard_report();
  42. for (int row = 0; row < matrix_rows(); row++) {
  43. for (int col = 0; col < matrix_cols(); col++) {
  44. if (!matrix_is_on(row, col)) continue;
  45. uint8_t code = layer_get_keycode(row, col);
  46. if (code == KB_NO) {
  47. // do nothing
  48. } else if (IS_MOD(code)) {
  49. host_add_mod_bit(MOD_BIT(code));
  50. } else if (IS_FN(code)) {
  51. fn_bits |= FN_BIT(code);
  52. }
  53. #ifdef USB_EXTRA_ENABLE
  54. /* TODO: use new API
  55. // audio control & system control
  56. else if (code == KB_MUTE) {
  57. usb_extra_audio_send(AUDIO_MUTE);
  58. usb_extra_audio_send(0);
  59. _delay_ms(500);
  60. } else if (code == KB_VOLU) {
  61. usb_extra_audio_send(AUDIO_VOL_UP);
  62. usb_extra_audio_send(0);
  63. _delay_ms(200);
  64. } else if (code == KB_VOLD) {
  65. usb_extra_audio_send(AUDIO_VOL_DOWN);
  66. usb_extra_audio_send(0);
  67. _delay_ms(200);
  68. } else if (code == KB_PWR) {
  69. if (suspend && remote_wakeup) {
  70. usb_remote_wakeup();
  71. } else {
  72. usb_extra_system_send(SYSTEM_POWER_DOWN);
  73. }
  74. _delay_ms(1000);
  75. }
  76. */
  77. #endif
  78. else if (IS_KEY(code)) {
  79. host_add_key(code);
  80. }
  81. #ifdef MOUSEKEY_ENABLE
  82. else if (IS_MOUSEKEY(code)) {
  83. mousekey_decode(code);
  84. }
  85. #endif
  86. else {
  87. debug("ignore keycode: "); debug_hex(code); debug("\n");
  88. }
  89. }
  90. }
  91. layer_switching(fn_bits);
  92. if (command_proc()) {
  93. // not send report
  94. return;
  95. }
  96. if (matrix_is_modified()) {
  97. host_send_keyboard_report();
  98. #ifdef DEBUG_LED
  99. // LED flash for debug
  100. DEBUG_LED_CONFIG;
  101. DEBUG_LED_OFF;
  102. #endif
  103. }
  104. #ifdef MOUSEKEY_ENABLE
  105. mousekey_send();
  106. #endif
  107. #ifdef PS2_MOUSE_ENABLE
  108. // TODO: should comform new API
  109. if (ps2_mouse_read() == 0)
  110. ps2_mouse_usb_send();
  111. #endif
  112. if (last_leds != host_keyboard_leds()) {
  113. keyboard_set_leds(host_keyboard_leds());
  114. last_leds = host_keyboard_leds();
  115. }
  116. }
  117. void keyboard_set_leds(uint8_t leds)
  118. {
  119. led_set(leds);
  120. }