custom_matrix.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Copyright 2016 Jun Wako <wakojun@gmail.com>
  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. #include "host.h"
  30. #include "keyboard.h"
  31. extern "C" {
  32. #include "quantum.h"
  33. }
  34. /* KEY CODE to Matrix
  35. *
  36. * HID keycode(1 byte):
  37. * Higher 5 bits indicates ROW and lower 3 bits COL.
  38. *
  39. * 7 6 5 4 3 2 1 0
  40. * +---------------+
  41. * | ROW | COL |
  42. * +---------------+
  43. *
  44. * Matrix space(16 * 16):
  45. * r\c0123456789ABCDEF
  46. * 0 +----------------+
  47. * : | |
  48. * : | |
  49. * 16 +----------------+
  50. */
  51. #define ROW_MASK 0xF0
  52. #define COL_MASK 0x0F
  53. #define CODE(row, col) (((row) << 4) | (col))
  54. #define ROW(code) (((code) & ROW_MASK) >> 4)
  55. #define COL(code) ((code) & COL_MASK)
  56. #define ROW_BITS(code) (1 << COL(code))
  57. // Integrated key state of all keyboards
  58. static report_keyboard_t local_keyboard_report;
  59. /*
  60. * USB Host Shield HID keyboards
  61. * This supports two cascaded hubs and four keyboards
  62. */
  63. USB usb_host;
  64. USBHub hub1(&usb_host);
  65. USBHub hub2(&usb_host);
  66. HIDBoot<HID_PROTOCOL_KEYBOARD> kbd1(&usb_host);
  67. HIDBoot<HID_PROTOCOL_KEYBOARD> kbd2(&usb_host);
  68. HIDBoot<HID_PROTOCOL_KEYBOARD> kbd3(&usb_host);
  69. HIDBoot<HID_PROTOCOL_KEYBOARD> kbd4(&usb_host);
  70. KBDReportParser kbd_parser1;
  71. KBDReportParser kbd_parser2;
  72. KBDReportParser kbd_parser3;
  73. KBDReportParser kbd_parser4;
  74. extern "C" {
  75. uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  76. uint8_t matrix_cols(void) { return MATRIX_COLS; }
  77. bool matrix_has_ghost(void) { return false; }
  78. void matrix_init(void) {
  79. // USB Host Shield setup
  80. usb_host.Init();
  81. kbd1.SetReportParser(0, (HIDReportParser*)&kbd_parser1);
  82. kbd2.SetReportParser(0, (HIDReportParser*)&kbd_parser2);
  83. kbd3.SetReportParser(0, (HIDReportParser*)&kbd_parser3);
  84. kbd4.SetReportParser(0, (HIDReportParser*)&kbd_parser4);
  85. matrix_init_kb();
  86. }
  87. static void or_report(report_keyboard_t report) {
  88. // integrate reports into local_keyboard_report
  89. local_keyboard_report.mods |= report.mods;
  90. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  91. if (IS_ANY(report.keys[i])) {
  92. for (uint8_t j = 0; j < KEYBOARD_REPORT_KEYS; j++) {
  93. if (! local_keyboard_report.keys[j]) {
  94. local_keyboard_report.keys[j] = report.keys[i];
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. __attribute__ ((weak))
  102. void matrix_init_kb(void) {
  103. matrix_init_user();
  104. }
  105. __attribute__ ((weak))
  106. void matrix_init_user(void) {
  107. }
  108. __attribute__ ((weak))
  109. void matrix_scan_kb(void) {
  110. matrix_scan_user();
  111. }
  112. __attribute__ ((weak))
  113. void matrix_scan_user(void) {
  114. }
  115. uint8_t matrix_scan(void) {
  116. bool changed = false;
  117. static uint16_t last_time_stamp1 = 0;
  118. static uint16_t last_time_stamp2 = 0;
  119. static uint16_t last_time_stamp3 = 0;
  120. static uint16_t last_time_stamp4 = 0;
  121. // check report came from keyboards
  122. if (kbd_parser1.time_stamp != last_time_stamp1 ||
  123. kbd_parser2.time_stamp != last_time_stamp2 ||
  124. kbd_parser3.time_stamp != last_time_stamp3 ||
  125. kbd_parser4.time_stamp != last_time_stamp4) {
  126. last_time_stamp1 = kbd_parser1.time_stamp;
  127. last_time_stamp2 = kbd_parser2.time_stamp;
  128. last_time_stamp3 = kbd_parser3.time_stamp;
  129. last_time_stamp4 = kbd_parser4.time_stamp;
  130. // clear and integrate all reports
  131. local_keyboard_report = {};
  132. or_report(kbd_parser1.report);
  133. or_report(kbd_parser2.report);
  134. or_report(kbd_parser3.report);
  135. or_report(kbd_parser4.report);
  136. changed = true;
  137. dprintf("state: %02X %02X", local_keyboard_report.mods, local_keyboard_report.reserved);
  138. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  139. dprintf(" %02X", local_keyboard_report.keys[i]);
  140. }
  141. dprint("\r\n");
  142. }
  143. uint16_t timer;
  144. timer = timer_read();
  145. usb_host.Task();
  146. timer = timer_elapsed(timer);
  147. if (timer > 100) {
  148. dprintf("host.Task: %d\n", timer);
  149. }
  150. static uint8_t usb_state = 0;
  151. if (usb_state != usb_host.getUsbTaskState()) {
  152. usb_state = usb_host.getUsbTaskState();
  153. dprintf("usb_state: %02X\n", usb_state);
  154. // restore LED state when keyboard comes up
  155. if (usb_state == USB_STATE_RUNNING) {
  156. dprintf("speed: %s\n", usb_host.getVbusState()==FSHOST ? "full" : "low");
  157. led_set(host_keyboard_leds());
  158. }
  159. }
  160. matrix_scan_kb();
  161. return changed;
  162. }
  163. bool matrix_is_on(uint8_t row, uint8_t col) {
  164. uint8_t code = CODE(row, col);
  165. if (IS_MODIFIER_KEYCODE(code)) {
  166. if (local_keyboard_report.mods & ROW_BITS(code)) {
  167. return true;
  168. }
  169. }
  170. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  171. if (local_keyboard_report.keys[i] == code) {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. matrix_row_t matrix_get_row(uint8_t row) {
  178. uint16_t row_bits = 0;
  179. if (IS_MODIFIER_KEYCODE(CODE(row, 0)) && local_keyboard_report.mods) {
  180. row_bits |= local_keyboard_report.mods;
  181. }
  182. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  183. if (IS_ANY(local_keyboard_report.keys[i])) {
  184. if (row == ROW(local_keyboard_report.keys[i])) {
  185. row_bits |= ROW_BITS(local_keyboard_report.keys[i]);
  186. }
  187. }
  188. }
  189. return row_bits;
  190. }
  191. void matrix_print(void) {
  192. print("\nr/c 0123456789ABCDEF\n");
  193. for (uint8_t row = 0; row < matrix_rows(); row++) {
  194. xprintf("%02d: ", row);
  195. print_bin_reverse16(matrix_get_row(row));
  196. print("\n");
  197. }
  198. }
  199. void led_set(uint8_t usb_led) {
  200. if (kbd1.isReady()) kbd1.SetReport(0, 0, 2, 0, 1, &usb_led);
  201. if (kbd2.isReady()) kbd2.SetReport(0, 0, 2, 0, 1, &usb_led);
  202. if (kbd3.isReady()) kbd3.SetReport(0, 0, 2, 0, 1, &usb_led);
  203. if (kbd4.isReady()) kbd4.SetReport(0, 0, 2, 0, 1, &usb_led);
  204. led_set_user(usb_led);
  205. led_update_kb((led_t){.raw = usb_led});
  206. }
  207. }