ble.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "outputselect.h"
  32. #include "print.h"
  33. #include "ble.h"
  34. #include "usart.h"
  35. keyboard_config_t ble_config;
  36. static uint8_t bluefruit_keyboard_leds = 0;
  37. static void bluefruit_serial_send(uint8_t);
  38. void send_str(const char *str)
  39. {
  40. uint8_t c;
  41. while ((c = pgm_read_byte(str++)))
  42. uart1_putc(c);
  43. }
  44. void serial_send(uint8_t data)
  45. {
  46. dprintf("Sending: %u\n", data);
  47. }
  48. void send_bytes(uint8_t data)
  49. {
  50. char hexStr[3];
  51. sprintf(hexStr, "%02X", data);
  52. for (int j = 0; j < sizeof(hexStr) - 1; j++)
  53. {
  54. uart1_putc(hexStr[j]);
  55. }
  56. }
  57. #ifdef BLUEFRUIT_TRACE_SERIAL
  58. static void bluefruit_trace_header(void)
  59. {
  60. dprintf("+------------------------------------+\n");
  61. dprintf("| HID report to Bluefruit via serial |\n");
  62. dprintf("+------------------------------------+\n|");
  63. }
  64. static void bluefruit_trace_footer(void)
  65. {
  66. dprintf("|\n+------------------------------------+\n\n");
  67. }
  68. #endif
  69. static void bluefruit_serial_send(uint8_t data)
  70. {
  71. #ifdef BLUEFRUIT_TRACE_SERIAL
  72. dprintf(" ");
  73. debug_hex8(data);
  74. dprintf(" ");
  75. #endif
  76. serial_send(data);
  77. }
  78. /*------------------------------------------------------------------*
  79. * Host driver
  80. *------------------------------------------------------------------*/
  81. static uint8_t keyboard_leds(void);
  82. static void send_keyboard(report_keyboard_t *report);
  83. static void send_mouse(report_mouse_t *report);
  84. static void send_system(uint16_t data);
  85. static void send_consumer(uint16_t data);
  86. host_driver_t bluefruit_driver = {
  87. keyboard_leds,
  88. send_keyboard,
  89. send_mouse,
  90. send_system,
  91. send_consumer};
  92. host_driver_t null_driver = {};
  93. static uint8_t keyboard_leds(void)
  94. {
  95. return bluefruit_keyboard_leds;
  96. }
  97. static void send_keyboard(report_keyboard_t *report)
  98. {
  99. #ifdef BLUEFRUIT_TRACE_SERIAL
  100. bluefruit_trace_header();
  101. #endif
  102. dprintf("Sending...\n");
  103. send_str(PSTR("AT+BLEKEYBOARDCODE="));
  104. for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++)
  105. {
  106. send_bytes(report->raw[i]);
  107. if (i < (KEYBOARD_EPSIZE - 1))
  108. {
  109. send_str(PSTR("-"));
  110. }
  111. }
  112. send_str(PSTR("\r\n"));
  113. #ifdef BLUEFRUIT_TRACE_SERIAL
  114. bluefruit_trace_footer();
  115. #endif
  116. }
  117. static void send_mouse(report_mouse_t *report)
  118. {
  119. #ifdef BLUEFRUIT_TRACE_SERIAL
  120. bluefruit_trace_header();
  121. #endif
  122. bluefruit_serial_send(0xFD);
  123. bluefruit_serial_send(0x00);
  124. bluefruit_serial_send(0x03);
  125. bluefruit_serial_send(report->buttons);
  126. bluefruit_serial_send(report->x);
  127. bluefruit_serial_send(report->y);
  128. bluefruit_serial_send(report->v); // should try sending the wheel v here
  129. bluefruit_serial_send(report->h); // should try sending the wheel h here
  130. bluefruit_serial_send(0x00);
  131. #ifdef BLUEFRUIT_TRACE_SERIAL
  132. bluefruit_trace_footer();
  133. #endif
  134. }
  135. static void send_system(uint16_t data)
  136. {
  137. }
  138. /*
  139. +-----------------+-------------------+-------+
  140. | Consumer Key | Bit Map | Hex |
  141. +-----------------+-------------------+-------+
  142. | Home | 00000001 00000000 | 01 00 |
  143. | KeyboardLayout | 00000010 00000000 | 02 00 |
  144. | Search | 00000100 00000000 | 04 00 |
  145. | Snapshot | 00001000 00000000 | 08 00 |
  146. | VolumeUp | 00010000 00000000 | 10 00 |
  147. | VolumeDown | 00100000 00000000 | 20 00 |
  148. | Play/Pause | 01000000 00000000 | 40 00 |
  149. | Fast Forward | 10000000 00000000 | 80 00 |
  150. | Rewind | 00000000 00000001 | 00 01 |
  151. | Scan Next Track | 00000000 00000010 | 00 02 |
  152. | Scan Prev Track | 00000000 00000100 | 00 04 |
  153. | Random Play | 00000000 00001000 | 00 08 |
  154. | Stop | 00000000 00010000 | 00 10 |
  155. +-------------------------------------+-------+
  156. */
  157. #define CONSUMER2BLUEFRUIT(usage) \
  158. (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)))))))))))))))))))
  159. static void send_consumer(uint16_t data)
  160. {
  161. static uint16_t last_data = 0;
  162. if (data == last_data)
  163. return;
  164. last_data = data;
  165. uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
  166. #ifdef BLUEFRUIT_TRACE_SERIAL
  167. dprintf("\nData: ");
  168. debug_hex16(data);
  169. dprintf("; bitmap: ");
  170. debug_hex16(bitmap);
  171. dprintf("\n");
  172. bluefruit_trace_header();
  173. #endif
  174. send_str(PSTR("AT+BLEHIDCONTROLKEY=0x"));
  175. send_bytes((bitmap >> 8) & 0xFF);
  176. send_bytes(bitmap & 0xFF);
  177. send_str(PSTR("\r\n"));
  178. #ifdef BLUEFRUIT_TRACE_SERIAL
  179. bluefruit_trace_footer();
  180. #endif
  181. }
  182. void usart_init(void)
  183. {
  184. uart1_init(UART_BAUD_SELECT_DOUBLE_SPEED(76800, 8000000L));
  185. wait_ms(250);
  186. send_str(PSTR("\r\n"));
  187. send_str(PSTR("\r\n"));
  188. send_str(PSTR("\r\n"));
  189. }