Keyboard firmwares for Atmel AVR and Cortex-M
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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