logo

qmk_firmware

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

custom_matrix.cpp (7237B)


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