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.

278 lines
10 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
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. #if (QWIIC_KEYBOARD_COLS <= 8)
  28. typedef uint8_t qwiic_row_t;
  29. #elif (QWIIC_KEYBOARD_COLS <= 16)
  30. typedef uint16_t qwiic_row_t;
  31. #elif (QWIIC_KEYBOARD_COLS <= 32)
  32. typedef uint32_t qwiic_row_t;
  33. #else
  34. #error "QWIIC_KEYBOARD_COLS: invalid value"
  35. #endif
  36. #define QWIIC_KEYBOARD_HANDSHAKE_ADDRESS 0b01010100
  37. #define QWIIC_KEYBOARD_LISTENING_ADDRESS_START 0b01010110
  38. #define QWIIC_KEYBOARD_MAX_DEVICES 1
  39. #define QWIIC_KEYBOARD_KEYMAP_MESSAGE_SIZE (QWIIC_KEYBOARD_LAYERS * QWIIC_KEYBOARD_ROWS * QWIIC_KEYBOARD_COLS)
  40. #define QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE (QWIIC_KEYBOARD_ROWS)
  41. void qwiic_keyboard_write_keymap(uint8_t * pointer);
  42. void qwiic_keyboard_read_keymap(uint8_t * pointer, uint8_t index);
  43. bool qwiic_keyboard_master = false;
  44. bool qwiic_keyboard_connected[QWIIC_KEYBOARD_MAX_DEVICES] = { false };
  45. uint8_t qwiic_keyboard_keymap_message[QWIIC_KEYBOARD_KEYMAP_MESSAGE_SIZE] = {0};
  46. uint8_t qwiic_keyboard_matrix_message[QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE] = {0};
  47. twi2c_message_received qwiic_keyboard_message_received_ptr = qwiic_keyboard_message_received;
  48. float song_one_up[][2] = SONG(ONE_UP_SOUND);
  49. uint8_t first_message = 0;
  50. uint16_t qwiic_keyboard_keymap[QWIIC_KEYBOARD_MAX_DEVICES][QWIIC_KEYBOARD_LAYERS][QWIIC_KEYBOARD_ROWS][QWIIC_KEYBOARD_COLS] = {0};
  51. bool qwiic_keyboard_keymap_initialised[QWIIC_KEYBOARD_MAX_DEVICES] = { false };
  52. uint8_t qwiic_keyboard_listening_address[QWIIC_KEYBOARD_MAX_DEVICES] = { 0 };
  53. #define QWIIC_KEYBOARD_NOT_PROCESSING 255
  54. uint8_t qwiic_keyboard_processing_slave = QWIIC_KEYBOARD_NOT_PROCESSING;
  55. void qwiic_keyboard_init(void) {
  56. twi2c_init();
  57. twi2c_start();
  58. twi2c_start_listening(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS, qwiic_keyboard_message_received_ptr);
  59. }
  60. void qwiic_keyboard_set_master(void) {
  61. twi2c_stop();
  62. twi2c_start();
  63. qwiic_keyboard_master = true;
  64. }
  65. static uint8_t keymap_command[1] = { 0x01 };
  66. static uint8_t matrix_command[1] = { 0x02 };
  67. static uint8_t look_counter = 0;
  68. // uint8_t get_available_device_index(void) {
  69. // for (uint8_t i = 0; i < QWIIC_KEYBOARD_MAX_DEVICES; i++) {
  70. // if (!qwiic_keyboard_connected[i])
  71. // return i;
  72. // }
  73. // return 255;
  74. // }
  75. void qwiic_keyboard_get_matrix(uint8_t device_i) {
  76. msg_t ret = twi2c_transmit_receive(qwiic_keyboard_listening_address[device_i],
  77. matrix_command, 1,
  78. qwiic_keyboard_matrix_message, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE
  79. );
  80. switch (ret) {
  81. case I2C_OK:
  82. SEND_STRING("1");
  83. // majority of this is pulled from keyboard.c:keyboard_task()
  84. static qwiic_row_t matrix_prev[QWIIC_KEYBOARD_ROWS];
  85. qwiic_row_t matrix_row = 0;
  86. qwiic_row_t matrix_change = 0;
  87. #ifdef QMK_KEYS_PER_SCAN
  88. uint8_t keys_processed = 0;
  89. #endif
  90. qwiic_keyboard_processing_slave = device_i;
  91. for (uint8_t r = 0; r < QWIIC_KEYBOARD_ROWS; r++) {
  92. matrix_row = qwiic_keyboard_matrix_message[r];
  93. matrix_change = matrix_row ^ matrix_prev[r];
  94. if (matrix_change) {
  95. for (uint8_t c = 0; c < QWIIC_KEYBOARD_COLS; c++) {
  96. if (matrix_change & ((qwiic_row_t)1<<c)) {
  97. action_exec((keyevent_t){
  98. .key = (keypos_t){ .row = r, .col = c },
  99. .pressed = (matrix_row & ((qwiic_row_t)1<<c)),
  100. .time = (timer_read() | 1) /* time should not be 0 */
  101. });
  102. // record a processed key
  103. matrix_prev[r] ^= ((qwiic_row_t)1<<c);
  104. #ifdef QMK_KEYS_PER_SCAN
  105. // only jump out if we have processed "enough" keys.
  106. if (++keys_processed >= QMK_KEYS_PER_SCAN)
  107. #endif
  108. // process a key per task call
  109. goto QWIIC_MATRIX_LOOP_END;
  110. }
  111. }
  112. }
  113. }
  114. // call with pseudo tick event when no real key event.
  115. #ifdef QMK_KEYS_PER_SCAN
  116. // we can get here with some keys processed now.
  117. if (!keys_processed)
  118. #endif
  119. action_exec(TICK);
  120. QWIIC_MATRIX_LOOP_END:
  121. qwiic_keyboard_processing_slave = QWIIC_KEYBOARD_NOT_PROCESSING;
  122. SEND_STRING("2");
  123. // if (first_message == 100) {
  124. // PLAY_SONG(song_one_up);
  125. // }
  126. // first_message += 1;
  127. break;
  128. case I2C_ERROR:
  129. break;
  130. default:
  131. twi2c_start();
  132. break;
  133. }
  134. }
  135. void qwiic_keyboard_task(void) {
  136. if (USB_DRIVER.state == USB_ACTIVE)
  137. qwiic_keyboard_set_master();
  138. if (qwiic_keyboard_master) {
  139. for (uint8_t device_i = 0; device_i < QWIIC_KEYBOARD_MAX_DEVICES; device_i++) {
  140. if (qwiic_keyboard_connected[device_i]) {
  141. // send empty message, expecting matrix info
  142. // twi2c_transmit(qwiic_keyboard_listening_address[device_i], command, 1);
  143. // if (MSG_OK == twi2c_receive(qwiic_keyboard_listening_address[device_i],
  144. // qwiic_keyboard_matrix_message, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE
  145. // )) {
  146. if (!qwiic_keyboard_keymap_initialised[device_i]) {
  147. if (MSG_OK == twi2c_transmit_receive(qwiic_keyboard_listening_address[device_i],
  148. keymap_command, 1,
  149. qwiic_keyboard_keymap_message, QWIIC_KEYBOARD_KEYMAP_MESSAGE_SIZE
  150. )) {
  151. // load keymap into memory
  152. qwiic_keyboard_keymap_initialised[device_i] = true;
  153. qwiic_keyboard_read_keymap(qwiic_keyboard_keymap_message, device_i);
  154. wait_ms(10);
  155. qwiic_keyboard_get_matrix(device_i);
  156. SEND_STRING("3");
  157. }
  158. } else {
  159. qwiic_keyboard_get_matrix(device_i);
  160. } // end else - not init
  161. } else { // if not connected
  162. //if (look_counter == 0) {
  163. // send new address to listen on, expect back keymap
  164. uint8_t new_address = QWIIC_KEYBOARD_LISTENING_ADDRESS_START + (device_i*2);
  165. uint8_t address_copy = 0;
  166. // twi2c_transmit(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS, &new_address, 1);
  167. // if (MSG_OK == twi2c_receive(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS,
  168. // &address_copy, 1
  169. // )) {
  170. if (MSG_OK == twi2c_transmit_receive(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS,
  171. &new_address, 1,
  172. &address_copy, 1
  173. )) {
  174. send_byte(new_address);
  175. //if (address_copy == new_address) {
  176. PLAY_SONG(song_one_up);
  177. qwiic_keyboard_connected[device_i] = true;
  178. qwiic_keyboard_listening_address[device_i] = new_address;
  179. //}
  180. }
  181. //} // end if - look for new
  182. } // end else - connected
  183. } // end for - devices
  184. look_counter = ((look_counter + 1) % 150);
  185. } // end if - master
  186. }
  187. void qwiic_keyboard_message_received(I2CDriver *i2cp, uint8_t * body, uint16_t size) {
  188. switch (body[0]) {
  189. // send keymap
  190. case 0x01:
  191. qwiic_keyboard_write_keymap(qwiic_keyboard_keymap_message);
  192. twi2c_reply(i2cp, qwiic_keyboard_keymap_message, QWIIC_KEYBOARD_KEYMAP_MESSAGE_SIZE);
  193. break;
  194. // send matrix
  195. case 0x02:
  196. case 0x00:
  197. if (first_message == 0) {
  198. PLAY_SONG(song_one_up);
  199. }
  200. first_message += 1;
  201. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  202. if (row < MATRIX_ROWS) {
  203. qwiic_keyboard_matrix_message[row] = matrix_get_row(row);
  204. } else {
  205. qwiic_keyboard_matrix_message[row] = 0;
  206. }
  207. }
  208. twi2c_reply(i2cp, qwiic_keyboard_matrix_message, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE);
  209. break;
  210. default:
  211. twi2c_remove_listening(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS);
  212. qwiic_keyboard_connected[0] = true;
  213. //qwiic_keyboard_master = false;
  214. qwiic_keyboard_listening_address[0] = QWIIC_KEYBOARD_LISTENING_ADDRESS_START;
  215. twi2c_reply(i2cp, qwiic_keyboard_listening_address, 1);
  216. twi2c_add_listening(qwiic_keyboard_listening_address[0]);
  217. break;
  218. }
  219. }
  220. // qwiic_keyboard_message_received_ptr = qwiic_keyboard_message_received;
  221. __attribute__((optimize("O0")))
  222. void qwiic_keyboard_write_keymap(uint8_t * pointer) {
  223. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  224. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  225. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  226. uint16_t keycode = 0;
  227. keycode = pgm_read_word(&keymaps[layer][row][col]);
  228. *pointer++ = (keycode >> 8);
  229. *pointer++ = (keycode & 0xFF);
  230. }
  231. }
  232. }
  233. }
  234. void qwiic_keyboard_read_keymap(uint8_t * pointer, uint8_t index) {
  235. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  236. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  237. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  238. uint16_t keycode = ((*pointer++) << 8);
  239. keycode |= (*pointer++);
  240. qwiic_keyboard_keymap[index][layer][row][col] = keycode;
  241. }
  242. }
  243. }
  244. }
  245. // overwrite the built-in function - slaves don't need to process keycodes
  246. bool is_keyboard_master(void) {
  247. return qwiic_keyboard_master;
  248. }
  249. // overwrite the built-in function
  250. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
  251. if (qwiic_keyboard_processing_slave == QWIIC_KEYBOARD_NOT_PROCESSING) {
  252. // Read entire word (16bits)
  253. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  254. } else {
  255. // trick the built-in handling to accept our replacement keymap
  256. return qwiic_keyboard_keymap[qwiic_keyboard_processing_slave][(layer)][(key.row)][(key.col)];
  257. }
  258. }