upload
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

bluefruit.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Bluefruit Protocol for TMK firmware
  3. Author: Benjamin Gould, 2013
  4. Based on code Copyright 2011 Jun Wako <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include "host.h"
  18. #include "report.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "host_driver.h"
  22. #include "serial.h"
  23. #include "bluefruit.h"
  24. #define BLUEFRUIT_TRACE_SERIAL 1
  25. static uint8_t bluefruit_keyboard_leds = 0;
  26. static void bluefruit_serial_send(uint8_t);
  27. void bluefruit_keyboard_print_report(report_keyboard_t *report)
  28. {
  29. if (!debug_keyboard) return;
  30. dprintf("keys: "); for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { debug_hex8(report->keys[i]); dprintf(" "); }
  31. dprintf(" mods: "); debug_hex8(report->mods);
  32. dprintf(" reserved: "); debug_hex8(report->reserved);
  33. dprintf("\n");
  34. }
  35. #ifdef BLUEFRUIT_TRACE_SERIAL
  36. static void bluefruit_trace_header()
  37. {
  38. dprintf("+------------------------------------+\n");
  39. dprintf("| HID report to Bluefruit via serial |\n");
  40. dprintf("+------------------------------------+\n|");
  41. }
  42. static void bluefruit_trace_footer()
  43. {
  44. dprintf("|\n+------------------------------------+\n\n");
  45. }
  46. #endif
  47. static void bluefruit_serial_send(uint8_t data)
  48. {
  49. #ifdef BLUEFRUIT_TRACE_SERIAL
  50. dprintf(" ");
  51. debug_hex8(data);
  52. dprintf(" ");
  53. #endif
  54. serial_send(data);
  55. }
  56. /*------------------------------------------------------------------*
  57. * Host driver
  58. *------------------------------------------------------------------*/
  59. static uint8_t keyboard_leds(void);
  60. static void send_keyboard(report_keyboard_t *report);
  61. static void send_mouse(report_mouse_t *report);
  62. static void send_system(uint16_t data);
  63. static void send_consumer(uint16_t data);
  64. void sendString(char string[], int length) {
  65. for(int i = 0; i < length; i++) {
  66. serial_send(string[i]);
  67. }
  68. }
  69. static host_driver_t driver = {
  70. keyboard_leds,
  71. send_keyboard,
  72. send_mouse,
  73. send_system,
  74. send_consumer
  75. };
  76. host_driver_t *bluefruit_driver(void)
  77. {
  78. return &driver;
  79. }
  80. static uint8_t keyboard_leds(void) {
  81. return bluefruit_keyboard_leds;
  82. }
  83. static void send_keyboard(report_keyboard_t *report)
  84. {
  85. #ifdef BLUEFRUIT_TRACE_SERIAL
  86. bluefruit_trace_header();
  87. #endif
  88. bluefruit_serial_send(0xFD);
  89. for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) {
  90. bluefruit_serial_send(report->raw[i]);
  91. }
  92. #ifdef BLUEFRUIT_TRACE_SERIAL
  93. bluefruit_trace_footer();
  94. #endif
  95. }
  96. static void send_mouse(report_mouse_t *report)
  97. {
  98. #ifdef BLUEFRUIT_TRACE_SERIAL
  99. bluefruit_trace_header();
  100. #endif
  101. bluefruit_serial_send(0xFD);
  102. bluefruit_serial_send(0x00);
  103. bluefruit_serial_send(0x03);
  104. bluefruit_serial_send(report->buttons);
  105. bluefruit_serial_send(report->x);
  106. bluefruit_serial_send(report->y);
  107. bluefruit_serial_send(report->v); // should try sending the wheel v here
  108. bluefruit_serial_send(report->h); // should try sending the wheel h here
  109. bluefruit_serial_send(0x00);
  110. #ifdef BLUEFRUIT_TRACE_SERIAL
  111. bluefruit_trace_footer();
  112. #endif
  113. }
  114. static void send_system(uint16_t data)
  115. {
  116. }
  117. /*
  118. +-----------------+-------------------+-------+
  119. | Consumer Key | Bit Map | Hex |
  120. +-----------------+-------------------+-------+
  121. | Home | 00000001 00000000 | 01 00 |
  122. | KeyboardLayout | 00000010 00000000 | 02 00 |
  123. | Search | 00000100 00000000 | 04 00 |
  124. | Snapshot | 00001000 00000000 | 08 00 |
  125. | VolumeUp | 00010000 00000000 | 10 00 |
  126. | VolumeDown | 00100000 00000000 | 20 00 |
  127. | Play/Pause | 01000000 00000000 | 40 00 |
  128. | Fast Forward | 10000000 00000000 | 80 00 |
  129. | Rewind | 00000000 00000001 | 00 01 |
  130. | Scan Next Track | 00000000 00000010 | 00 02 |
  131. | Scan Prev Track | 00000000 00000100 | 00 04 |
  132. | Random Play | 00000000 00001000 | 00 08 |
  133. | Stop | 00000000 00010000 | 00 10 |
  134. +-------------------------------------+-------+
  135. */
  136. #define CONSUMER2BLUEFRUIT(usage) \
  137. (usage == AUDIO_MUTE ? 0x0000 : \
  138. (usage == AUDIO_VOL_UP ? 0x1000 : \
  139. (usage == AUDIO_VOL_DOWN ? 0x2000 : \
  140. (usage == TRANSPORT_NEXT_TRACK ? 0x0002 : \
  141. (usage == TRANSPORT_PREV_TRACK ? 0x0004 : \
  142. (usage == TRANSPORT_STOP ? 0x0010 : \
  143. (usage == TRANSPORT_STOP_EJECT ? 0x0000 : \
  144. (usage == TRANSPORT_PLAY_PAUSE ? 0x4000 : \
  145. (usage == AL_CC_CONFIG ? 0x0000 : \
  146. (usage == AL_EMAIL ? 0x0000 : \
  147. (usage == AL_CALCULATOR ? 0x0000 : \
  148. (usage == AL_LOCAL_BROWSER ? 0x0000 : \
  149. (usage == AC_SEARCH ? 0x0400 : \
  150. (usage == AC_HOME ? 0x0100 : \
  151. (usage == AC_BACK ? 0x0000 : \
  152. (usage == AC_FORWARD ? 0x0000 : \
  153. (usage == AC_STOP ? 0x0000 : \
  154. (usage == AC_REFRESH ? 0x0000 : \
  155. (usage == AC_BOOKMARKS ? 0x0000 : 0)))))))))))))))))))
  156. static void send_consumer(uint16_t data)
  157. {
  158. static uint16_t last_data = 0;
  159. if (data == last_data) return;
  160. last_data = data;
  161. uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
  162. #ifdef BLUEFRUIT_TRACE_SERIAL
  163. dprintf("\nData: ");
  164. debug_hex16(data);
  165. dprintf("; bitmap: ");
  166. debug_hex16(bitmap);
  167. dprintf("\n");
  168. bluefruit_trace_header();
  169. #endif
  170. bluefruit_serial_send(0xFD);
  171. bluefruit_serial_send(0x00);
  172. bluefruit_serial_send(0x02);
  173. bluefruit_serial_send((bitmap>>8)&0xFF);
  174. bluefruit_serial_send(bitmap&0xFF);
  175. bluefruit_serial_send(0x00);
  176. bluefruit_serial_send(0x00);
  177. bluefruit_serial_send(0x00);
  178. bluefruit_serial_send(0x00);
  179. #ifdef BLUEFRUIT_TRACE_SERIAL
  180. bluefruit_trace_footer();
  181. #endif
  182. }