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.

174 lines
5.1 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 "bluetooth.h"
  15. #include "ble.h"
  16. #include "usart.h"
  17. #include "progmem.h"
  18. #include "wait.h"
  19. #include "debug.h"
  20. #include "usb_descriptor.h"
  21. #include "report.h"
  22. keyboard_config_t ble_config;
  23. static void bluefruit_serial_send(uint8_t);
  24. void send_str(const char *str)
  25. {
  26. uint8_t c;
  27. while ((c = pgm_read_byte(str++)))
  28. uart1_putc(c);
  29. }
  30. void serial_send(uint8_t data)
  31. {
  32. dprintf("Sending: %u\n", data);
  33. }
  34. void send_bytes(uint8_t data)
  35. {
  36. char hexStr[3];
  37. sprintf(hexStr, "%02X", data);
  38. for (int j = 0; j < sizeof(hexStr) - 1; j++)
  39. {
  40. uart1_putc(hexStr[j]);
  41. }
  42. }
  43. #ifdef BLUEFRUIT_TRACE_SERIAL
  44. static void bluefruit_trace_header(void)
  45. {
  46. dprintf("+------------------------------------+\n");
  47. dprintf("| HID report to Bluefruit via serial |\n");
  48. dprintf("+------------------------------------+\n|");
  49. }
  50. static void bluefruit_trace_footer(void)
  51. {
  52. dprintf("|\n+------------------------------------+\n\n");
  53. }
  54. #endif
  55. static void bluefruit_serial_send(uint8_t data)
  56. {
  57. #ifdef BLUEFRUIT_TRACE_SERIAL
  58. dprintf(" ");
  59. debug_hex8(data);
  60. dprintf(" ");
  61. #endif
  62. serial_send(data);
  63. }
  64. void bluetooth_init(void) {
  65. usart_init();
  66. }
  67. void bluetooth_task(void) {}
  68. void bluetooth_send_keyboard(report_keyboard_t *report)
  69. {
  70. #ifdef BLUEFRUIT_TRACE_SERIAL
  71. bluefruit_trace_header();
  72. #endif
  73. dprintf("Sending...\n");
  74. send_str(PSTR("AT+BLEKEYBOARDCODE="));
  75. for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++)
  76. {
  77. send_bytes(report->raw[i]);
  78. if (i < (KEYBOARD_EPSIZE - 1))
  79. {
  80. send_str(PSTR("-"));
  81. }
  82. }
  83. send_str(PSTR("\r\n"));
  84. #ifdef BLUEFRUIT_TRACE_SERIAL
  85. bluefruit_trace_footer();
  86. #endif
  87. }
  88. void bluetooth_send_mouse(report_mouse_t *report)
  89. {
  90. #ifdef BLUEFRUIT_TRACE_SERIAL
  91. bluefruit_trace_header();
  92. #endif
  93. bluefruit_serial_send(0xFD);
  94. bluefruit_serial_send(0x00);
  95. bluefruit_serial_send(0x03);
  96. bluefruit_serial_send(report->buttons);
  97. bluefruit_serial_send(report->x);
  98. bluefruit_serial_send(report->y);
  99. bluefruit_serial_send(report->v); // should try sending the wheel v here
  100. bluefruit_serial_send(report->h); // should try sending the wheel h here
  101. bluefruit_serial_send(0x00);
  102. #ifdef BLUEFRUIT_TRACE_SERIAL
  103. bluefruit_trace_footer();
  104. #endif
  105. }
  106. /*
  107. +-----------------+-------------------+-------+
  108. | Consumer Key | Bit Map | Hex |
  109. +-----------------+-------------------+-------+
  110. | Home | 00000001 00000000 | 01 00 |
  111. | KeyboardLayout | 00000010 00000000 | 02 00 |
  112. | Search | 00000100 00000000 | 04 00 |
  113. | Snapshot | 00001000 00000000 | 08 00 |
  114. | VolumeUp | 00010000 00000000 | 10 00 |
  115. | VolumeDown | 00100000 00000000 | 20 00 |
  116. | Play/Pause | 01000000 00000000 | 40 00 |
  117. | Fast Forward | 10000000 00000000 | 80 00 |
  118. | Rewind | 00000000 00000001 | 00 01 |
  119. | Scan Next Track | 00000000 00000010 | 00 02 |
  120. | Scan Prev Track | 00000000 00000100 | 00 04 |
  121. | Random Play | 00000000 00001000 | 00 08 |
  122. | Stop | 00000000 00010000 | 00 10 |
  123. +-------------------------------------+-------+
  124. */
  125. #define CONSUMER2BLUEFRUIT(usage) \
  126. (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)))))))))))))))))))
  127. void bluetooth_send_consumer(uint16_t usage)
  128. {
  129. uint16_t bitmap = CONSUMER2BLUEFRUIT(usage);
  130. #ifdef BLUEFRUIT_TRACE_SERIAL
  131. dprintf("\nData: ");
  132. debug_hex16(data);
  133. dprintf("; bitmap: ");
  134. debug_hex16(bitmap);
  135. dprintf("\n");
  136. bluefruit_trace_header();
  137. #endif
  138. send_str(PSTR("AT+BLEHIDCONTROLKEY=0x"));
  139. send_bytes((bitmap >> 8) & 0xFF);
  140. send_bytes(bitmap & 0xFF);
  141. send_str(PSTR("\r\n"));
  142. #ifdef BLUEFRUIT_TRACE_SERIAL
  143. bluefruit_trace_footer();
  144. #endif
  145. }
  146. void usart_init(void)
  147. {
  148. uart1_init(UART_BAUD_SELECT_DOUBLE_SPEED(76800, 8000000L));
  149. wait_ms(250);
  150. send_str(PSTR("\r\n"));
  151. send_str(PSTR("\r\n"));
  152. send_str(PSTR("\r\n"));
  153. }