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.

219 lines
6.0 KiB

  1. /*
  2. Copyright 2019 Basic I/O Instruments(Scott Wei) <scot.wei@gmail.com>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/pgmspace.h>
  15. #include "report.h"
  16. #include "host.h"
  17. #include "host_driver.h"
  18. #include "keyboard.h"
  19. #include "action.h"
  20. #include "led.h"
  21. #include "sendchar.h"
  22. #include "debug.h"
  23. #ifdef SLEEP_LED_ENABLE
  24. #include "sleep_led.h"
  25. #endif
  26. #include "suspend.h"
  27. #include "usb_descriptor.h"
  28. #include "lufa.h"
  29. #include "quantum.h"
  30. #include <util/atomic.h>
  31. #include "print.h"
  32. #include "ble.h"
  33. #include "usart.h"
  34. keyboard_config_t ble_config;
  35. static uint8_t bluefruit_keyboard_leds = 0;
  36. static void bluefruit_serial_send(uint8_t);
  37. void send_str(const char *str)
  38. {
  39. uint8_t c;
  40. while ((c = pgm_read_byte(str++)))
  41. uart1_putc(c);
  42. }
  43. void serial_send(uint8_t data)
  44. {
  45. dprintf("Sending: %u\n", data);
  46. }
  47. void send_bytes(uint8_t data)
  48. {
  49. char hexStr[3];
  50. sprintf(hexStr, "%02X", data);
  51. for (int j = 0; j < sizeof(hexStr) - 1; j++)
  52. {
  53. uart1_putc(hexStr[j]);
  54. }
  55. }
  56. #ifdef BLUEFRUIT_TRACE_SERIAL
  57. static void bluefruit_trace_header(void)
  58. {
  59. dprintf("+------------------------------------+\n");
  60. dprintf("| HID report to Bluefruit via serial |\n");
  61. dprintf("+------------------------------------+\n|");
  62. }
  63. static void bluefruit_trace_footer(void)
  64. {
  65. dprintf("|\n+------------------------------------+\n\n");
  66. }
  67. #endif
  68. static void bluefruit_serial_send(uint8_t data)
  69. {
  70. #ifdef BLUEFRUIT_TRACE_SERIAL
  71. dprintf(" ");
  72. debug_hex8(data);
  73. dprintf(" ");
  74. #endif
  75. serial_send(data);
  76. }
  77. /*------------------------------------------------------------------*
  78. * Host driver
  79. *------------------------------------------------------------------*/
  80. static uint8_t keyboard_leds(void);
  81. static void send_keyboard(report_keyboard_t *report);
  82. static void send_mouse(report_mouse_t *report);
  83. static void send_system(uint16_t data);
  84. static void send_consumer(uint16_t data);
  85. host_driver_t bluefruit_driver = {
  86. keyboard_leds,
  87. send_keyboard,
  88. send_mouse,
  89. send_system,
  90. send_consumer};
  91. host_driver_t null_driver = {};
  92. static uint8_t keyboard_leds(void)
  93. {
  94. return bluefruit_keyboard_leds;
  95. }
  96. static void send_keyboard(report_keyboard_t *report)
  97. {
  98. #ifdef BLUEFRUIT_TRACE_SERIAL
  99. bluefruit_trace_header();
  100. #endif
  101. dprintf("Sending...\n");
  102. send_str(PSTR("AT+BLEKEYBOARDCODE="));
  103. for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++)
  104. {
  105. send_bytes(report->raw[i]);
  106. if (i < (KEYBOARD_EPSIZE - 1))
  107. {
  108. send_str(PSTR("-"));
  109. }
  110. }
  111. send_str(PSTR("\r\n"));
  112. #ifdef BLUEFRUIT_TRACE_SERIAL
  113. bluefruit_trace_footer();
  114. #endif
  115. }
  116. static void send_mouse(report_mouse_t *report)
  117. {
  118. #ifdef BLUEFRUIT_TRACE_SERIAL
  119. bluefruit_trace_header();
  120. #endif
  121. bluefruit_serial_send(0xFD);
  122. bluefruit_serial_send(0x00);
  123. bluefruit_serial_send(0x03);
  124. bluefruit_serial_send(report->buttons);
  125. bluefruit_serial_send(report->x);
  126. bluefruit_serial_send(report->y);
  127. bluefruit_serial_send(report->v); // should try sending the wheel v here
  128. bluefruit_serial_send(report->h); // should try sending the wheel h here
  129. bluefruit_serial_send(0x00);
  130. #ifdef BLUEFRUIT_TRACE_SERIAL
  131. bluefruit_trace_footer();
  132. #endif
  133. }
  134. static void send_system(uint16_t data)
  135. {
  136. }
  137. /*
  138. +-----------------+-------------------+-------+
  139. | Consumer Key | Bit Map | Hex |
  140. +-----------------+-------------------+-------+
  141. | Home | 00000001 00000000 | 01 00 |
  142. | KeyboardLayout | 00000010 00000000 | 02 00 |
  143. | Search | 00000100 00000000 | 04 00 |
  144. | Snapshot | 00001000 00000000 | 08 00 |
  145. | VolumeUp | 00010000 00000000 | 10 00 |
  146. | VolumeDown | 00100000 00000000 | 20 00 |
  147. | Play/Pause | 01000000 00000000 | 40 00 |
  148. | Fast Forward | 10000000 00000000 | 80 00 |
  149. | Rewind | 00000000 00000001 | 00 01 |
  150. | Scan Next Track | 00000000 00000010 | 00 02 |
  151. | Scan Prev Track | 00000000 00000100 | 00 04 |
  152. | Random Play | 00000000 00001000 | 00 08 |
  153. | Stop | 00000000 00010000 | 00 10 |
  154. +-------------------------------------+-------+
  155. */
  156. #define CONSUMER2BLUEFRUIT(usage) \
  157. (usage == AUDIO_MUTE ? 0x00e2 : (usage == AUDIO_VOL_UP ? 0x00e9 : (usage == AUDIO_VOL_DOWN ? 0x00ea : (usage == TRANSPORT_NEXT_TRACK ? 0x00b5 : (usage == TRANSPORT_PREV_TRACK ? 0x00b6 : (usage == TRANSPORT_STOP ? 0x00b7 : (usage == TRANSPORT_STOP_EJECT ? 0x00b8 : (usage == TRANSPORT_PLAY_PAUSE ? 0x00b1 : (usage == AL_CC_CONFIG ? 0x0183 : (usage == AL_EMAIL ? 0x018c : (usage == AL_CALCULATOR ? 0x0192 : (usage == AL_LOCAL_BROWSER ? 0x0196 : (usage == AC_SEARCH ? 0x021f : (usage == AC_HOME ? 0x0223 : (usage == AC_BACK ? 0x0224 : (usage == AC_FORWARD ? 0x0225 : (usage == AC_STOP ? 0x0226 : (usage == AC_REFRESH ? 0x0227 : (usage == AC_BOOKMARKS ? 0x022a : 0)))))))))))))))))))
  158. static void send_consumer(uint16_t data)
  159. {
  160. static uint16_t last_data = 0;
  161. if (data == last_data)
  162. return;
  163. last_data = data;
  164. uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
  165. #ifdef BLUEFRUIT_TRACE_SERIAL
  166. dprintf("\nData: ");
  167. debug_hex16(data);
  168. dprintf("; bitmap: ");
  169. debug_hex16(bitmap);
  170. dprintf("\n");
  171. bluefruit_trace_header();
  172. #endif
  173. send_str(PSTR("AT+BLEHIDCONTROLKEY=0x"));
  174. send_bytes((bitmap >> 8) & 0xFF);
  175. send_bytes(bitmap & 0xFF);
  176. send_str(PSTR("\r\n"));
  177. #ifdef BLUEFRUIT_TRACE_SERIAL
  178. bluefruit_trace_footer();
  179. #endif
  180. }
  181. void usart_init(void)
  182. {
  183. uart1_init(UART_BAUD_SELECT_DOUBLE_SPEED(76800, 8000000L));
  184. wait_ms(250);
  185. send_str(PSTR("\r\n"));
  186. send_str(PSTR("\r\n"));
  187. send_str(PSTR("\r\n"));
  188. }