Keyboard firmwares for Atmel AVR and Cortex-M
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.

bluefruit.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. static host_driver_t driver = {
  65. keyboard_leds,
  66. send_keyboard,
  67. send_mouse,
  68. send_system,
  69. send_consumer
  70. };
  71. host_driver_t *bluefruit_driver(void)
  72. {
  73. return &driver;
  74. }
  75. static uint8_t keyboard_leds(void) {
  76. return bluefruit_keyboard_leds;
  77. }
  78. static void send_keyboard(report_keyboard_t *report)
  79. {
  80. #ifdef BLUEFRUIT_TRACE_SERIAL
  81. bluefruit_trace_header();
  82. #endif
  83. bluefruit_serial_send(0xFD);
  84. for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) {
  85. bluefruit_serial_send(report->raw[i]);
  86. }
  87. #ifdef BLUEFRUIT_TRACE_SERIAL
  88. bluefruit_trace_footer();
  89. #endif
  90. }
  91. static void send_mouse(report_mouse_t *report)
  92. {
  93. #ifdef BLUEFRUIT_TRACE_SERIAL
  94. bluefruit_trace_header();
  95. #endif
  96. bluefruit_serial_send(0xFD);
  97. bluefruit_serial_send(0x00);
  98. bluefruit_serial_send(0x03);
  99. bluefruit_serial_send(report->buttons);
  100. bluefruit_serial_send(report->x);
  101. bluefruit_serial_send(report->y);
  102. bluefruit_serial_send(report->v); // should try sending the wheel v here
  103. bluefruit_serial_send(report->h); // should try sending the wheel h here
  104. bluefruit_serial_send(0x00);
  105. #ifdef BLUEFRUIT_TRACE_SERIAL
  106. bluefruit_trace_footer();
  107. #endif
  108. }
  109. static void send_system(uint16_t data)
  110. {
  111. }
  112. /*
  113. +-----------------+-------------------+-------+
  114. | Consumer Key | Bit Map | Hex |
  115. +-----------------+-------------------+-------+
  116. | Home | 00000001 00000000 | 01 00 |
  117. | KeyboardLayout | 00000010 00000000 | 02 00 |
  118. | Search | 00000100 00000000 | 04 00 |
  119. | Snapshot | 00001000 00000000 | 08 00 |
  120. | VolumeUp | 00010000 00000000 | 10 00 |
  121. | VolumeDown | 00100000 00000000 | 20 00 |
  122. | Play/Pause | 01000000 00000000 | 40 00 |
  123. | Fast Forward | 10000000 00000000 | 80 00 |
  124. | Rewind | 00000000 00000001 | 00 01 |
  125. | Scan Next Track | 00000000 00000010 | 00 02 |
  126. | Scan Prev Track | 00000000 00000100 | 00 04 |
  127. | Random Play | 00000000 00001000 | 00 08 |
  128. | Stop | 00000000 00010000 | 00 10 |
  129. +-------------------------------------+-------+
  130. */
  131. #define CONSUMER2BLUEFRUIT(usage) \
  132. (usage == AUDIO_MUTE ? 0x0000 : \
  133. (usage == AUDIO_VOL_UP ? 0x1000 : \
  134. (usage == AUDIO_VOL_DOWN ? 0x2000 : \
  135. (usage == TRANSPORT_NEXT_TRACK ? 0x0002 : \
  136. (usage == TRANSPORT_PREV_TRACK ? 0x0004 : \
  137. (usage == TRANSPORT_STOP ? 0x0010 : \
  138. (usage == TRANSPORT_STOP_EJECT ? 0x0000 : \
  139. (usage == TRANSPORT_PLAY_PAUSE ? 0x4000 : \
  140. (usage == AL_CC_CONFIG ? 0x0000 : \
  141. (usage == AL_EMAIL ? 0x0000 : \
  142. (usage == AL_CALCULATOR ? 0x0000 : \
  143. (usage == AL_LOCAL_BROWSER ? 0x0000 : \
  144. (usage == AC_SEARCH ? 0x0400 : \
  145. (usage == AC_HOME ? 0x0100 : \
  146. (usage == AC_BACK ? 0x0000 : \
  147. (usage == AC_FORWARD ? 0x0000 : \
  148. (usage == AC_STOP ? 0x0000 : \
  149. (usage == AC_REFRESH ? 0x0000 : \
  150. (usage == AC_BOOKMARKS ? 0x0000 : 0)))))))))))))))))))
  151. static void send_consumer(uint16_t data)
  152. {
  153. static uint16_t last_data = 0;
  154. if (data == last_data) return;
  155. last_data = data;
  156. uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
  157. #ifdef BLUEFRUIT_TRACE_SERIAL
  158. dprintf("\nData: ");
  159. debug_hex16(data);
  160. dprintf("; bitmap: ");
  161. debug_hex16(bitmap);
  162. dprintf("\n");
  163. bluefruit_trace_header();
  164. #endif
  165. bluefruit_serial_send(0xFD);
  166. bluefruit_serial_send(0x00);
  167. bluefruit_serial_send(0x02);
  168. bluefruit_serial_send((bitmap>>8)&0xFF);
  169. bluefruit_serial_send(bitmap&0xFF);
  170. bluefruit_serial_send(0x00);
  171. bluefruit_serial_send(0x00);
  172. bluefruit_serial_send(0x00);
  173. bluefruit_serial_send(0x00);
  174. #ifdef BLUEFRUIT_TRACE_SERIAL
  175. bluefruit_trace_footer();
  176. #endif
  177. }