Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

host.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "host.h"
  18. #include "util.h"
  19. #include "debug.h"
  20. #ifdef NKRO_ENABLE
  21. bool keyboard_nkro = false;
  22. #endif
  23. static host_driver_t *driver;
  24. static report_keyboard_t report0;
  25. static report_keyboard_t report1;
  26. report_keyboard_t *keyboard_report = &report0;
  27. report_keyboard_t *keyboard_report_prev = &report1;
  28. static inline void add_key_byte(uint8_t code);
  29. static inline void del_key_byte(uint8_t code);
  30. static inline void add_key_bit(uint8_t code);
  31. static inline void del_key_bit(uint8_t code);
  32. void host_set_driver(host_driver_t *d)
  33. {
  34. driver = d;
  35. }
  36. host_driver_t *host_get_driver(void)
  37. {
  38. return driver;
  39. }
  40. uint8_t host_keyboard_leds(void)
  41. {
  42. if (!driver) return 0;
  43. return (*driver->keyboard_leds)();
  44. }
  45. /* new interface */
  46. void host_register_key(uint8_t key)
  47. {
  48. host_add_key(key);
  49. host_send_keyboard_report();
  50. }
  51. void host_unregister_key(uint8_t key)
  52. {
  53. host_del_key(key);
  54. host_send_keyboard_report();
  55. }
  56. /* keyboard report operations */
  57. void host_add_key(uint8_t key)
  58. {
  59. #ifdef NKRO_ENABLE
  60. if (keyboard_nkro) {
  61. add_key_bit(key);
  62. return;
  63. }
  64. #endif
  65. add_key_byte(key);
  66. }
  67. void host_del_key(uint8_t key)
  68. {
  69. #ifdef NKRO_ENABLE
  70. if (keyboard_nkro) {
  71. del_key_bit(key);
  72. return;
  73. }
  74. #endif
  75. del_key_byte(key);
  76. }
  77. void host_add_mod_bit(uint8_t mod)
  78. {
  79. keyboard_report->mods |= mod;
  80. }
  81. void host_del_mod_bit(uint8_t mod)
  82. {
  83. keyboard_report->mods &= ~mod;
  84. }
  85. void host_set_mods(uint8_t mods)
  86. {
  87. keyboard_report->mods = mods;
  88. }
  89. void host_add_code(uint8_t code)
  90. {
  91. if (IS_MOD(code)) {
  92. host_add_mod_bit(MOD_BIT(code));
  93. } else {
  94. host_add_key(code);
  95. }
  96. }
  97. void host_del_code(uint8_t code)
  98. {
  99. if (IS_MOD(code)) {
  100. host_del_mod_bit(MOD_BIT(code));
  101. } else {
  102. host_del_key(code);
  103. }
  104. }
  105. void host_swap_keyboard_report(void)
  106. {
  107. uint8_t sreg = SREG;
  108. cli();
  109. report_keyboard_t *tmp = keyboard_report_prev;
  110. keyboard_report_prev = keyboard_report;
  111. keyboard_report = tmp;
  112. SREG = sreg;
  113. }
  114. void host_clear_keyboard_report(void)
  115. {
  116. keyboard_report->mods = 0;
  117. for (int8_t i = 0; i < REPORT_KEYS; i++) {
  118. keyboard_report->keys[i] = 0;
  119. }
  120. }
  121. uint8_t host_has_anykey(void)
  122. {
  123. uint8_t cnt = 0;
  124. for (int i = 0; i < REPORT_KEYS; i++) {
  125. if (keyboard_report->keys[i])
  126. cnt++;
  127. }
  128. return cnt;
  129. }
  130. uint8_t host_get_first_key(void)
  131. {
  132. #ifdef NKRO_ENABLE
  133. if (keyboard_nkro) {
  134. uint8_t i = 0;
  135. for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
  136. ;
  137. return i<<3 | biton(keyboard_report->keys[i]);
  138. }
  139. #endif
  140. return keyboard_report->keys[0];
  141. }
  142. void host_send_keyboard_report(void)
  143. {
  144. if (!driver) return;
  145. (*driver->send_keyboard)(keyboard_report);
  146. if (debug_keyboard) {
  147. print("keys: ");
  148. for (int i = 0; i < REPORT_KEYS; i++) {
  149. phex(keyboard_report->keys[i]); print(" ");
  150. }
  151. print(" mods: "); phex(keyboard_report->mods); print("\n");
  152. }
  153. }
  154. void host_mouse_send(report_mouse_t *report)
  155. {
  156. if (!driver) return;
  157. (*driver->send_mouse)(report);
  158. }
  159. void host_system_send(uint16_t data)
  160. {
  161. static uint16_t last_data = 0;
  162. if (data == last_data) return;
  163. last_data = data;
  164. if (!driver) return;
  165. (*driver->send_system)(data);
  166. }
  167. void host_consumer_send(uint16_t data)
  168. {
  169. static uint16_t last_data = 0;
  170. if (data == last_data) return;
  171. last_data = data;
  172. if (!driver) return;
  173. (*driver->send_consumer)(data);
  174. }
  175. static inline void add_key_byte(uint8_t code)
  176. {
  177. // TODO: fix ugly code
  178. int8_t i = 0;
  179. int8_t empty = -1;
  180. for (; i < REPORT_KEYS; i++) {
  181. if (keyboard_report_prev->keys[i] == code) {
  182. keyboard_report->keys[i] = code;
  183. break;
  184. }
  185. if (empty == -1 &&
  186. keyboard_report_prev->keys[i] == 0 &&
  187. keyboard_report->keys[i] == 0) {
  188. empty = i;
  189. }
  190. }
  191. if (i == REPORT_KEYS) {
  192. if (empty != -1) {
  193. keyboard_report->keys[empty] = code;
  194. }
  195. }
  196. }
  197. static inline void del_key_byte(uint8_t code)
  198. {
  199. int i = 0;
  200. for (; i < REPORT_KEYS; i++) {
  201. if (keyboard_report->keys[i] == code) {
  202. keyboard_report->keys[i] = 0;
  203. }
  204. }
  205. }
  206. static inline void add_key_bit(uint8_t code)
  207. {
  208. if ((code>>3) < REPORT_KEYS) {
  209. keyboard_report->keys[code>>3] |= 1<<(code&7);
  210. } else {
  211. debug("add_key_bit: can't add: "); phex(code); debug("\n");
  212. }
  213. }
  214. static inline void del_key_bit(uint8_t code)
  215. {
  216. if ((code>>3) < REPORT_KEYS) {
  217. keyboard_report->keys[code>>3] &= ~(1<<(code&7));
  218. } else {
  219. debug("del_key_bit: can't del: "); phex(code); debug("\n");
  220. }
  221. }