aboutsummaryrefslogtreecommitdiff
path: root/protocol
diff options
context:
space:
mode:
authorBenjamin Gould <ben@bgould.org>2013-11-27 00:24:57 -0500
committerBenjamin Gould <ben@bgould.org>2013-11-27 00:24:57 -0500
commit05792e2b83ea99009d895a4caad0e40b5395aff3 (patch)
tree57d7a8056d04c957f6b0eaa0cc87a51b807e9c6f /protocol
parentc18c52f551545b46a28902c69730eefbdb75577d (diff)
downloadqmk_firmware-05792e2b83ea99009d895a4caad0e40b5395aff3.tar.gz
qmk_firmware-05792e2b83ea99009d895a4caad0e40b5395aff3.zip
Added bluefruit protocol and converter for 70% M
Diffstat (limited to 'protocol')
-rw-r--r--protocol/bluefruit.mk27
-rw-r--r--protocol/bluefruit/bluefruit.c202
-rw-r--r--protocol/bluefruit/bluefruit.h28
-rw-r--r--protocol/bluefruit/main.c116
4 files changed, 373 insertions, 0 deletions
diff --git a/protocol/bluefruit.mk b/protocol/bluefruit.mk
new file mode 100644
index 000000000..7e6328f6c
--- /dev/null
+++ b/protocol/bluefruit.mk
@@ -0,0 +1,27 @@
1BLUEFRUIT_DIR = protocol/bluefruit
2PJRC_DIR = protocol/pjrc
3
4SRC += $(BLUEFRUIT_DIR)/main.c \
5 $(BLUEFRUIT_DIR)/bluefruit.c \
6 serial_uart.c \
7 $(PJRC_DIR)/pjrc.c \
8 $(PJRC_DIR)/usb_keyboard.c \
9 $(PJRC_DIR)/usb_debug.c \
10 $(PJRC_DIR)/usb.c
11
12# Option modules
13ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE)
14 SRC += $(PJRC_DIR)/usb_mouse.c
15endif
16
17ifdef EXTRAKEY_ENABLE
18 SRC += $(PJRC_DIR)/usb_extra.c
19endif
20
21# Search Path
22VPATH += $(TOP_DIR)/$(BLUEFRUIT_DIR)
23#VPATH += $(TOP_DIR)/$(BLUEFRUIT_DIR)/usb_debug_only
24VPATH += $(TOP_DIR)/$(PJRC_DIR)
25
26OPT_DEFS += -DPROTOCOL_BLUEFRUIT
27OPT_DEFS += -DPROTOCOL_PJRC
diff --git a/protocol/bluefruit/bluefruit.c b/protocol/bluefruit/bluefruit.c
new file mode 100644
index 000000000..bdc3de3e6
--- /dev/null
+++ b/protocol/bluefruit/bluefruit.c
@@ -0,0 +1,202 @@
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 < 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 < 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) \
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
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
diff --git a/protocol/bluefruit/bluefruit.h b/protocol/bluefruit/bluefruit.h
new file mode 100644
index 000000000..4f9b58836
--- /dev/null
+++ b/protocol/bluefruit/bluefruit.h
@@ -0,0 +1,28 @@
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#ifndef VUSB_H
21#define VUSB_H
22
23#include "host_driver.h"
24
25
26host_driver_t *bluefruit_driver(void);
27
28#endif
diff --git a/protocol/bluefruit/main.c b/protocol/bluefruit/main.c
new file mode 100644
index 000000000..871062ab1
--- /dev/null
+++ b/protocol/bluefruit/main.c
@@ -0,0 +1,116 @@
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#include <stdint.h>
21#include <avr/interrupt.h>
22#include <avr/wdt.h>
23#include <avr/sleep.h>
24#include <util/delay.h>
25#include "serial.h"
26#include "keyboard.h"
27#include "usb.h"
28#include "host.h"
29#include "timer.h"
30#include "print.h"
31#include "debug.h"
32#include "sendchar.h"
33#include "suspend.h"
34#include "bluefruit.h"
35#include "pjrc.h"
36
37#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
38
39#define HOST_DRIVER_NOT_SET 0
40#define BLUEFRUIT_HOST_DRIVER 1
41#define PJRC_HOST_DRIVER 2
42
43int main(void)
44{
45
46 CPU_PRESCALE(0);
47
48 DDRD = _BV(PD5);
49 DDRB = _BV(PB0);
50
51 PORTD = _BV(PD5);
52 PORTB = _BV(PB0);
53
54 print_set_sendchar(sendchar);
55
56 usb_init();
57 _delay_ms(2000);
58 // while (!usb_configured()) /* wait */
59
60 dprintf("Initializing keyboard...\n");
61 keyboard_init();
62
63 // This implementation is pretty simplistic... if the USB connection
64 // is not configured, choose the Bluefruit, otherwise use USB
65 // Definitely would prefer to have this driven by an input pin and make
66 // it switch dynamically - BCG
67 if (!usb_configured()) {
68
69 // Send power to Bluefruit... Adafruit says it takes 27 mA, I think
70 // the pins should provide 40 mA, but just in case I switch the
71 // Bluefruit using a transistor - BCG
72 DDRB = _BV(PB6);
73 PORTB |= _BV(PB6);
74
75 dprintf("Setting host driver to bluefruit...\n");
76 host_set_driver(bluefruit_driver());
77
78 dprintf("Initializing serial...\n");
79 serial_init();
80
81 // wait an extra second for the PC's operating system
82 // to load drivers and do whatever it does to actually
83 // be ready for input
84 _delay_ms(1000);
85 PORTD = ~_BV(PD5);
86 dprintf("Starting main loop");
87 while (1) {
88 keyboard_task();
89 }
90
91 } else {
92
93 // I'm not smart enough to get this done with LUFA - BCG
94 dprintf("Setting host driver to PJRC...\n");
95 host_set_driver(pjrc_driver());
96#ifdef SLEEP_LED_ENABLE
97 sleep_led_init();
98#endif
99 // wait an extra second for the PC's operating system
100 // to load drivers and do whatever it does to actually
101 // be ready for input
102 _delay_ms(1000);
103 PORTB = ~_BV(PB0);
104 dprintf("Starting main loop");
105 while (1) {
106 while (suspend) {
107 suspend_power_down();
108 if (remote_wakeup && suspend_wakeup_condition()) {
109 usb_remote_wakeup();
110 }
111 }
112 keyboard_task();
113 }
114 }
115
116}