]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/bluefruit/bluefruit.c
2 Bluefruit Protocol for TMK firmware
3 Author: Benjamin Gould, 2013
4 Based on code Copyright 2011 Jun Wako <wakojun@gmail.com>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "host_driver.h"
28 #include "bluefruit.h"
30 #define BLUEFRUIT_TRACE_SERIAL 1
32 static uint8_t bluefruit_keyboard_leds
= 0;
34 static void bluefruit_serial_send(uint8_t);
36 void bluefruit_keyboard_print_report(report_keyboard_t
*report
)
38 if (!debug_keyboard
) return;
39 dprintf("keys: "); for (int i
= 0; i
< KEYBOARD_REPORT_KEYS
; i
++) { debug_hex8(report
->keys
[i
]); dprintf(" "); }
40 dprintf(" mods: "); debug_hex8(report
->mods
);
41 dprintf(" reserved: "); debug_hex8(report
->reserved
);
45 #ifdef BLUEFRUIT_TRACE_SERIAL
46 static void bluefruit_trace_header()
48 dprintf("+------------------------------------+\n");
49 dprintf("| HID report to Bluefruit via serial |\n");
50 dprintf("+------------------------------------+\n|");
53 static void bluefruit_trace_footer()
55 dprintf("|\n+------------------------------------+\n\n");
59 static void bluefruit_serial_send(uint8_t data
)
61 #ifdef BLUEFRUIT_TRACE_SERIAL
69 /*------------------------------------------------------------------*
71 *------------------------------------------------------------------*/
73 static uint8_t keyboard_leds(void);
74 static void send_keyboard(report_keyboard_t
*report
);
75 static void send_mouse(report_mouse_t
*report
);
76 static void send_system(uint16_t data
);
77 static void send_consumer(uint16_t data
);
79 static host_driver_t driver
= {
87 host_driver_t
*bluefruit_driver(void)
92 static uint8_t keyboard_leds(void) {
93 return bluefruit_keyboard_leds
;
96 static void send_keyboard(report_keyboard_t
*report
)
98 #ifdef BLUEFRUIT_TRACE_SERIAL
99 bluefruit_trace_header();
101 bluefruit_serial_send(0xFD);
102 for (uint8_t i
= 0; i
< KEYBOARD_REPORT_SIZE
; i
++) {
103 bluefruit_serial_send(report
->raw
[i
]);
105 #ifdef BLUEFRUIT_TRACE_SERIAL
106 bluefruit_trace_footer();
110 static void send_mouse(report_mouse_t
*report
)
112 #ifdef BLUEFRUIT_TRACE_SERIAL
113 bluefruit_trace_header();
115 bluefruit_serial_send(0xFD);
116 bluefruit_serial_send(0x00);
117 bluefruit_serial_send(0x03);
118 bluefruit_serial_send(report
->buttons
);
119 bluefruit_serial_send(report
->x
);
120 bluefruit_serial_send(report
->y
);
121 bluefruit_serial_send(report
->v
); // should try sending the wheel v here
122 bluefruit_serial_send(report
->h
); // should try sending the wheel h here
123 bluefruit_serial_send(0x00);
124 #ifdef BLUEFRUIT_TRACE_SERIAL
125 bluefruit_trace_footer();
129 static void send_system(uint16_t data
)
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 +-------------------------------------+-------+
152 #define CONSUMER2BLUEFRUIT(usage) \
153 (usage == AUDIO_MUTE ? 0x0000 : \
154 (usage == AUDIO_VOL_UP ? 0x1000 : \
155 (usage == AUDIO_VOL_DOWN ? 0x2000 : \
156 (usage == TRANSPORT_NEXT_TRACK ? 0x0002 : \
157 (usage == TRANSPORT_PREV_TRACK ? 0x0004 : \
158 (usage == TRANSPORT_STOP ? 0x0010 : \
159 (usage == TRANSPORT_STOP_EJECT ? 0x0000 : \
160 (usage == TRANSPORT_PLAY_PAUSE ? 0x4000 : \
161 (usage == APPLAUNCH_CC_CONFIG ? 0x0000 : \
162 (usage == APPLAUNCH_EMAIL ? 0x0000 : \
163 (usage == APPLAUNCH_CALCULATOR ? 0x0000 : \
164 (usage == APPLAUNCH_LOCAL_BROWSER ? 0x0000 : \
165 (usage == APPCONTROL_SEARCH ? 0x0400 : \
166 (usage == APPCONTROL_HOME ? 0x0100 : \
167 (usage == APPCONTROL_BACK ? 0x0000 : \
168 (usage == APPCONTROL_FORWARD ? 0x0000 : \
169 (usage == APPCONTROL_STOP ? 0x0000 : \
170 (usage == APPCONTROL_REFRESH ? 0x0000 : \
171 (usage == APPCONTROL_BOOKMARKS ? 0x0000 : 0)))))))))))))))))))
173 static void send_consumer(uint16_t data
)
175 static uint16_t last_data
= 0;
176 if (data
== last_data
) return;
179 uint16_t bitmap
= CONSUMER2BLUEFRUIT(data
);
181 #ifdef BLUEFRUIT_TRACE_SERIAL
184 dprintf("; bitmap: ");
187 bluefruit_trace_header();
189 bluefruit_serial_send(0xFD);
190 bluefruit_serial_send(0x00);
191 bluefruit_serial_send(0x02);
192 bluefruit_serial_send((bitmap
>>8)&0xFF);
193 bluefruit_serial_send(bitmap
&0xFF);
194 bluefruit_serial_send(0x00);
195 bluefruit_serial_send(0x00);
196 bluefruit_serial_send(0x00);
197 bluefruit_serial_send(0x00);
198 #ifdef BLUEFRUIT_TRACE_SERIAL
199 bluefruit_trace_footer();