ble.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_extra(report_extra_t *report);
  84. host_driver_t bluefruit_driver = {
  85. keyboard_leds,
  86. send_keyboard,
  87. send_mouse,
  88. send_extra
  89. };
  90. host_driver_t null_driver = {};
  91. static uint8_t keyboard_leds(void)
  92. {
  93. return bluefruit_keyboard_leds;
  94. }
  95. static void send_keyboard(report_keyboard_t *report)
  96. {
  97. #ifdef BLUEFRUIT_TRACE_SERIAL
  98. bluefruit_trace_header();
  99. #endif
  100. dprintf("Sending...\n");
  101. send_str(PSTR("AT+BLEKEYBOARDCODE="));
  102. for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++)
  103. {
  104. send_bytes(report->raw[i]);
  105. if (i < (KEYBOARD_EPSIZE - 1))
  106. {
  107. send_str(PSTR("-"));
  108. }
  109. }
  110. send_str(PSTR("\r\n"));
  111. #ifdef BLUEFRUIT_TRACE_SERIAL
  112. bluefruit_trace_footer();
  113. #endif
  114. }
  115. static void send_mouse(report_mouse_t *report)
  116. {
  117. #ifdef BLUEFRUIT_TRACE_SERIAL
  118. bluefruit_trace_header();
  119. #endif
  120. bluefruit_serial_send(0xFD);
  121. bluefruit_serial_send(0x00);
  122. bluefruit_serial_send(0x03);
  123. bluefruit_serial_send(report->buttons);
  124. bluefruit_serial_send(report->x);
  125. bluefruit_serial_send(report->y);
  126. bluefruit_serial_send(report->v); // should try sending the wheel v here
  127. bluefruit_serial_send(report->h); // should try sending the wheel h here
  128. bluefruit_serial_send(0x00);
  129. #ifdef BLUEFRUIT_TRACE_SERIAL
  130. bluefruit_trace_footer();
  131. #endif
  132. }
  133. /*
  134. +-----------------+-------------------+-------+
  135. | Consumer Key | Bit Map | Hex |
  136. +-----------------+-------------------+-------+
  137. | Home | 00000001 00000000 | 01 00 |
  138. | KeyboardLayout | 00000010 00000000 | 02 00 |
  139. | Search | 00000100 00000000 | 04 00 |
  140. | Snapshot | 00001000 00000000 | 08 00 |
  141. | VolumeUp | 00010000 00000000 | 10 00 |
  142. | VolumeDown | 00100000 00000000 | 20 00 |
  143. | Play/Pause | 01000000 00000000 | 40 00 |
  144. | Fast Forward | 10000000 00000000 | 80 00 |
  145. | Rewind | 00000000 00000001 | 00 01 |
  146. | Scan Next Track | 00000000 00000010 | 00 02 |
  147. | Scan Prev Track | 00000000 00000100 | 00 04 |
  148. | Random Play | 00000000 00001000 | 00 08 |
  149. | Stop | 00000000 00010000 | 00 10 |
  150. +-------------------------------------+-------+
  151. */
  152. #define CONSUMER2BLUEFRUIT(usage) \
  153. (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)))))))))))))))))))
  154. static void send_extra(report_extra_t *report)
  155. {
  156. if (report->report_id == REPORT_ID_CONSUMER) {
  157. uint16_t bitmap = CONSUMER2BLUEFRUIT(report->usage);
  158. #ifdef BLUEFRUIT_TRACE_SERIAL
  159. dprintf("\nData: ");
  160. debug_hex16(data);
  161. dprintf("; bitmap: ");
  162. debug_hex16(bitmap);
  163. dprintf("\n");
  164. bluefruit_trace_header();
  165. #endif
  166. send_str(PSTR("AT+BLEHIDCONTROLKEY=0x"));
  167. send_bytes((bitmap >> 8) & 0xFF);
  168. send_bytes(bitmap & 0xFF);
  169. send_str(PSTR("\r\n"));
  170. #ifdef BLUEFRUIT_TRACE_SERIAL
  171. bluefruit_trace_footer();
  172. #endif
  173. }
  174. }
  175. void usart_init(void)
  176. {
  177. uart1_init(UART_BAUD_SELECT_DOUBLE_SPEED(76800, 8000000L));
  178. wait_ms(250);
  179. send_str(PSTR("\r\n"));
  180. send_str(PSTR("\r\n"));
  181. send_str(PSTR("\r\n"));
  182. }