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

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