qwiic_keyboard.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. 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. twi2c_transmit(qwiic_keyboard_listening_address, NULL, 0);
  62. if (MSG_OK == twi2c_receive(qwiic_keyboard_listening_address,
  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. qwiic_keyboard_processing_slave = true;
  70. SEND_STRING("K.");
  71. for (uint8_t r = 0; r < QWIIC_KEYBOARD_ROWS; r++) {
  72. matrix_row = qwiic_keyboard_matrix_message[r];
  73. matrix_change = matrix_row ^ matrix_prev[r];
  74. if (matrix_change) {
  75. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  76. if (matrix_change & ((qwiic_matrix_t)1<<c)) {
  77. action_exec((keyevent_t){
  78. .key = (keypos_t){ .row = r, .col = c },
  79. .pressed = (matrix_row & ((qwiic_matrix_t)1<<c)),
  80. .time = (timer_read() | 1) /* time should not be 0 */
  81. });
  82. // record a processed key
  83. matrix_prev[r] ^= ((qwiic_matrix_t)1<<c);
  84. #ifdef QMK_KEYS_PER_SCAN
  85. // only jump out if we have processed "enough" keys.
  86. if (++keys_processed >= QMK_KEYS_PER_SCAN)
  87. #endif
  88. // process a key per task call
  89. goto MATRIX_LOOP_END;
  90. }
  91. }
  92. }
  93. }
  94. // call with pseudo tick event when no real key event.
  95. #ifdef QMK_KEYS_PER_SCAN
  96. // we can get here with some keys processed now.
  97. if (!keys_processed)
  98. #endif
  99. action_exec(TICK);
  100. MATRIX_LOOP_END:
  101. qwiic_keyboard_processing_slave = false;
  102. } else {
  103. // disconnect
  104. // qwiic_keyboard_connected = false;
  105. }
  106. } else {
  107. // send new address to listen on, expect back keymap
  108. twi2c_transmit(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS, &qwiic_keyboard_new_listening_address, 1);
  109. if (MSG_OK == twi2c_receive(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS,
  110. qwiic_keyboard_handshake_message, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE
  111. )) {
  112. qwiic_keyboard_connected = true;
  113. // increment address (for future implemenations of supporting multiple devices)
  114. qwiic_keyboard_new_listening_address+=2;
  115. // load keymap into memory
  116. qwiic_keyboard_read_keymap(qwiic_keyboard_handshake_message);
  117. }
  118. }
  119. }
  120. }
  121. uint8_t qwiic_keyboard_reply[MATRIX_ROWS];
  122. void qwiic_keyboard_message_received(I2CDriver *i2cp, uint8_t * body, uint16_t size) {
  123. if (qwiic_keyboard_connected) {
  124. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  125. qwiic_keyboard_reply[row] = matrix_get_row(row);
  126. }
  127. twi2c_reply(i2cp, qwiic_keyboard_reply, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE);
  128. } else {
  129. qwiic_keyboard_connected = true;
  130. qwiic_keyboard_master = false;
  131. qwiic_keyboard_listening_address = body[0];
  132. qwiic_keyboard_write_keymap(qwiic_keyboard_reply);
  133. twi2c_reply(i2cp, qwiic_keyboard_reply, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE);
  134. twi2c_stop();
  135. twi2c_start();
  136. twi2c_start_listening(qwiic_keyboard_listening_address, qwiic_keyboard_message_received_ptr);
  137. }
  138. }
  139. // qwiic_keyboard_message_received_ptr = qwiic_keyboard_message_received;
  140. __attribute__((optimize("O0")))
  141. void qwiic_keyboard_write_keymap(uint8_t * pointer) {
  142. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  143. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  144. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  145. uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
  146. *pointer++ = (keycode >> 8);
  147. *pointer++ = (keycode & 0xFF);
  148. }
  149. }
  150. }
  151. }
  152. void qwiic_keyboard_read_keymap(uint8_t * pointer) {
  153. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  154. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  155. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  156. uint16_t keycode = *pointer++;
  157. keycode |= (*pointer++) << 8;
  158. qwiic_keyboard_keymap[layer][row][col] = keycode;
  159. }
  160. }
  161. }
  162. }
  163. // overwrite the built-in function - slaves don't need to process keycodes
  164. bool is_keyboard_master(void) {
  165. return qwiic_keyboard_master;
  166. }
  167. // overwrite the built-in function
  168. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
  169. if (qwiic_keyboard_processing_slave) {
  170. // trick the built-in handling to accept our replacement keymap
  171. return qwiic_keyboard_keymap[(layer)][(key.row)][(key.col)];
  172. } else {
  173. // Read entire word (16bits)
  174. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  175. }
  176. }