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.

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