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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usb_usb.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright 2016 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 <stdbool.h>
  16. // USB HID host
  17. #include "Usb.h"
  18. #include "usbhub.h"
  19. #include "hid.h"
  20. #include "hidboot.h"
  21. #include "parser.h"
  22. #include "keycode.h"
  23. #include "util.h"
  24. #include "print.h"
  25. #include "debug.h"
  26. #include "timer.h"
  27. #include "matrix.h"
  28. #include "led.h"
  29. /* KEY CODE to Matrix
  30. *
  31. * HID keycode(1 byte):
  32. * Higher 5 bits indicates ROW and lower 3 bits COL.
  33. *
  34. * 7 6 5 4 3 2 1 0
  35. * +---------------+
  36. * | ROW | COL |
  37. * +---------------+
  38. *
  39. * Matrix space(16 * 16):
  40. * r\c0123456789ABCDEF
  41. * 0 +----------------+
  42. * : | |
  43. * : | |
  44. * 16 +----------------+
  45. */
  46. #define ROW_MASK 0xF0
  47. #define COL_MASK 0x0F
  48. #define CODE(row, col) (((row) << 4) | (col))
  49. #define ROW(code) (((code) & ROW_MASK) >> 4)
  50. #define COL(code) ((code) & COL_MASK)
  51. #define ROW_BITS(code) (1 << COL(code))
  52. // Integrated key state of all keyboards
  53. static report_keyboard_t keyboard_report;
  54. static bool matrix_is_mod =false;
  55. /*
  56. * USB Host Shield HID keyboards
  57. * This supports two cascaded hubs and four keyboards
  58. */
  59. USB usb_host;
  60. USBHub hub1(&usb_host);
  61. USBHub hub2(&usb_host);
  62. HIDBoot<HID_PROTOCOL_KEYBOARD> kbd1(&usb_host);
  63. HIDBoot<HID_PROTOCOL_KEYBOARD> kbd2(&usb_host);
  64. HIDBoot<HID_PROTOCOL_KEYBOARD> kbd3(&usb_host);
  65. HIDBoot<HID_PROTOCOL_KEYBOARD> kbd4(&usb_host);
  66. KBDReportParser kbd_parser1;
  67. KBDReportParser kbd_parser2;
  68. KBDReportParser kbd_parser3;
  69. KBDReportParser kbd_parser4;
  70. uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  71. uint8_t matrix_cols(void) { return MATRIX_COLS; }
  72. bool matrix_has_ghost(void) { return false; }
  73. void matrix_init(void) {
  74. // USB Host Shield setup
  75. usb_host.Init();
  76. kbd1.SetReportParser(0, (HIDReportParser*)&kbd_parser1);
  77. kbd2.SetReportParser(0, (HIDReportParser*)&kbd_parser2);
  78. kbd3.SetReportParser(0, (HIDReportParser*)&kbd_parser3);
  79. kbd4.SetReportParser(0, (HIDReportParser*)&kbd_parser4);
  80. }
  81. static void or_report(report_keyboard_t report) {
  82. // integrate reports into keyboard_report
  83. keyboard_report.mods |= report.mods;
  84. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  85. if (IS_ANY(report.keys[i])) {
  86. for (uint8_t j = 0; j < KEYBOARD_REPORT_KEYS; j++) {
  87. if (! keyboard_report.keys[j]) {
  88. keyboard_report.keys[j] = report.keys[i];
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. uint8_t matrix_scan(void) {
  96. static uint16_t last_time_stamp1 = 0;
  97. static uint16_t last_time_stamp2 = 0;
  98. static uint16_t last_time_stamp3 = 0;
  99. static uint16_t last_time_stamp4 = 0;
  100. // check report came from keyboards
  101. if (kbd_parser1.time_stamp != last_time_stamp1 ||
  102. kbd_parser2.time_stamp != last_time_stamp2 ||
  103. kbd_parser3.time_stamp != last_time_stamp3 ||
  104. kbd_parser4.time_stamp != last_time_stamp4) {
  105. last_time_stamp1 = kbd_parser1.time_stamp;
  106. last_time_stamp2 = kbd_parser2.time_stamp;
  107. last_time_stamp3 = kbd_parser3.time_stamp;
  108. last_time_stamp4 = kbd_parser4.time_stamp;
  109. // clear and integrate all reports
  110. keyboard_report = {};
  111. or_report(kbd_parser1.report);
  112. or_report(kbd_parser2.report);
  113. or_report(kbd_parser3.report);
  114. or_report(kbd_parser4.report);
  115. matrix_is_mod = true;
  116. dprintf("state: %02X %02X", keyboard_report.mods, keyboard_report.reserved);
  117. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  118. dprintf(" %02X", keyboard_report.keys[i]);
  119. }
  120. dprint("\r\n");
  121. } else {
  122. matrix_is_mod = false;
  123. }
  124. uint16_t timer;
  125. timer = timer_read();
  126. usb_host.Task();
  127. timer = timer_elapsed(timer);
  128. if (timer > 100) {
  129. dprintf("host.Task: %d\n", timer);
  130. }
  131. return 1;
  132. }
  133. bool matrix_is_modified(void) {
  134. return matrix_is_mod;
  135. }
  136. bool matrix_is_on(uint8_t row, uint8_t col) {
  137. uint8_t code = CODE(row, col);
  138. if (IS_MOD(code)) {
  139. if (keyboard_report.mods & ROW_BITS(code)) {
  140. return true;
  141. }
  142. }
  143. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  144. if (keyboard_report.keys[i] == code) {
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. matrix_row_t matrix_get_row(uint8_t row) {
  151. uint16_t row_bits = 0;
  152. if (IS_MOD(CODE(row, 0)) && keyboard_report.mods) {
  153. row_bits |= keyboard_report.mods;
  154. }
  155. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  156. if (IS_ANY(keyboard_report.keys[i])) {
  157. if (row == ROW(keyboard_report.keys[i])) {
  158. row_bits |= ROW_BITS(keyboard_report.keys[i]);
  159. }
  160. }
  161. }
  162. return row_bits;
  163. }
  164. uint8_t matrix_key_count(void) {
  165. uint8_t count = 0;
  166. count += bitpop(keyboard_report.mods);
  167. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  168. if (IS_ANY(keyboard_report.keys[i])) {
  169. count++;
  170. }
  171. }
  172. return count;
  173. }
  174. void matrix_print(void) {
  175. print("\nr/c 0123456789ABCDEF\n");
  176. for (uint8_t row = 0; row < matrix_rows(); row++) {
  177. xprintf("%02d: ", row);
  178. print_bin_reverse16(matrix_get_row(row));
  179. print("\n");
  180. }
  181. }
  182. void led_set(uint8_t usb_led)
  183. {
  184. kbd1.SetReport(0, 0, 2, 0, 1, &usb_led);
  185. kbd2.SetReport(0, 0, 2, 0, 1, &usb_led);
  186. kbd3.SetReport(0, 0, 2, 0, 1, &usb_led);
  187. kbd4.SetReport(0, 0, 2, 0, 1, &usb_led);
  188. }