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.

host.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. static host_driver_t *driver;
  24. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  25. static inline void add_key_byte(uint8_t code);
  26. static inline void del_key_byte(uint8_t code);
  27. static inline void add_key_bit(uint8_t code);
  28. static inline void del_key_bit(uint8_t code);
  29. void host_set_driver(host_driver_t *d)
  30. {
  31. driver = d;
  32. }
  33. host_driver_t *host_get_driver(void)
  34. {
  35. return driver;
  36. }
  37. uint8_t host_keyboard_leds(void)
  38. {
  39. if (!driver) return 0;
  40. return (*driver->keyboard_leds)();
  41. }
  42. /* keyboard report operations */
  43. void host_add_key(uint8_t key)
  44. {
  45. #ifdef NKRO_ENABLE
  46. if (keyboard_nkro) {
  47. add_key_bit(key);
  48. return;
  49. }
  50. #endif
  51. add_key_byte(key);
  52. }
  53. void host_del_key(uint8_t key)
  54. {
  55. #ifdef NKRO_ENABLE
  56. if (keyboard_nkro) {
  57. del_key_bit(key);
  58. return;
  59. }
  60. #endif
  61. del_key_byte(key);
  62. }
  63. void host_clear_keys(void)
  64. {
  65. for (int8_t i = 0; i < REPORT_KEYS; i++) {
  66. keyboard_report->keys[i] = 0;
  67. }
  68. }
  69. void host_add_mod_bit(uint8_t mod)
  70. {
  71. keyboard_report->mods |= mod;
  72. }
  73. void host_del_mod_bit(uint8_t mod)
  74. {
  75. keyboard_report->mods &= ~mod;
  76. }
  77. void host_set_mods(uint8_t mods)
  78. {
  79. keyboard_report->mods = mods;
  80. }
  81. void host_clear_mods(void)
  82. {
  83. keyboard_report->mods = 0;
  84. }
  85. uint8_t host_has_anykey(void)
  86. {
  87. uint8_t cnt = 0;
  88. for (int i = 0; i < REPORT_KEYS; i++) {
  89. if (keyboard_report->keys[i])
  90. cnt++;
  91. }
  92. return cnt;
  93. }
  94. uint8_t host_get_first_key(void)
  95. {
  96. #ifdef NKRO_ENABLE
  97. if (keyboard_nkro) {
  98. uint8_t i = 0;
  99. for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
  100. ;
  101. return i<<3 | biton(keyboard_report->keys[i]);
  102. }
  103. #endif
  104. return keyboard_report->keys[0];
  105. }
  106. void host_send_keyboard_report(void)
  107. {
  108. if (!driver) return;
  109. (*driver->send_keyboard)(keyboard_report);
  110. if (debug_keyboard) {
  111. print("keys: ");
  112. for (int i = 0; i < REPORT_KEYS; i++) {
  113. phex(keyboard_report->keys[i]); print(" ");
  114. }
  115. print(" mods: "); phex(keyboard_report->mods); print("\n");
  116. }
  117. }
  118. /* send report */
  119. void host_keyboard_send(report_keyboard_t *report)
  120. {
  121. if (!driver) return;
  122. (*driver->send_keyboard)(report);
  123. }
  124. void host_mouse_send(report_mouse_t *report)
  125. {
  126. if (!driver) return;
  127. (*driver->send_mouse)(report);
  128. }
  129. void host_system_send(uint16_t data)
  130. {
  131. static uint16_t last_data = 0;
  132. if (data == last_data) return;
  133. last_data = data;
  134. if (!driver) return;
  135. (*driver->send_system)(data);
  136. }
  137. void host_consumer_send(uint16_t data)
  138. {
  139. static uint16_t last_data = 0;
  140. if (data == last_data) return;
  141. last_data = data;
  142. if (!driver) return;
  143. (*driver->send_consumer)(data);
  144. }
  145. static inline void add_key_byte(uint8_t code)
  146. {
  147. int8_t i = 0;
  148. int8_t empty = -1;
  149. for (; i < REPORT_KEYS; i++) {
  150. if (keyboard_report->keys[i] == code) {
  151. break;
  152. }
  153. if (empty == -1 && keyboard_report->keys[i] == 0) {
  154. empty = i;
  155. }
  156. }
  157. if (i == REPORT_KEYS) {
  158. if (empty != -1) {
  159. keyboard_report->keys[empty] = code;
  160. }
  161. }
  162. }
  163. static inline void del_key_byte(uint8_t code)
  164. {
  165. int i = 0;
  166. for (; i < REPORT_KEYS; i++) {
  167. if (keyboard_report->keys[i] == code) {
  168. keyboard_report->keys[i] = 0;
  169. }
  170. }
  171. }
  172. static inline void add_key_bit(uint8_t code)
  173. {
  174. if ((code>>3) < REPORT_KEYS) {
  175. keyboard_report->keys[code>>3] |= 1<<(code&7);
  176. } else {
  177. debug("add_key_bit: can't add: "); phex(code); debug("\n");
  178. }
  179. }
  180. static inline void del_key_bit(uint8_t code)
  181. {
  182. if ((code>>3) < REPORT_KEYS) {
  183. keyboard_report->keys[code>>3] &= ~(1<<(code&7));
  184. } else {
  185. debug("del_key_bit: can't del: "); phex(code); debug("\n");
  186. }
  187. }