logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

custom_matrix.cpp (6863B)


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