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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Copyright 2011,2012 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 "keycode.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. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  24. report_mouse_t mouse_report = {};
  25. static host_driver_t *driver;
  26. static uint16_t last_system_report = 0;
  27. static uint16_t last_consumer_report = 0;
  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. /* send report */
  46. void host_keyboard_send(report_keyboard_t *report)
  47. {
  48. if (!driver) return;
  49. (*driver->send_keyboard)(report);
  50. if (debug_keyboard) {
  51. print("keys: ");
  52. for (int i = 0; i < REPORT_KEYS; i++) {
  53. phex(keyboard_report->keys[i]); print(" ");
  54. }
  55. print(" mods: "); phex(keyboard_report->mods); print("\n");
  56. }
  57. }
  58. void host_mouse_send(report_mouse_t *report)
  59. {
  60. if (!driver) return;
  61. (*driver->send_mouse)(report);
  62. }
  63. void host_system_send(uint16_t report)
  64. {
  65. if (report == last_system_report) return;
  66. last_system_report = report;
  67. if (!driver) return;
  68. (*driver->send_system)(report);
  69. }
  70. void host_consumer_send(uint16_t report)
  71. {
  72. if (report == last_consumer_report) return;
  73. last_consumer_report = report;
  74. if (!driver) return;
  75. (*driver->send_consumer)(report);
  76. }
  77. /* keyboard report utils */
  78. void host_add_key(uint8_t key)
  79. {
  80. #ifdef NKRO_ENABLE
  81. if (keyboard_nkro) {
  82. add_key_bit(key);
  83. return;
  84. }
  85. #endif
  86. add_key_byte(key);
  87. }
  88. void host_del_key(uint8_t key)
  89. {
  90. #ifdef NKRO_ENABLE
  91. if (keyboard_nkro) {
  92. del_key_bit(key);
  93. return;
  94. }
  95. #endif
  96. del_key_byte(key);
  97. }
  98. void host_clear_keys(void)
  99. {
  100. for (int8_t i = 0; i < REPORT_KEYS; i++) {
  101. keyboard_report->keys[i] = 0;
  102. }
  103. }
  104. void host_add_mod_bit(uint8_t mod)
  105. {
  106. keyboard_report->mods |= mod;
  107. }
  108. void host_del_mod_bit(uint8_t mod)
  109. {
  110. keyboard_report->mods &= ~mod;
  111. }
  112. void host_set_mods(uint8_t mods)
  113. {
  114. keyboard_report->mods = mods;
  115. }
  116. void host_clear_mods(void)
  117. {
  118. keyboard_report->mods = 0;
  119. }
  120. uint8_t host_has_anykey(void)
  121. {
  122. uint8_t cnt = 0;
  123. for (int i = 0; i < REPORT_KEYS; i++) {
  124. if (keyboard_report->keys[i])
  125. cnt++;
  126. }
  127. return cnt;
  128. }
  129. uint8_t host_has_anymod(void)
  130. {
  131. return bitpop(keyboard_report->mods);
  132. }
  133. uint8_t host_get_first_key(void)
  134. {
  135. #ifdef NKRO_ENABLE
  136. if (keyboard_nkro) {
  137. uint8_t i = 0;
  138. for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
  139. ;
  140. return i<<3 | biton(keyboard_report->keys[i]);
  141. }
  142. #endif
  143. return keyboard_report->keys[0];
  144. }
  145. void host_send_keyboard_report(void)
  146. {
  147. if (!driver) return;
  148. host_keyboard_send(keyboard_report);
  149. }
  150. uint8_t host_mouse_in_use(void)
  151. {
  152. return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h);
  153. }
  154. uint16_t host_last_sysytem_report(void)
  155. {
  156. return last_system_report;
  157. }
  158. uint16_t host_last_consumer_report(void)
  159. {
  160. return last_consumer_report;
  161. }
  162. static inline void add_key_byte(uint8_t code)
  163. {
  164. int8_t i = 0;
  165. int8_t empty = -1;
  166. for (; i < REPORT_KEYS; i++) {
  167. if (keyboard_report->keys[i] == code) {
  168. break;
  169. }
  170. if (empty == -1 && keyboard_report->keys[i] == 0) {
  171. empty = i;
  172. }
  173. }
  174. if (i == REPORT_KEYS) {
  175. if (empty != -1) {
  176. keyboard_report->keys[empty] = code;
  177. }
  178. }
  179. }
  180. static inline void del_key_byte(uint8_t code)
  181. {
  182. int i = 0;
  183. for (; i < REPORT_KEYS; i++) {
  184. if (keyboard_report->keys[i] == code) {
  185. keyboard_report->keys[i] = 0;
  186. }
  187. }
  188. }
  189. static inline void add_key_bit(uint8_t code)
  190. {
  191. if ((code>>3) < REPORT_KEYS) {
  192. keyboard_report->keys[code>>3] |= 1<<(code&7);
  193. } else {
  194. debug("add_key_bit: can't add: "); phex(code); debug("\n");
  195. }
  196. }
  197. static inline void del_key_bit(uint8_t code)
  198. {
  199. if ((code>>3) < REPORT_KEYS) {
  200. keyboard_report->keys[code>>3] &= ~(1<<(code&7));
  201. } else {
  202. debug("del_key_bit: can't del: "); phex(code); debug("\n");
  203. }
  204. }