qwiic_keyboard.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright 2018 Jack Humbert
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "qwiic_keyboard.h"
  17. #include "keymap.h"
  18. #include "matrix.h"
  19. #include "keyboard.h"
  20. #include "twi2c.h"
  21. #include <string.h>
  22. #include "usb_main.h"
  23. #include "usb_driver.h"
  24. #define QWIIC_KEYBOARD_LAYERS 16
  25. #define QWIIC_KEYBOARD_ROWS 8
  26. #define QWIIC_KEYBOARD_COLS 8
  27. #define qwiic_matrix_t uint8_t
  28. #define QWIIC_KEYBOARD_HANDSHAKE_ADDRESS 0b01010100
  29. #define QWIIC_KEYBOARD_LISTENING_ADDRESS_START 0b01010110
  30. #define QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE (QWIIC_KEYBOARD_LAYERS * QWIIC_KEYBOARD_ROWS * QWIIC_KEYBOARD_COLS)
  31. #define QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE MATRIX_ROWS
  32. void qwiic_keyboard_write_keymap(uint8_t * pointer);
  33. void qwiic_keyboard_read_keymap(uint8_t * pointer);
  34. bool qwiic_keyboard_master = false;
  35. bool qwiic_keyboard_connected = false;
  36. uint8_t qwiic_keyboard_handshake_message[QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE] = {0};
  37. uint8_t qwiic_keyboard_matrix_message[QWIIC_KEYBOARD_ROWS] = {0};
  38. twi2c_message_received qwiic_keyboard_message_received_ptr = qwiic_keyboard_message_received;
  39. uint16_t qwiic_keyboard_keymap[QWIIC_KEYBOARD_LAYERS][QWIIC_KEYBOARD_ROWS][QWIIC_KEYBOARD_COLS] = {{{0}}};
  40. uint8_t qwiic_keyboard_listening_address = QWIIC_KEYBOARD_LISTENING_ADDRESS_START;
  41. uint8_t qwiic_keyboard_processing_slave = false;
  42. void qwiic_keyboard_init(void) {
  43. twi2c_init();
  44. twi2c_start();
  45. twi2c_start_listening(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS, qwiic_keyboard_message_received_ptr);
  46. }
  47. void qwiic_keyboard_set_master(void) {
  48. twi2c_stop();
  49. twi2c_start();
  50. qwiic_keyboard_master = true;
  51. }
  52. uint8_t command[1] = { 0x00 };
  53. void qwiic_keyboard_task(void) {
  54. if (USB_DRIVER.state == USB_ACTIVE)
  55. qwiic_keyboard_master = true;
  56. else
  57. qwiic_keyboard_master = false;
  58. if (qwiic_keyboard_master) {
  59. if (qwiic_keyboard_connected) {
  60. // send empty message, expecting matrix info
  61. if (MSG_OK == twi2c_transmit_receive(qwiic_keyboard_listening_address,
  62. command, 1,
  63. qwiic_keyboard_matrix_message, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE
  64. )) {
  65. // majority of this is pulled from keyboard.c:keyboard_task()
  66. static qwiic_matrix_t matrix_prev[QWIIC_KEYBOARD_ROWS];
  67. qwiic_matrix_t matrix_row = 0;
  68. qwiic_matrix_t matrix_change = 0;
  69. #ifdef QMK_KEYS_PER_SCAN
  70. uint8_t keys_processed = 0;
  71. #endif
  72. for (uint8_t r = 0; r < QWIIC_KEYBOARD_ROWS; r++) {
  73. matrix_row = qwiic_keyboard_matrix_message[r];
  74. matrix_change = matrix_row ^ matrix_prev[r];
  75. if (matrix_change) {
  76. for (uint8_t c = 0; c < QWIIC_KEYBOARD_COLS; c++) {
  77. if (matrix_change & ((qwiic_matrix_t)1<<c)) {
  78. action_exec((keyevent_t){
  79. // Always use matrix 1 for remotes now
  80. .key = (keymatrix_t){.pos = (keypos_t){ .row = r, .col = c }, .matrix = 1},
  81. .pressed = (matrix_row & ((qwiic_matrix_t)1<<c)),
  82. .time = (timer_read() | 1) /* time should not be 0 */
  83. });
  84. // record a processed key
  85. matrix_prev[r] ^= ((qwiic_matrix_t)1<<c);
  86. #ifdef QMK_KEYS_PER_SCAN
  87. // only jump out if we have processed "enough" keys.
  88. if (++keys_processed >= QMK_KEYS_PER_SCAN)
  89. #endif
  90. // process a key per task call
  91. goto QWIIC_MATRIX_LOOP_END;
  92. }
  93. }
  94. }
  95. }
  96. // call with pseudo tick event when no real key event.
  97. #ifdef QMK_KEYS_PER_SCAN
  98. // we can get here with some keys processed now.
  99. if (!keys_processed)
  100. #endif
  101. action_exec(TICK);
  102. QWIIC_MATRIX_LOOP_END:
  103. qwiic_keyboard_processing_slave = false;
  104. } else {
  105. // disconnect
  106. // qwiic_keyboard_connected = false;
  107. }
  108. } else { // if not connected
  109. // send new address to listen on, expect back keymap
  110. if (MSG_OK == twi2c_transmit_receive(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS,
  111. &qwiic_keyboard_listening_address, 1,
  112. qwiic_keyboard_handshake_message, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE
  113. )) {
  114. qwiic_keyboard_connected = true;
  115. // load keymap into memory
  116. qwiic_keyboard_read_keymap(qwiic_keyboard_handshake_message);
  117. }
  118. }
  119. }
  120. }
  121. float song_one_up[][2] = SONG(ONE_UP_SOUND);
  122. bool first_message = true;
  123. void qwiic_keyboard_message_received(I2CDriver *i2cp, uint8_t * body, uint16_t size) {
  124. if (qwiic_keyboard_connected) {
  125. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  126. if (row < MATRIX_ROWS) {
  127. qwiic_keyboard_matrix_message[row] = matrix_get_row(row);
  128. } else {
  129. qwiic_keyboard_matrix_message[row] = 0;
  130. }
  131. }
  132. twi2c_reply(i2cp, qwiic_keyboard_matrix_message, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE);
  133. if (first_message) {
  134. PLAY_SONG(song_one_up);
  135. first_message = false;
  136. }
  137. } else {
  138. qwiic_keyboard_connected = true;
  139. qwiic_keyboard_master = false;
  140. qwiic_keyboard_listening_address = body[0];
  141. twi2c_restart_listening(qwiic_keyboard_listening_address);
  142. qwiic_keyboard_write_keymap(qwiic_keyboard_handshake_message);
  143. twi2c_reply(i2cp, qwiic_keyboard_handshake_message, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE);
  144. }
  145. }
  146. // qwiic_keyboard_message_received_ptr = qwiic_keyboard_message_received;
  147. __attribute__((optimize("O0")))
  148. void qwiic_keyboard_write_keymap(uint8_t * pointer) {
  149. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  150. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  151. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  152. uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
  153. *pointer++ = (keycode >> 8);
  154. *pointer++ = (keycode & 0xFF);
  155. }
  156. }
  157. }
  158. }
  159. void qwiic_keyboard_read_keymap(uint8_t * pointer) {
  160. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  161. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  162. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  163. uint16_t keycode = ((*pointer++) << 8);
  164. keycode |= (*pointer++);
  165. qwiic_keyboard_keymap[layer][row][col] = keycode;
  166. }
  167. }
  168. }
  169. }
  170. // overwrite the built-in function - slaves don't need to process keycodes
  171. bool is_keyboard_master(void) {
  172. return qwiic_keyboard_master;
  173. }
  174. // overwrite the built-in function
  175. uint16_t keymap_key_to_keycode(uint8_t layer, keymatrix_t key) {
  176. if (key.matrix == 0) {
  177. // Read entire word (16bits)
  178. return pgm_read_word(&keymaps[(layer)][(key.pos.row)][(key.pos.col)]);
  179. } else {
  180. return qwiic_keyboard_keymap[(layer)][(key.pos.row)][(key.pos.col)];
  181. }
  182. }