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.

пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <stdint.h>
  15. #include <avr/interrupt.h>
  16. #include "usb_keycodes.h"
  17. #include "usb_keyboard.h"
  18. #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
  19. #include "usb_mouse.h"
  20. #endif
  21. #ifdef EXTRAKEY_ENABLE
  22. #include "usb_extra.h"
  23. #endif
  24. #include "debug.h"
  25. #include "host.h"
  26. #include "util.h"
  27. #ifdef NKRO_ENABLE
  28. bool keyboard_nkro = false;
  29. #endif
  30. static report_keyboard_t report0;
  31. static report_keyboard_t report1;
  32. report_keyboard_t *keyboard_report = &report0;
  33. report_keyboard_t *keyboard_report_prev = &report1;
  34. static inline void add_key_byte(uint8_t code);
  35. static inline void add_key_bit(uint8_t code);
  36. uint8_t host_keyboard_leds(void)
  37. {
  38. return usb_keyboard_leds;
  39. }
  40. /* keyboard report operations */
  41. void host_add_key(uint8_t key)
  42. {
  43. #ifdef NKRO_ENABLE
  44. if (keyboard_nkro) {
  45. add_key_bit(key);
  46. return;
  47. }
  48. #endif
  49. add_key_byte(key);
  50. }
  51. void host_add_mod_bit(uint8_t mod)
  52. {
  53. keyboard_report->mods |= mod;
  54. }
  55. void host_set_mods(uint8_t mods)
  56. {
  57. keyboard_report->mods = mods;
  58. }
  59. void host_add_code(uint8_t code)
  60. {
  61. if (IS_MOD(code)) {
  62. host_add_mod_bit(MOD_BIT(code));
  63. } else {
  64. host_add_key(code);
  65. }
  66. }
  67. void host_swap_keyboard_report(void)
  68. {
  69. uint8_t sreg = SREG;
  70. cli();
  71. report_keyboard_t *tmp = keyboard_report_prev;
  72. keyboard_report_prev = keyboard_report;
  73. keyboard_report = tmp;
  74. SREG = sreg;
  75. }
  76. void host_clear_keyboard_report(void)
  77. {
  78. keyboard_report->mods = 0;
  79. for (int8_t i = 0; i < REPORT_KEYS; i++) {
  80. keyboard_report->keys[i] = 0;
  81. }
  82. }
  83. uint8_t host_has_anykey(void)
  84. {
  85. uint8_t cnt = 0;
  86. for (int i = 0; i < REPORT_KEYS; i++) {
  87. if (keyboard_report->keys[i])
  88. cnt++;
  89. }
  90. return cnt;
  91. }
  92. uint8_t host_get_first_key(void)
  93. {
  94. #ifdef NKRO_ENABLE
  95. if (keyboard_nkro) {
  96. uint8_t i = 0;
  97. for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
  98. ;
  99. return i<<3 | biton(keyboard_report->keys[i]);
  100. }
  101. #endif
  102. return keyboard_report->keys[0];
  103. }
  104. void host_send_keyboard_report(void)
  105. {
  106. usb_keyboard_send_report(keyboard_report);
  107. }
  108. #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
  109. void host_mouse_send(report_mouse_t *report)
  110. {
  111. usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons);
  112. }
  113. #endif
  114. #ifdef EXTRAKEY_ENABLE
  115. void host_system_send(uint16_t data)
  116. {
  117. usb_extra_system_send(data);
  118. }
  119. void host_consumer_send(uint16_t data)
  120. {
  121. static uint16_t last_data = 0;
  122. if (data == last_data) return;
  123. last_data = data;
  124. usb_extra_consumer_send(data);
  125. }
  126. #endif
  127. static inline void add_key_byte(uint8_t code)
  128. {
  129. // TODO: fix ugly code
  130. int8_t i = 0;
  131. int8_t empty = -1;
  132. for (; i < REPORT_KEYS; i++) {
  133. if (keyboard_report_prev->keys[i] == code) {
  134. keyboard_report->keys[i] = code;
  135. break;
  136. }
  137. if (empty == -1 &&
  138. keyboard_report_prev->keys[i] == 0 &&
  139. keyboard_report->keys[i] == 0) {
  140. empty = i;
  141. }
  142. }
  143. if (i == REPORT_KEYS) {
  144. if (empty != -1) {
  145. keyboard_report->keys[empty] = code;
  146. }
  147. }
  148. }
  149. static inline void add_key_bit(uint8_t code)
  150. {
  151. if ((code>>3) < REPORT_KEYS) {
  152. keyboard_report->keys[code>>3] |= 1<<(code&7);
  153. } else {
  154. debug("add_key_bit: can't add: "); phex(code); debug("\n");
  155. }
  156. }