aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/lufa/lufa.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/protocol/lufa/lufa.c')
-rw-r--r--tmk_core/protocol/lufa/lufa.c305
1 files changed, 245 insertions, 60 deletions
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c
index 01c0e45b0..60cba8d2a 100644
--- a/tmk_core/protocol/lufa/lufa.c
+++ b/tmk_core/protocol/lufa/lufa.c
@@ -51,6 +51,9 @@
51 51
52#include "descriptor.h" 52#include "descriptor.h"
53#include "lufa.h" 53#include "lufa.h"
54#include "quantum.h"
55#include <util/atomic.h>
56#include "outputselect.h"
54 57
55#ifdef NKRO_ENABLE 58#ifdef NKRO_ENABLE
56 #include "keycode_config.h" 59 #include "keycode_config.h"
@@ -64,13 +67,29 @@
64#endif 67#endif
65 68
66#ifdef BLUETOOTH_ENABLE 69#ifdef BLUETOOTH_ENABLE
70 #ifdef MODULE_ADAFRUIT_BLE
71 #include "adafruit_ble.h"
72 #else
67 #include "bluetooth.h" 73 #include "bluetooth.h"
74 #endif
68#endif 75#endif
69 76
70#ifdef VIRTSER_ENABLE 77#ifdef VIRTSER_ENABLE
71 #include "virtser.h" 78 #include "virtser.h"
72#endif 79#endif
73 80
81#if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
82 #include "rgblight.h"
83#endif
84
85#ifdef MIDI_ENABLE
86 #include "sysex_tools.h"
87#endif
88
89#ifdef RAW_ENABLE
90 #include "raw_hid.h"
91#endif
92
74uint8_t keyboard_idle = 0; 93uint8_t keyboard_idle = 0;
75/* 0: Boot Protocol, 1: Report Protocol(default) */ 94/* 0: Boot Protocol, 1: Report Protocol(default) */
76uint8_t keyboard_protocol = 1; 95uint8_t keyboard_protocol = 1;
@@ -79,9 +98,9 @@ static uint8_t keyboard_led_stats = 0;
79static report_keyboard_t keyboard_report_sent; 98static report_keyboard_t keyboard_report_sent;
80 99
81#ifdef MIDI_ENABLE 100#ifdef MIDI_ENABLE
82void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2); 101static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
83void usb_get_midi(MidiDevice * device); 102static void usb_get_midi(MidiDevice * device);
84void midi_usb_init(MidiDevice * device); 103static void midi_usb_init(MidiDevice * device);
85#endif 104#endif
86 105
87/* Host driver */ 106/* Host driver */
@@ -166,6 +185,80 @@ USB_ClassInfo_CDC_Device_t cdc_device =
166}; 185};
167#endif 186#endif
168 187
188#ifdef RAW_ENABLE
189
190void raw_hid_send( uint8_t *data, uint8_t length )
191{
192 // TODO: implement variable size packet
193 if ( length != RAW_EPSIZE )
194 {
195 return;
196 }
197
198 if (USB_DeviceState != DEVICE_STATE_Configured)
199 {
200 return;
201 }
202
203 // TODO: decide if we allow calls to raw_hid_send() in the middle
204 // of other endpoint usage.
205 uint8_t ep = Endpoint_GetCurrentEndpoint();
206
207 Endpoint_SelectEndpoint(RAW_IN_EPNUM);
208
209 // Check to see if the host is ready to accept another packet
210 if (Endpoint_IsINReady())
211 {
212 // Write data
213 Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
214 // Finalize the stream transfer to send the last packet
215 Endpoint_ClearIN();
216 }
217
218 Endpoint_SelectEndpoint(ep);
219}
220
221__attribute__ ((weak))
222void raw_hid_receive( uint8_t *data, uint8_t length )
223{
224 // Users should #include "raw_hid.h" in their own code
225 // and implement this function there. Leave this as weak linkage
226 // so users can opt to not handle data coming in.
227}
228
229static void raw_hid_task(void)
230{
231 // Create a temporary buffer to hold the read in data from the host
232 uint8_t data[RAW_EPSIZE];
233 bool data_read = false;
234
235 // Device must be connected and configured for the task to run
236 if (USB_DeviceState != DEVICE_STATE_Configured)
237 return;
238
239 Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
240
241 // Check to see if a packet has been sent from the host
242 if (Endpoint_IsOUTReceived())
243 {
244 // Check to see if the packet contains data
245 if (Endpoint_IsReadWriteAllowed())
246 {
247 /* Read data */
248 Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
249 data_read = true;
250 }
251
252 // Finalize the stream transfer to receive the last packet
253 Endpoint_ClearOUT();
254
255 if ( data_read )
256 {
257 raw_hid_receive( data, sizeof(data) );
258 }
259 }
260}
261#endif
169 262
170/******************************************************************************* 263/*******************************************************************************
171 * Console 264 * Console
@@ -285,10 +378,14 @@ void EVENT_USB_Device_WakeUp()
285#endif 378#endif
286} 379}
287 380
381
382
288#ifdef CONSOLE_ENABLE 383#ifdef CONSOLE_ENABLE
289static bool console_flush = false; 384static bool console_flush = false;
290#define CONSOLE_FLUSH_SET(b) do { \ 385#define CONSOLE_FLUSH_SET(b) do { \
291 uint8_t sreg = SREG; cli(); console_flush = b; SREG = sreg; \ 386 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {\
387 console_flush = b; \
388 } \
292} while (0) 389} while (0)
293 390
294// called every 1ms 391// called every 1ms
@@ -302,6 +399,7 @@ void EVENT_USB_Device_StartOfFrame(void)
302 Console_Task(); 399 Console_Task();
303 console_flush = false; 400 console_flush = false;
304} 401}
402
305#endif 403#endif
306 404
307/** Event handler for the USB_ConfigurationChanged event. 405/** Event handler for the USB_ConfigurationChanged event.
@@ -330,6 +428,14 @@ void EVENT_USB_Device_ConfigurationChanged(void)
330 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE); 428 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
331#endif 429#endif
332 430
431#ifdef RAW_ENABLE
432 /* Setup Raw HID Report Endpoints */
433 ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
434 RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
435 ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
436 RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
437#endif
438
333#ifdef CONSOLE_ENABLE 439#ifdef CONSOLE_ENABLE
334 /* Setup Console HID Report Endpoints */ 440 /* Setup Console HID Report Endpoints */
335 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 441 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
@@ -485,7 +591,6 @@ void EVENT_USB_Device_ControlRequest(void)
485 591
486/******************************************************************************* 592/*******************************************************************************
487 * Host driver 593 * Host driver
488p
489 ******************************************************************************/ 594 ******************************************************************************/
490static uint8_t keyboard_leds(void) 595static uint8_t keyboard_leds(void)
491{ 596{
@@ -494,18 +599,25 @@ static uint8_t keyboard_leds(void)
494 599
495static void send_keyboard(report_keyboard_t *report) 600static void send_keyboard(report_keyboard_t *report)
496{ 601{
602 uint8_t timeout = 255;
603 uint8_t where = where_to_send();
497 604
498#ifdef BLUETOOTH_ENABLE 605#ifdef BLUETOOTH_ENABLE
499 bluefruit_serial_send(0xFD); 606 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
500 for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++) { 607 #ifdef MODULE_ADAFRUIT_BLE
608 adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys));
609 #else
610 bluefruit_serial_send(0xFD);
611 for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++) {
501 bluefruit_serial_send(report->raw[i]); 612 bluefruit_serial_send(report->raw[i]);
502 } 613 }
614 #endif
615 }
503#endif 616#endif
504 617
505 uint8_t timeout = 255; 618 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
506 619 return;
507 if (USB_DeviceState != DEVICE_STATE_Configured) 620 }
508 return;
509 621
510 /* Select the Keyboard Report Endpoint */ 622 /* Select the Keyboard Report Endpoint */
511#ifdef NKRO_ENABLE 623#ifdef NKRO_ENABLE
@@ -543,23 +655,31 @@ static void send_keyboard(report_keyboard_t *report)
543static void send_mouse(report_mouse_t *report) 655static void send_mouse(report_mouse_t *report)
544{ 656{
545#ifdef MOUSE_ENABLE 657#ifdef MOUSE_ENABLE
658 uint8_t timeout = 255;
659 uint8_t where = where_to_send();
546 660
547#ifdef BLUETOOTH_ENABLE 661#ifdef BLUETOOTH_ENABLE
548 bluefruit_serial_send(0xFD); 662 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
549 bluefruit_serial_send(0x00); 663 #ifdef MODULE_ADAFRUIT_BLE
550 bluefruit_serial_send(0x03); 664 // FIXME: mouse buttons
551 bluefruit_serial_send(report->buttons); 665 adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h);
552 bluefruit_serial_send(report->x); 666 #else
553 bluefruit_serial_send(report->y); 667 bluefruit_serial_send(0xFD);
554 bluefruit_serial_send(report->v); // should try sending the wheel v here 668 bluefruit_serial_send(0x00);
555 bluefruit_serial_send(report->h); // should try sending the wheel h here 669 bluefruit_serial_send(0x03);
556 bluefruit_serial_send(0x00); 670 bluefruit_serial_send(report->buttons);
671 bluefruit_serial_send(report->x);
672 bluefruit_serial_send(report->y);
673 bluefruit_serial_send(report->v); // should try sending the wheel v here
674 bluefruit_serial_send(report->h); // should try sending the wheel h here
675 bluefruit_serial_send(0x00);
676 #endif
677 }
557#endif 678#endif
558 679
559 uint8_t timeout = 255; 680 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
560 681 return;
561 if (USB_DeviceState != DEVICE_STATE_Configured) 682 }
562 return;
563 683
564 /* Select the Mouse Report Endpoint */ 684 /* Select the Mouse Report Endpoint */
565 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM); 685 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
@@ -585,7 +705,7 @@ static void send_system(uint16_t data)
585 705
586 report_extra_t r = { 706 report_extra_t r = {
587 .report_id = REPORT_ID_SYSTEM, 707 .report_id = REPORT_ID_SYSTEM,
588 .usage = data 708 .usage = data - SYSTEM_POWER_DOWN + 1
589 }; 709 };
590 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM); 710 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
591 711
@@ -599,27 +719,34 @@ static void send_system(uint16_t data)
599 719
600static void send_consumer(uint16_t data) 720static void send_consumer(uint16_t data)
601{ 721{
722 uint8_t timeout = 255;
723 uint8_t where = where_to_send();
602 724
603#ifdef BLUETOOTH_ENABLE 725#ifdef BLUETOOTH_ENABLE
604 static uint16_t last_data = 0; 726 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
605 if (data == last_data) return; 727 #ifdef MODULE_ADAFRUIT_BLE
606 last_data = data; 728 adafruit_ble_send_consumer_key(data, 0);
607 uint16_t bitmap = CONSUMER2BLUEFRUIT(data); 729 #else
608 bluefruit_serial_send(0xFD); 730 static uint16_t last_data = 0;
609 bluefruit_serial_send(0x00); 731 if (data == last_data) return;
610 bluefruit_serial_send(0x02); 732 last_data = data;
611 bluefruit_serial_send((bitmap>>8)&0xFF); 733 uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
612 bluefruit_serial_send(bitmap&0xFF); 734 bluefruit_serial_send(0xFD);
613 bluefruit_serial_send(0x00); 735 bluefruit_serial_send(0x00);
614 bluefruit_serial_send(0x00); 736 bluefruit_serial_send(0x02);
615 bluefruit_serial_send(0x00); 737 bluefruit_serial_send((bitmap>>8)&0xFF);
616 bluefruit_serial_send(0x00); 738 bluefruit_serial_send(bitmap&0xFF);
739 bluefruit_serial_send(0x00);
740 bluefruit_serial_send(0x00);
741 bluefruit_serial_send(0x00);
742 bluefruit_serial_send(0x00);
743 #endif
744 }
617#endif 745#endif
618 746
619 uint8_t timeout = 255; 747 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
620 748 return;
621 if (USB_DeviceState != DEVICE_STATE_Configured) 749 }
622 return;
623 750
624 report_extra_t r = { 751 report_extra_t r = {
625 .report_id = REPORT_ID_CONSUMER, 752 .report_id = REPORT_ID_CONSUMER,
@@ -709,7 +836,7 @@ int8_t sendchar(uint8_t c)
709 ******************************************************************************/ 836 ******************************************************************************/
710 837
711#ifdef MIDI_ENABLE 838#ifdef MIDI_ENABLE
712void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { 839static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
713 MIDI_EventPacket_t event; 840 MIDI_EventPacket_t event;
714 event.Data1 = byte0; 841 event.Data1 = byte0;
715 event.Data2 = byte1; 842 event.Data2 = byte1;
@@ -769,7 +896,7 @@ void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byt
769 USB_USBTask(); 896 USB_USBTask();
770} 897}
771 898
772void usb_get_midi(MidiDevice * device) { 899static void usb_get_midi(MidiDevice * device) {
773 MIDI_EventPacket_t event; 900 MIDI_EventPacket_t event;
774 while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) { 901 while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) {
775 902
@@ -799,12 +926,12 @@ void usb_get_midi(MidiDevice * device) {
799 USB_USBTask(); 926 USB_USBTask();
800} 927}
801 928
802void midi_usb_init(MidiDevice * device){ 929static void midi_usb_init(MidiDevice * device){
803 midi_device_init(device); 930 midi_device_init(device);
804 midi_device_set_send_func(device, usb_send_func); 931 midi_device_set_send_func(device, usb_send_func);
805 midi_device_set_pre_input_process_func(device, usb_get_midi); 932 midi_device_set_pre_input_process_func(device, usb_get_midi);
806 933
807 SetupHardware(); 934 // SetupHardware();
808 sei(); 935 sei();
809} 936}
810 937
@@ -969,16 +1096,23 @@ void cc_callback(MidiDevice * device,
969 uint8_t chan, uint8_t num, uint8_t val); 1096 uint8_t chan, uint8_t num, uint8_t val);
970void sysex_callback(MidiDevice * device, 1097void sysex_callback(MidiDevice * device,
971 uint16_t start, uint8_t length, uint8_t * data); 1098 uint16_t start, uint8_t length, uint8_t * data);
1099
1100void setup_midi(void)
1101{
1102#ifdef MIDI_ADVANCED
1103 midi_init();
1104#endif
1105 midi_device_init(&midi_device);
1106 midi_device_set_send_func(&midi_device, usb_send_func);
1107 midi_device_set_pre_input_process_func(&midi_device, usb_get_midi);
1108}
972#endif 1109#endif
973 1110
974int main(void) __attribute__ ((weak)); 1111int main(void) __attribute__ ((weak));
975int main(void) 1112int main(void)
976{ 1113{
977
978#ifdef MIDI_ENABLE 1114#ifdef MIDI_ENABLE
979 midi_device_init(&midi_device); 1115 setup_midi();
980 midi_device_set_send_func(&midi_device, usb_send_func);
981 midi_device_set_pre_input_process_func(&midi_device, usb_get_midi);
982#endif 1116#endif
983 1117
984 setup_mcu(); 1118 setup_mcu();
@@ -998,7 +1132,7 @@ int main(void)
998 // midi_send_noteoff(&midi_device, 0, 64, 127); 1132 // midi_send_noteoff(&midi_device, 0, 64, 127);
999#endif 1133#endif
1000 1134
1001#ifdef BLUETOOTH_ENABLE 1135#ifdef MODULE_ADAFRUIT_EZKEY
1002 serial_init(); 1136 serial_init();
1003#endif 1137#endif
1004 1138
@@ -1029,7 +1163,7 @@ int main(void)
1029 1163
1030 print("Keyboard start.\n"); 1164 print("Keyboard start.\n");
1031 while (1) { 1165 while (1) {
1032 #ifndef BLUETOOTH_ENABLE 1166 #if !defined(BLUETOOTH_ENABLE)
1033 while (USB_DeviceState == DEVICE_STATE_Suspended) { 1167 while (USB_DeviceState == DEVICE_STATE_Suspended) {
1034 print("[s]"); 1168 print("[s]");
1035 suspend_power_down(); 1169 suspend_power_down();
@@ -1039,20 +1173,36 @@ int main(void)
1039 } 1173 }
1040 #endif 1174 #endif
1041 1175
1176 keyboard_task();
1177
1042#ifdef MIDI_ENABLE 1178#ifdef MIDI_ENABLE
1043 midi_device_process(&midi_device); 1179 midi_device_process(&midi_device);
1044 // MIDI_Task(); 1180#ifdef MIDI_ADVANCED
1181 midi_task();
1182#endif
1183#endif
1184
1185#if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
1186 rgblight_task();
1187#endif
1188
1189#ifdef MODULE_ADAFRUIT_BLE
1190 adafruit_ble_task();
1045#endif 1191#endif
1046 keyboard_task();
1047 1192
1048#ifdef VIRTSER_ENABLE 1193#ifdef VIRTSER_ENABLE
1049 virtser_task(); 1194 virtser_task();
1050 CDC_Device_USBTask(&cdc_device); 1195 CDC_Device_USBTask(&cdc_device);
1051#endif 1196#endif
1052 1197
1198#ifdef RAW_ENABLE
1199 raw_hid_task();
1200#endif
1201
1053#if !defined(INTERRUPT_CONTROL_ENDPOINT) 1202#if !defined(INTERRUPT_CONTROL_ENDPOINT)
1054 USB_USBTask(); 1203 USB_USBTask();
1055#endif 1204#endif
1205
1056 } 1206 }
1057} 1207}
1058 1208
@@ -1077,15 +1227,50 @@ void fallthrough_callback(MidiDevice * device,
1077#endif 1227#endif
1078} 1228}
1079 1229
1230
1080void cc_callback(MidiDevice * device, 1231void cc_callback(MidiDevice * device,
1081 uint8_t chan, uint8_t num, uint8_t val) { 1232 uint8_t chan, uint8_t num, uint8_t val) {
1082 //sending it back on the next channel 1233 //sending it back on the next channel
1083 midi_send_cc(device, (chan + 1) % 16, num, val); 1234 // midi_send_cc(device, (chan + 1) % 16, num, val);
1084} 1235}
1085 1236
1086void sysex_callback(MidiDevice * device, 1237#ifdef API_SYSEX_ENABLE
1087 uint16_t start, uint8_t length, uint8_t * data) { 1238uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0};
1088 for (int i = 0; i < length; i++) 1239#endif
1089 midi_send_cc(device, 15, 0x7F & data[i], 0x7F & (start + i)); 1240
1241void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) {
1242 #ifdef API_SYSEX_ENABLE
1243 // SEND_STRING("\n");
1244 // send_word(start);
1245 // SEND_STRING(": ");
1246 // Don't store the header
1247 int16_t pos = start - 4;
1248 for (uint8_t place = 0; place < length; place++) {
1249 // send_byte(*data);
1250 if (pos >= 0) {
1251 if (*data == 0xF7) {
1252 // SEND_STRING("\nRD: ");
1253 // for (uint8_t i = 0; i < start + place + 1; i++){
1254 // send_byte(midi_buffer[i]);
1255 // SEND_STRING(" ");
1256 // }
1257 const unsigned decoded_length = sysex_decoded_length(pos);
1258 uint8_t decoded[API_SYSEX_MAX_SIZE];
1259 sysex_decode(decoded, midi_buffer, pos);
1260 process_api(decoded_length, decoded);
1261 return;
1262 }
1263 else if (pos >= MIDI_SYSEX_BUFFER) {
1264 return;
1265 }
1266 midi_buffer[pos] = *data;
1267 }
1268 // SEND_STRING(" ");
1269 data++;
1270 pos++;
1271 }
1272 #endif
1090} 1273}
1274
1275
1091#endif 1276#endif