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

11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. uint8_t host_get_mods(void)
  105. {
  106. return keyboard_report->mods;
  107. }
  108. void host_add_mods(uint8_t mods)
  109. {
  110. keyboard_report->mods |= mods;
  111. }
  112. void host_del_mods(uint8_t mods)
  113. {
  114. keyboard_report->mods &= ~mods;
  115. }
  116. void host_set_mods(uint8_t mods)
  117. {
  118. keyboard_report->mods = mods;
  119. }
  120. void host_clear_mods(void)
  121. {
  122. keyboard_report->mods = 0;
  123. }
  124. uint8_t host_has_anykey(void)
  125. {
  126. uint8_t cnt = 0;
  127. for (int i = 0; i < REPORT_KEYS; i++) {
  128. if (keyboard_report->keys[i])
  129. cnt++;
  130. }
  131. return cnt;
  132. }
  133. uint8_t host_has_anymod(void)
  134. {
  135. return bitpop(keyboard_report->mods);
  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. host_keyboard_send(keyboard_report);
  153. }
  154. uint8_t host_mouse_in_use(void)
  155. {
  156. return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h);
  157. }
  158. uint16_t host_last_sysytem_report(void)
  159. {
  160. return last_system_report;
  161. }
  162. uint16_t host_last_consumer_report(void)
  163. {
  164. return last_consumer_report;
  165. }
  166. static inline void add_key_byte(uint8_t code)
  167. {
  168. int8_t i = 0;
  169. int8_t empty = -1;
  170. for (; i < REPORT_KEYS; i++) {
  171. if (keyboard_report->keys[i] == code) {
  172. break;
  173. }
  174. if (empty == -1 && keyboard_report->keys[i] == 0) {
  175. empty = i;
  176. }
  177. }
  178. if (i == REPORT_KEYS) {
  179. if (empty != -1) {
  180. keyboard_report->keys[empty] = code;
  181. }
  182. }
  183. }
  184. static inline void del_key_byte(uint8_t code)
  185. {
  186. int i = 0;
  187. for (; i < REPORT_KEYS; i++) {
  188. if (keyboard_report->keys[i] == code) {
  189. keyboard_report->keys[i] = 0;
  190. }
  191. }
  192. }
  193. static inline void add_key_bit(uint8_t code)
  194. {
  195. if ((code>>3) < REPORT_KEYS) {
  196. keyboard_report->keys[code>>3] |= 1<<(code&7);
  197. } else {
  198. debug("add_key_bit: can't add: "); phex(code); debug("\n");
  199. }
  200. }
  201. static inline void del_key_bit(uint8_t code)
  202. {
  203. if ((code>>3) < REPORT_KEYS) {
  204. keyboard_report->keys[code>>3] &= ~(1<<(code&7));
  205. } else {
  206. debug("del_key_bit: can't del: "); phex(code); debug("\n");
  207. }
  208. }