You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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. static bool qwiic_keyboard_master = false;
  35. static bool qwiic_keyboard_connected = false;
  36. static uint8_t qwiic_keyboard_handshake_message[QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE] = {0};
  37. static uint8_t qwiic_keyboard_matrix_message[QWIIC_KEYBOARD_ROWS] = {0};
  38. static qwiic_matrix_t matrix_prev[QWIIC_KEYBOARD_ROWS] = {0};
  39. static twi2c_message_received qwiic_keyboard_message_received_ptr = qwiic_keyboard_message_received;
  40. static uint16_t qwiic_keyboard_keymap[QWIIC_KEYBOARD_LAYERS][QWIIC_KEYBOARD_ROWS][QWIIC_KEYBOARD_COLS] = {{{0}}};
  41. static uint8_t qwiic_keyboard_listening_address = QWIIC_KEYBOARD_LISTENING_ADDRESS_START;
  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. // disconnect
  66. // qwiic_keyboard_connected = false;
  67. }
  68. } else { // if not connected
  69. // send new address to listen on, expect back keymap
  70. if (MSG_OK == twi2c_transmit_receive(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS,
  71. &qwiic_keyboard_listening_address, 1,
  72. qwiic_keyboard_handshake_message, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE
  73. )) {
  74. qwiic_keyboard_connected = true;
  75. // load keymap into memory
  76. qwiic_keyboard_read_keymap(qwiic_keyboard_handshake_message);
  77. }
  78. }
  79. }
  80. }
  81. float song_one_up[][2] = SONG(ONE_UP_SOUND);
  82. bool first_message = true;
  83. void qwiic_keyboard_message_received(I2CDriver *i2cp, uint8_t * body, uint16_t size) {
  84. if (qwiic_keyboard_connected) {
  85. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  86. if (row < MATRIX_ROWS) {
  87. qwiic_keyboard_matrix_message[row] = matrix_get_row(row);
  88. } else {
  89. qwiic_keyboard_matrix_message[row] = 0;
  90. }
  91. }
  92. twi2c_reply(i2cp, qwiic_keyboard_matrix_message, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE);
  93. if (first_message) {
  94. PLAY_SONG(song_one_up);
  95. first_message = false;
  96. }
  97. } else {
  98. qwiic_keyboard_connected = true;
  99. qwiic_keyboard_master = false;
  100. qwiic_keyboard_listening_address = body[0];
  101. twi2c_restart_listening(qwiic_keyboard_listening_address);
  102. qwiic_keyboard_write_keymap(qwiic_keyboard_handshake_message);
  103. twi2c_reply(i2cp, qwiic_keyboard_handshake_message, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE);
  104. }
  105. }
  106. // qwiic_keyboard_message_received_ptr = qwiic_keyboard_message_received;
  107. __attribute__((optimize("O0")))
  108. void qwiic_keyboard_write_keymap(uint8_t * pointer) {
  109. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  110. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  111. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  112. uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
  113. *pointer++ = (keycode >> 8);
  114. *pointer++ = (keycode & 0xFF);
  115. }
  116. }
  117. }
  118. }
  119. void qwiic_keyboard_read_keymap(uint8_t * pointer) {
  120. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  121. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  122. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  123. uint16_t keycode = ((*pointer++) << 8);
  124. keycode |= (*pointer++);
  125. qwiic_keyboard_keymap[layer][row][col] = keycode;
  126. }
  127. }
  128. }
  129. }
  130. // overwrite the built-in function - slaves don't need to process keycodes
  131. bool is_keyboard_master(void) {
  132. return qwiic_keyboard_master;
  133. }
  134. // overwrite the built-in function
  135. uint16_t keymap_key_to_keycode(uint8_t layer, keymatrix_t key) {
  136. if (key.matrix == 0) {
  137. // Read entire word (16bits)
  138. return pgm_read_word(&keymaps[(layer)][(key.pos.row)][(key.pos.col)]);
  139. } else {
  140. return qwiic_keyboard_keymap[(layer)][(key.pos.row)][(key.pos.col)];
  141. }
  142. }
  143. uint8_t multimatrix_get_num_matrices(void) {
  144. return qwiic_keyboard_connected ? 1 : 0;
  145. }
  146. uint8_t multimatrix_get_num_cols(uint8_t matrix) {
  147. return QWIIC_KEYBOARD_COLS;
  148. }
  149. uint8_t multimatrix_get_num_rows(uint8_t matrix) {
  150. return QWIIC_KEYBOARD_ROWS;
  151. }
  152. uint32_t multimatrix_get_row(uint8_t matrix, uint8_t row) {
  153. return qwiic_keyboard_matrix_message[row];
  154. }
  155. uint32_t multimatrix_get_row_cache(uint8_t matrix, uint8_t row) {
  156. return matrix_prev[row];
  157. }
  158. void multimatrix_set_row_cache(uint8_t matrix, uint8_t row, uint32_t value) {
  159. matrix_prev[row] = value;
  160. }
  161. uint8_t* multimatrix_get_source_layers_cache(uint8_t matrix) {
  162. static uint8_t source_layers_cache[(QWIIC_KEYBOARD_ROWS * QWIIC_KEYBOARD_COLS * MAX_LAYER_BITS + 7) / 8] = {0};
  163. return source_layers_cache;
  164. }