qwiic_keyboard.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_new_listening_address = QWIIC_KEYBOARD_LISTENING_ADDRESS_START;
  41. uint8_t qwiic_keyboard_listening_address = QWIIC_KEYBOARD_LISTENING_ADDRESS_START;
  42. uint8_t qwiic_keyboard_processing_slave = false;
  43. void qwiic_keyboard_init(void) {
  44. twi2c_init();
  45. twi2c_start();
  46. twi2c_start_listening(qwiic_keyboard_listening_address, qwiic_keyboard_message_received_ptr);
  47. }
  48. void qwiic_keyboard_set_master(void) {
  49. twi2c_stop();
  50. twi2c_start();
  51. qwiic_keyboard_master = true;
  52. }
  53. uint8_t command[1] = { 0x00 };
  54. void qwiic_keyboard_task(void) {
  55. if (USB_DRIVER.state == USB_ACTIVE)
  56. qwiic_keyboard_master = true;
  57. else
  58. qwiic_keyboard_master = false;
  59. if (qwiic_keyboard_master) {
  60. if (qwiic_keyboard_connected) {
  61. // send empty message, expecting matrix info
  62. if (MSG_OK == twi2c_transmit_receive(qwiic_keyboard_listening_address,
  63. command, 1,
  64. qwiic_keyboard_matrix_message, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE
  65. )) {
  66. // majority of this is pulled from keyboard.c:keyboard_task()
  67. static qwiic_matrix_t matrix_prev[QWIIC_KEYBOARD_ROWS];
  68. qwiic_matrix_t matrix_row = 0;
  69. qwiic_matrix_t matrix_change = 0;
  70. qwiic_keyboard_processing_slave = true;
  71. SEND_STRING("K.");
  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 < MATRIX_COLS; c++) {
  77. if (matrix_change & ((qwiic_matrix_t)1<<c)) {
  78. action_exec((keyevent_t){
  79. .key = (keypos_t){ .row = r, .col = c },
  80. .pressed = (matrix_row & ((qwiic_matrix_t)1<<c)),
  81. .time = (timer_read() | 1) /* time should not be 0 */
  82. });
  83. // record a processed key
  84. matrix_prev[r] ^= ((qwiic_matrix_t)1<<c);
  85. #ifdef QMK_KEYS_PER_SCAN
  86. // only jump out if we have processed "enough" keys.
  87. if (++keys_processed >= QMK_KEYS_PER_SCAN)
  88. #endif
  89. // process a key per task call
  90. goto MATRIX_LOOP_END;
  91. }
  92. }
  93. }
  94. }
  95. // call with pseudo tick event when no real key event.
  96. #ifdef QMK_KEYS_PER_SCAN
  97. // we can get here with some keys processed now.
  98. if (!keys_processed)
  99. #endif
  100. action_exec(TICK);
  101. MATRIX_LOOP_END:
  102. qwiic_keyboard_processing_slave = false;
  103. } else {
  104. // disconnect
  105. // qwiic_keyboard_connected = false;
  106. }
  107. } else { // if not connected
  108. // send new address to listen on, expect back keymap
  109. if (MSG_OK == twi2c_transmit_receive(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS,
  110. &qwiic_keyboard_new_listening_address, 1,
  111. qwiic_keyboard_handshake_message, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE
  112. )) {
  113. qwiic_keyboard_connected = true;
  114. // increment address (for future implemenations of supporting multiple devices)
  115. qwiic_keyboard_new_listening_address+=2;
  116. // load keymap into memory
  117. qwiic_keyboard_read_keymap(qwiic_keyboard_handshake_message);
  118. }
  119. }
  120. }
  121. }
  122. uint8_t qwiic_keyboard_reply[MATRIX_ROWS];
  123. float song_one_up[][2] = SONG(ONE_UP_SOUND);
  124. void qwiic_keyboard_message_received(I2CDriver *i2cp, uint8_t * body, uint16_t size) {
  125. if (qwiic_keyboard_connected) {
  126. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  127. qwiic_keyboard_reply[row] = matrix_get_row(row);
  128. }
  129. twi2c_reply(i2cp, qwiic_keyboard_reply, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE);
  130. } else {
  131. qwiic_keyboard_connected = true;
  132. qwiic_keyboard_master = false;
  133. qwiic_keyboard_listening_address = body[0];
  134. qwiic_keyboard_write_keymap(qwiic_keyboard_reply);
  135. twi2c_reply(i2cp, qwiic_keyboard_reply, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE);
  136. twi2c_stop();
  137. twi2c_start();
  138. twi2c_start_listening(qwiic_keyboard_listening_address, qwiic_keyboard_message_received_ptr);
  139. stop_all_notes();
  140. PLAY_SONG(song_one_up);
  141. }
  142. }
  143. // qwiic_keyboard_message_received_ptr = qwiic_keyboard_message_received;
  144. __attribute__((optimize("O0")))
  145. void qwiic_keyboard_write_keymap(uint8_t * pointer) {
  146. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  147. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  148. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  149. uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
  150. *pointer++ = (keycode >> 8);
  151. *pointer++ = (keycode & 0xFF);
  152. }
  153. }
  154. }
  155. }
  156. void qwiic_keyboard_read_keymap(uint8_t * pointer) {
  157. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  158. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  159. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  160. uint16_t keycode = *pointer++;
  161. keycode |= ((*pointer++) << 8);
  162. qwiic_keyboard_keymap[layer][row][col] = keycode;
  163. }
  164. }
  165. }
  166. }
  167. // overwrite the built-in function - slaves don't need to process keycodes
  168. bool is_keyboard_master(void) {
  169. return qwiic_keyboard_master;
  170. }
  171. // overwrite the built-in function
  172. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
  173. if (qwiic_keyboard_processing_slave) {
  174. // trick the built-in handling to accept our replacement keymap
  175. return qwiic_keyboard_keymap[(layer)][(key.row)][(key.col)];
  176. } else {
  177. // Read entire word (16bits)
  178. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  179. }
  180. }