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.

178 lines
4.8 KiB

  1. /* Copyright 2021 Jay Greco
  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. /*
  17. Remote keyboard is an experimental feature that allows for connecting another
  18. keyboard, macropad, numpad, or accessory without requiring an additional USB connection.
  19. The "remote keyboard" forwards its keystrokes using UART serial over TRRS. Dynamic VUSB
  20. detect allows the keyboard automatically switch to host or remote mode depending on
  21. which is connected to the USB port.
  22. Possible functionality includes the ability to send data from the host to the remote using
  23. a reverse link, allowing for LED sync, configuration, and more data sharing between devices.
  24. This will require a new communication protocol, as the current one is limited.
  25. */
  26. #include "remote_kb.h"
  27. #include "uart.h"
  28. uint8_t
  29. msg[UART_MSG_LEN],
  30. msg_idx = 0;
  31. bool
  32. is_host = true;
  33. // Private functions
  34. static bool vbus_detect(void) {
  35. #if defined(__AVR_ATmega32U4__)
  36. //returns true if VBUS is present, false otherwise.
  37. USBCON |= (1 << OTGPADE); //enables VBUS pad
  38. _delay_us(10);
  39. return (USBSTA & (1<<VBUS)); //checks state of VBUS
  40. #else
  41. #error vbus_detect is not implemented for this architecure!
  42. #endif
  43. }
  44. static uint8_t chksum8(const unsigned char *buf, size_t len) {
  45. unsigned int sum;
  46. for (sum = 0 ; len != 0 ; len--)
  47. sum += *(buf++);
  48. return (uint8_t)sum;
  49. }
  50. static void send_msg(uint16_t keycode, bool pressed) {
  51. msg[IDX_PREAMBLE] = UART_PREAMBLE;
  52. msg[IDX_KCLSB] = (keycode & 0xFF);
  53. msg[IDX_KCMSB] = (keycode >> 8) & 0xFF;
  54. msg[IDX_PRESSED] = pressed;
  55. msg[IDX_CHECKSUM] = chksum8(msg, UART_MSG_LEN-1);
  56. for (int i=0; i<UART_MSG_LEN; i++) {
  57. uart_putchar(msg[i]);
  58. }
  59. }
  60. static void print_message_buffer(void) {
  61. for (int i=0; i<UART_MSG_LEN; i++) {
  62. dprintf("msg[%u]: %u\n", i, msg[i]);
  63. }
  64. }
  65. static void process_uart(void) {
  66. uint8_t chksum = chksum8(msg, UART_MSG_LEN-1);
  67. if (msg[IDX_PREAMBLE] != UART_PREAMBLE || msg[IDX_CHECKSUM] != chksum) {
  68. dprintf("UART checksum mismatch!\n");
  69. print_message_buffer();
  70. dprintf("calc checksum: %u\n", chksum);
  71. } else {
  72. uint16_t keycode = (uint16_t)msg[IDX_KCLSB] | ((uint16_t)msg[IDX_KCMSB] << 8);
  73. bool pressed = (bool)msg[IDX_PRESSED];
  74. if (IS_RM_KC(keycode)) {
  75. keyrecord_t record;
  76. record.event.pressed = pressed;
  77. if (pressed) dprintf("Remote macro: press [%u]\n", keycode);
  78. else dprintf("Remote macro: release [%u]\n", keycode);
  79. process_record_user(keycode, &record);
  80. } else {
  81. if (pressed) {
  82. dprintf("Remote: press [%u]\n", keycode);
  83. register_code(keycode);
  84. } else {
  85. dprintf("Remote: release [%u]\n", keycode);
  86. unregister_code(keycode);
  87. }
  88. }
  89. }
  90. }
  91. static void get_msg(void) {
  92. while (uart_available()) {
  93. msg[msg_idx] = uart_getchar();
  94. dprintf("idx: %u, recv: %u\n", msg_idx, msg[msg_idx]);
  95. if (msg_idx == 0 && (msg[msg_idx] != UART_PREAMBLE)) {
  96. dprintf("Byte sync error!\n");
  97. msg_idx = 0;
  98. } else if (msg_idx == (UART_MSG_LEN-1)) {
  99. process_uart();
  100. msg_idx = 0;
  101. } else {
  102. msg_idx++;
  103. }
  104. }
  105. }
  106. static void handle_host_incoming(void) {
  107. get_msg();
  108. }
  109. static void handle_host_outgoing(void) {
  110. // for future reverse link use
  111. }
  112. static void handle_remote_incoming(void) {
  113. // for future reverse link use
  114. }
  115. static void handle_remote_outgoing(uint16_t keycode, keyrecord_t *record) {
  116. if (IS_HID_KC(keycode) || IS_RM_KC(keycode)) {
  117. dprintf("Remote: send [%u]\n", keycode);
  118. send_msg(keycode, record->event.pressed);
  119. }
  120. }
  121. // Public functions
  122. void matrix_init_remote_kb(void) {
  123. uart_init(SERIAL_UART_BAUD);
  124. is_host = vbus_detect();
  125. }
  126. void process_record_remote_kb(uint16_t keycode, keyrecord_t *record) {
  127. #if defined (KEYBOARD_HOST)
  128. handle_host_outgoing();
  129. #elif defined(KEYBOARD_REMOTE)
  130. handle_remote_outgoing(keycode, record);
  131. #else //auto check with VBUS
  132. if (is_host) {
  133. handle_host_outgoing();
  134. }
  135. else {
  136. handle_remote_outgoing(keycode, record);
  137. }
  138. #endif
  139. }
  140. void matrix_scan_remote_kb(void) {
  141. #if defined(KEYBOARD_HOST)
  142. handle_host_incoming();
  143. #elif defined (KEYBOARD_REMOTE)
  144. handle_remote_incoming();
  145. #else //auto check with VBUS
  146. if (is_host) {
  147. handle_host_incoming();
  148. }
  149. else {
  150. handle_remote_incoming();
  151. }
  152. #endif
  153. }