qwiic_keyboard.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #define QWIIC_KEYBOARD_LAYERS 16
  20. #define QWIIC_KEYBOARD_ROWS 8
  21. #define QWIIC_KEYBOARD_COLS 8
  22. #define QWIIC_KEYBOARD_HANDSHAKE_ADDRESS 0b01010100
  23. #define QWIIC_KEYBOARD_LISTENING_ADDRESS_START 0b01010110
  24. #define QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE (2 + (QWIIC_KEYBOARD_LAYERS * QWIIC_KEYBOARD_ROWS * QWIIC_KEYBOARD_COLS))
  25. #define QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE MATRIX_ROWS
  26. bool qwiic_keyboard_master = false;
  27. bool qwiic_keyboard_connected = false;
  28. uint8_t * qwiic_keyboard_handshake_message = {0};
  29. uint8_t * qwiic_keyboard_matrix_message[QWIIC_KEYBOARD_ROWS] = {0};
  30. uint16_t qwiic_keyboard_keymap[QWIIC_KEYBOARD_LAYERS][QWIIC_KEYBOARD_ROWS][QWIIC_KEYBOARD_COLS] = {0};
  31. uint8_t qwiic_keyboard_new_listening_address = QWIIC_KEYBOARD_LISTENING_ADDRESS_START;
  32. uint8_t qwiic_keyboard_listening_address = QWIIC_KEYBOARD_LISTENING_ADDRESS_START;
  33. uint8_t qwiic_keyboard_matrix_rows;
  34. uint8_t qwiic_keyboard_matrix_cols;
  35. void qwiic_keyboard_setup(void) {
  36. twi2c_start_listening(qwiic_keyboard_listening_address, qwiic_keyboard_message_received);
  37. }
  38. void qwiic_keyboard_set_master() {
  39. twi2c_stop();
  40. qwiic_keyboard_master = true;
  41. }
  42. void qwiic_keyboard_task(void) {
  43. if (qwiic_keyboard_master) {
  44. if (qwiic_keyboard_connected) {
  45. if (MSG_OK == twi2c_transmit_receive(qwiic_keyboard_listening_address,
  46. NULL, 0,
  47. qwiic_keyboard_matrix_message, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE
  48. )) {
  49. // process key event
  50. } else {
  51. // disconnect
  52. }
  53. }
  54. if (MSG_OK == twi2c_transmit_receive(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS,
  55. qwiic_keyboard_new_listening_address, 1,
  56. qwiic_keyboard_handshake_message, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE
  57. )) {
  58. qwiic_keyboard_new_listening_address+=2;
  59. uint8_t * message_pointer = qwiic_keyboard_handshake_message;
  60. qwiic_keyboard_matrix_rows = *message_pointer++;
  61. qwiic_keyboard_matrix_cols = *message_pointer++;
  62. qwiic_keyboard_read_keymap(message_pointer);
  63. }
  64. }
  65. }
  66. twi2c_message_received qwiic_keyboard_message_received;
  67. extern matrix_row_t matrix[MATRIX_ROWS];
  68. uint8_t * qwiic_keyboard_reply;
  69. void qwiic_keyboard_message_received(uint8_t * body, uint16_t size) {
  70. if (qwiic_keyboard_connected) {
  71. memcpy(qwiic_keyboard_reply, matrix, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE);
  72. twi2c_reply(qwiic_keyboard_reply, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE);
  73. } else {
  74. qwiic_keyboard_connected = true;
  75. qwiic_keyboard_listening_address
  76. uint8_t * message_pointer = qwiic_keyboard_reply;
  77. *message_pointer++ = MATRIX_ROWS;
  78. *message_pointer++ = MATRIX_COLS;
  79. qwiic_keyboard_write_keymap(message_pointer);
  80. twi2c_reply(qwiic_keyboard_reply, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE);
  81. twi2c_stop();
  82. twi2c_start_listening(qwiic_keyboard_listening_address, qwiic_keyboard_message_received);
  83. }
  84. }
  85. void qwiic_keyboard_write_keymap(uint8_t * pointer) {
  86. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  87. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  88. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  89. uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
  90. *pointer++ = (keycode >> 8);
  91. *pointer++ = (keycode & 0xFF);
  92. }
  93. }
  94. }
  95. }
  96. void qwiic_keyboard_read_keymap(uint8_t * pointer) {
  97. for (uint8_t layer = 0; layer < QWIIC_KEYBOARD_LAYERS; layer++) {
  98. for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
  99. for (uint8_t col = 0; col < QWIIC_KEYBOARD_COLS; col++) {
  100. uint16_t keycode = *pointer++;
  101. keycode |= (*pointer++) << 8;
  102. qwiic_keyboard_keymap[layer][row][col] = keycode;
  103. }
  104. }
  105. }
  106. }