update macro names in bluefruit
[tmk_keyboard.git] / tmk_core / protocol / bluefruit / bluefruit.c
CommitLineData
a074364c 1/*
2Bluefruit Protocol for TMK firmware
3Author: Benjamin Gould, 2013
4Based on code Copyright 2011 Jun Wako <wakojun@gmail.com>
5
6This program is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include <stdint.h>
22#include "host.h"
23#include "report.h"
24#include "print.h"
25#include "debug.h"
26#include "host_driver.h"
27#include "serial.h"
28#include "bluefruit.h"
29
30#define BLUEFRUIT_TRACE_SERIAL 1
31
32static uint8_t bluefruit_keyboard_leds = 0;
33
34static void bluefruit_serial_send(uint8_t);
35
36void bluefruit_keyboard_print_report(report_keyboard_t *report)
37{
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);
42 dprintf("\n");
43}
44
45#ifdef BLUEFRUIT_TRACE_SERIAL
46static void bluefruit_trace_header()
47{
48 dprintf("+------------------------------------+\n");
49 dprintf("| HID report to Bluefruit via serial |\n");
50 dprintf("+------------------------------------+\n|");
51}
52
53static void bluefruit_trace_footer()
54{
55 dprintf("|\n+------------------------------------+\n\n");
56}
57#endif
58
59static void bluefruit_serial_send(uint8_t data)
60{
61#ifdef BLUEFRUIT_TRACE_SERIAL
62 dprintf(" ");
63 debug_hex8(data);
64 dprintf(" ");
65#endif
66 serial_send(data);
67}
68
69/*------------------------------------------------------------------*
70 * Host driver
71 *------------------------------------------------------------------*/
72
73static uint8_t keyboard_leds(void);
74static void send_keyboard(report_keyboard_t *report);
75static void send_mouse(report_mouse_t *report);
76static void send_system(uint16_t data);
77static void send_consumer(uint16_t data);
78
79static host_driver_t driver = {
80 keyboard_leds,
81 send_keyboard,
82 send_mouse,
83 send_system,
84 send_consumer
85};
86
87host_driver_t *bluefruit_driver(void)
88{
89 return &driver;
90}
91
92static uint8_t keyboard_leds(void) {
93 return bluefruit_keyboard_leds;
94}
95
96static void send_keyboard(report_keyboard_t *report)
97{
98#ifdef BLUEFRUIT_TRACE_SERIAL
99 bluefruit_trace_header();
100#endif
101 bluefruit_serial_send(0xFD);
102 for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) {
103 bluefruit_serial_send(report->raw[i]);
104 }
105#ifdef BLUEFRUIT_TRACE_SERIAL
106 bluefruit_trace_footer();
107#endif
108}
109
110static void send_mouse(report_mouse_t *report)
111{
112#ifdef BLUEFRUIT_TRACE_SERIAL
113 bluefruit_trace_header();
114#endif
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();
126#endif
127}
128
129static void send_system(uint16_t data)
130{
131}
132
133/*
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+-------------------------------------+-------+
151*/
152#define CONSUMER2BLUEFRUIT(usage) \
0c8c7745 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)))))))))))))))))))
a074364c 172
173static void send_consumer(uint16_t data)
174{
175 static uint16_t last_data = 0;
176 if (data == last_data) return;
177 last_data = data;
178
179 uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
180
181#ifdef BLUEFRUIT_TRACE_SERIAL
182 dprintf("\nData: ");
183 debug_hex16(data);
184 dprintf("; bitmap: ");
185 debug_hex16(bitmap);
186 dprintf("\n");
187 bluefruit_trace_header();
188#endif
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();
200#endif
201}
202
Imprint / Impressum