logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

bluetooth_custom.c (4964B)


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