]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/bluefruit/bluefruit.c
changed tabs to spaces in bluefruit.c
[tmk_firmware.git] / protocol / bluefruit / bluefruit.c
1 /*
2 Bluefruit Protocol for TMK firmware
3 Author: Benjamin Gould, 2013
4 Based on code Copyright 2011 Jun Wako <wakojun@gmail.com>
5
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.
10
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.
15
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/>.
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
32 static uint8_t bluefruit_keyboard_leds = 0;
33
34 static void bluefruit_serial_send(uint8_t);
35
36 void bluefruit_keyboard_print_report(report_keyboard_t *report)
37 {
38     if (!debug_keyboard) return;
39     dprintf("keys: "); for (int i = 0; i < 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
46 static void bluefruit_trace_header()
47 {
48     dprintf("+------------------------------------+\n");
49     dprintf("| HID report to Bluefruit via serial |\n");
50     dprintf("+------------------------------------+\n|");
51 }
52
53 static void bluefruit_trace_footer()
54 {
55     dprintf("|\n+------------------------------------+\n\n");
56 }
57 #endif
58
59 static 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
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);
78
79 static host_driver_t driver = {
80         keyboard_leds,
81         send_keyboard,
82         send_mouse,
83         send_system,
84         send_consumer
85 };
86
87 host_driver_t *bluefruit_driver(void)
88 {
89     return &driver;
90 }
91
92 static uint8_t keyboard_leds(void) {
93     return bluefruit_keyboard_leds;
94 }
95
96 static 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 < REPORT_SIZE; i++) {
103         bluefruit_serial_send(report->raw[i]);
104     }
105 #ifdef BLUEFRUIT_TRACE_SERIAL   
106     bluefruit_trace_footer();   
107 #endif
108 }
109
110 static 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
129 static 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) \
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 == AL_CC_CONFIG         ? 0x0000  : \
162     (usage == AL_EMAIL             ? 0x0000  : \
163     (usage == AL_CALCULATOR        ? 0x0000  : \
164     (usage == AL_LOCAL_BROWSER     ? 0x0000  : \
165     (usage == AC_SEARCH            ? 0x0400  : \
166     (usage == AC_HOME              ? 0x0100  : \
167     (usage == AC_BACK              ? 0x0000  : \
168     (usage == AC_FORWARD           ? 0x0000  : \
169     (usage == AC_STOP              ? 0x0000  : \
170     (usage == AC_REFRESH           ? 0x0000  : \
171     (usage == AC_BOOKMARKS         ? 0x0000  : 0)))))))))))))))))))
172
173 static 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