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

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