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.c338
1 files changed, 29 insertions, 309 deletions
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c
index e3f8724e8..a1cab98a6 100644
--- a/tmk_core/protocol/lufa/lufa.c
+++ b/tmk_core/protocol/lufa/lufa.c
@@ -49,7 +49,7 @@
49#endif 49#endif
50#include "suspend.h" 50#include "suspend.h"
51 51
52#include "descriptor.h" 52#include "usb_descriptor.h"
53#include "lufa.h" 53#include "lufa.h"
54#include "quantum.h" 54#include "quantum.h"
55#include <util/atomic.h> 55#include <util/atomic.h>
@@ -83,7 +83,7 @@
83#endif 83#endif
84 84
85#ifdef MIDI_ENABLE 85#ifdef MIDI_ENABLE
86 #include "sysex_tools.h" 86 #include "qmk_midi.h"
87#endif 87#endif
88 88
89#ifdef RAW_ENABLE 89#ifdef RAW_ENABLE
@@ -97,12 +97,6 @@ static uint8_t keyboard_led_stats = 0;
97 97
98static report_keyboard_t keyboard_report_sent; 98static report_keyboard_t keyboard_report_sent;
99 99
100#ifdef MIDI_ENABLE
101static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
102static void usb_get_midi(MidiDevice * device);
103static void midi_usb_init(MidiDevice * device);
104#endif
105
106/* Host driver */ 100/* Host driver */
107static uint8_t keyboard_leds(void); 101static uint8_t keyboard_leds(void);
108static void send_keyboard(report_keyboard_t *report); 102static void send_keyboard(report_keyboard_t *report);
@@ -115,48 +109,8 @@ host_driver_t lufa_driver = {
115 send_mouse, 109 send_mouse,
116 send_system, 110 send_system,
117 send_consumer, 111 send_consumer,
118#ifdef MIDI_ENABLE
119 usb_send_func,
120 usb_get_midi,
121 midi_usb_init
122#endif
123};
124
125/*******************************************************************************
126 * MIDI
127 ******************************************************************************/
128
129#ifdef MIDI_ENABLE
130USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface =
131{
132 .Config =
133 {
134 .StreamingInterfaceNumber = AS_INTERFACE,
135 .DataINEndpoint =
136 {
137 .Address = MIDI_STREAM_IN_EPADDR,
138 .Size = MIDI_STREAM_EPSIZE,
139 .Banks = 1,
140 },
141 .DataOUTEndpoint =
142 {
143 .Address = MIDI_STREAM_OUT_EPADDR,
144 .Size = MIDI_STREAM_EPSIZE,
145 .Banks = 1,
146 },
147 },
148}; 112};
149 113
150#define SYSEX_START_OR_CONT 0x40
151#define SYSEX_ENDS_IN_1 0x50
152#define SYSEX_ENDS_IN_2 0x60
153#define SYSEX_ENDS_IN_3 0x70
154
155#define SYS_COMMON_1 0x50
156#define SYS_COMMON_2 0x20
157#define SYS_COMMON_3 0x30
158#endif
159
160#ifdef VIRTSER_ENABLE 114#ifdef VIRTSER_ENABLE
161USB_ClassInfo_CDC_Device_t cdc_device = 115USB_ClassInfo_CDC_Device_t cdc_device =
162{ 116{
@@ -853,170 +807,32 @@ int8_t sendchar(uint8_t c)
853 ******************************************************************************/ 807 ******************************************************************************/
854 808
855#ifdef MIDI_ENABLE 809#ifdef MIDI_ENABLE
856static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { 810USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface =
857 MIDI_EventPacket_t event;
858 event.Data1 = byte0;
859 event.Data2 = byte1;
860 event.Data3 = byte2;
861
862 uint8_t cable = 0;
863
864// Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);
865
866 //if the length is undefined we assume it is a SYSEX message
867 if (midi_packet_length(byte0) == UNDEFINED) {
868 switch(cnt) {
869 case 3:
870 if (byte2 == SYSEX_END)
871 event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_3);
872 else
873 event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
874 break;
875 case 2:
876 if (byte1 == SYSEX_END)
877 event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_2);
878 else
879 event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
880 break;
881 case 1:
882 if (byte0 == SYSEX_END)
883 event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_1);
884 else
885 event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
886 break;
887 default:
888 return; //invalid cnt
889 }
890 } else {
891 //deal with 'system common' messages
892 //TODO are there any more?
893 switch(byte0 & 0xF0){
894 case MIDI_SONGPOSITION:
895 event.Event = MIDI_EVENT(cable, SYS_COMMON_3);
896 break;
897 case MIDI_SONGSELECT:
898 case MIDI_TC_QUARTERFRAME:
899 event.Event = MIDI_EVENT(cable, SYS_COMMON_2);
900 break;
901 default:
902 event.Event = MIDI_EVENT(cable, byte0);
903 break;
904 }
905 }
906
907// Endpoint_Write_Stream_LE(&event, sizeof(event), NULL);
908// Endpoint_ClearIN();
909
910 MIDI_Device_SendEventPacket(&USB_MIDI_Interface, &event);
911 MIDI_Device_Flush(&USB_MIDI_Interface);
912 MIDI_Device_USBTask(&USB_MIDI_Interface);
913 USB_USBTask();
914}
915
916static void usb_get_midi(MidiDevice * device) {
917 MIDI_EventPacket_t event;
918 while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) {
919
920 midi_packet_length_t length = midi_packet_length(event.Data1);
921 uint8_t input[3];
922 input[0] = event.Data1;
923 input[1] = event.Data2;
924 input[2] = event.Data3;
925 if (length == UNDEFINED) {
926 //sysex
927 if (event.Event == MIDI_EVENT(0, SYSEX_START_OR_CONT) || event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_3)) {
928 length = 3;
929 } else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_2)) {
930 length = 2;
931 } else if(event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_1)) {
932 length = 1;
933 } else {
934 //XXX what to do?
935 }
936 }
937
938 //pass the data to the device input function
939 if (length != UNDEFINED)
940 midi_device_input(device, length, input);
941 }
942 MIDI_Device_USBTask(&USB_MIDI_Interface);
943 USB_USBTask();
944}
945
946static void midi_usb_init(MidiDevice * device){
947 midi_device_init(device);
948 midi_device_set_send_func(device, usb_send_func);
949 midi_device_set_pre_input_process_func(device, usb_get_midi);
950
951 // SetupHardware();
952 sei();
953}
954
955void MIDI_Task(void)
956{ 811{
957 812 .Config =
958 /* Device must be connected and configured for the task to run */ 813 {
959 dprint("in MIDI_TASK\n"); 814 .StreamingInterfaceNumber = AS_INTERFACE,
960 if (USB_DeviceState != DEVICE_STATE_Configured) 815 .DataINEndpoint =
961 return;
962 dprint("continuing in MIDI_TASK\n");
963
964 Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPADDR);
965
966 if (Endpoint_IsINReady())
967 { 816 {
968 817 .Address = MIDI_STREAM_IN_EPADDR,
969 dprint("Endpoint is ready\n"); 818 .Size = MIDI_STREAM_EPSIZE,
970 819 .Banks = 1,
971 uint8_t MIDICommand = 0; 820 },
972 uint8_t MIDIPitch; 821 .DataOUTEndpoint =
973
974 /* Get board button status - if pressed use channel 10 (percussion), otherwise use channel 1 */
975 uint8_t Channel = MIDI_CHANNEL(1);
976
977 MIDICommand = MIDI_COMMAND_NOTE_ON;
978 MIDIPitch = 0x3E;
979
980 /* Check if a MIDI command is to be sent */
981 if (MIDICommand)
982 {
983 dprint("Command exists\n");
984 MIDI_EventPacket_t MIDIEvent = (MIDI_EventPacket_t)
985 {
986 .Event = MIDI_EVENT(0, MIDICommand),
987
988 .Data1 = MIDICommand | Channel,
989 .Data2 = MIDIPitch,
990 .Data3 = MIDI_STANDARD_VELOCITY,
991 };
992
993 /* Write the MIDI event packet to the endpoint */
994 Endpoint_Write_Stream_LE(&MIDIEvent, sizeof(MIDIEvent), NULL);
995
996 /* Send the data in the endpoint to the host */
997 Endpoint_ClearIN();
998 }
999 }
1000
1001
1002 /* Select the MIDI OUT stream */
1003 Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPADDR);
1004
1005 /* Check if a MIDI command has been received */
1006 if (Endpoint_IsOUTReceived())
1007 { 822 {
1008 MIDI_EventPacket_t MIDIEvent; 823 .Address = MIDI_STREAM_OUT_EPADDR,
824 .Size = MIDI_STREAM_EPSIZE,
825 .Banks = 1,
826 },
827 },
828};
1009 829
1010 /* Read the MIDI event packet from the endpoint */ 830void send_midi_packet(MIDI_EventPacket_t* event) {
1011 Endpoint_Read_Stream_LE(&MIDIEvent, sizeof(MIDIEvent), NULL); 831 MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event);
832}
1012 833
1013 /* If the endpoint is now empty, clear the bank */ 834bool recv_midi_packet(MIDI_EventPacket_t* const event) {
1014 if (!(Endpoint_BytesInEndpoint())) 835 return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event);
1015 {
1016 /* Clear the endpoint ready for new packet */
1017 Endpoint_ClearOUT();
1018 }
1019 }
1020} 836}
1021 837
1022#endif 838#endif
@@ -1105,26 +921,6 @@ static void setup_usb(void)
1105 print_set_sendchar(sendchar); 921 print_set_sendchar(sendchar);
1106} 922}
1107 923
1108
1109#ifdef MIDI_ENABLE
1110void fallthrough_callback(MidiDevice * device,
1111 uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
1112void cc_callback(MidiDevice * device,
1113 uint8_t chan, uint8_t num, uint8_t val);
1114void sysex_callback(MidiDevice * device,
1115 uint16_t start, uint8_t length, uint8_t * data);
1116
1117void setup_midi(void)
1118{
1119#ifdef MIDI_ADVANCED
1120 midi_init();
1121#endif
1122 midi_device_init(&midi_device);
1123 midi_device_set_send_func(&midi_device, usb_send_func);
1124 midi_device_set_pre_input_process_func(&midi_device, usb_get_midi);
1125}
1126#endif
1127
1128int main(void) __attribute__ ((weak)); 924int main(void) __attribute__ ((weak));
1129int main(void) 925int main(void)
1130{ 926{
@@ -1137,18 +933,6 @@ int main(void)
1137 setup_usb(); 933 setup_usb();
1138 sei(); 934 sei();
1139 935
1140#ifdef MIDI_ENABLE
1141 midi_register_fallthrough_callback(&midi_device, fallthrough_callback);
1142 midi_register_cc_callback(&midi_device, cc_callback);
1143 midi_register_sysex_callback(&midi_device, sysex_callback);
1144
1145 // init_notes();
1146 // midi_send_cc(&midi_device, 0, 1, 2);
1147 // midi_send_cc(&midi_device, 15, 1, 0);
1148 // midi_send_noteon(&midi_device, 0, 64, 127);
1149 // midi_send_noteoff(&midi_device, 0, 64, 127);
1150#endif
1151
1152#if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42) 936#if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42)
1153 serial_init(); 937 serial_init();
1154#endif 938#endif
@@ -1193,10 +977,7 @@ int main(void)
1193 keyboard_task(); 977 keyboard_task();
1194 978
1195#ifdef MIDI_ENABLE 979#ifdef MIDI_ENABLE
1196 midi_device_process(&midi_device); 980 MIDI_Device_USBTask(&USB_MIDI_Interface);
1197#ifdef MIDI_ADVANCED
1198 midi_task();
1199#endif
1200#endif 981#endif
1201 982
1202#if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE) 983#if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
@@ -1223,71 +1004,10 @@ int main(void)
1223 } 1004 }
1224} 1005}
1225 1006
1226#ifdef MIDI_ENABLE 1007uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
1227void fallthrough_callback(MidiDevice * device, 1008 const uint16_t wIndex,
1228 uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2){ 1009 const void** const DescriptorAddress)
1229 1010{
1230#ifdef AUDIO_ENABLE 1011 return get_usb_descriptor(wValue, wIndex, DescriptorAddress);
1231 if (cnt == 3) {
1232 switch (byte0 & 0xF0) {
1233 case MIDI_NOTEON:
1234 play_note(((double)261.6)*pow(2.0, -4.0)*pow(2.0,(byte1 & 0x7F)/12.0), (byte2 & 0x7F) / 8);
1235 break;
1236 case MIDI_NOTEOFF:
1237 stop_note(((double)261.6)*pow(2.0, -4.0)*pow(2.0,(byte1 & 0x7F)/12.0));
1238 break;
1239 }
1240 }
1241 if (byte0 == MIDI_STOP) {
1242 stop_all_notes();
1243 }
1244#endif
1245}
1246
1247
1248void cc_callback(MidiDevice * device,
1249 uint8_t chan, uint8_t num, uint8_t val) {
1250 //sending it back on the next channel
1251 // midi_send_cc(device, (chan + 1) % 16, num, val);
1252}
1253
1254#ifdef API_SYSEX_ENABLE
1255uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0};
1256#endif
1257
1258void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) {
1259 #ifdef API_SYSEX_ENABLE
1260 // SEND_STRING("\n");
1261 // send_word(start);
1262 // SEND_STRING(": ");
1263 // Don't store the header
1264 int16_t pos = start - 4;
1265 for (uint8_t place = 0; place < length; place++) {
1266 // send_byte(*data);
1267 if (pos >= 0) {
1268 if (*data == 0xF7) {
1269 // SEND_STRING("\nRD: ");
1270 // for (uint8_t i = 0; i < start + place + 1; i++){
1271 // send_byte(midi_buffer[i]);
1272 // SEND_STRING(" ");
1273 // }
1274 const unsigned decoded_length = sysex_decoded_length(pos);
1275 uint8_t decoded[API_SYSEX_MAX_SIZE];
1276 sysex_decode(decoded, midi_buffer, pos);
1277 process_api(decoded_length, decoded);
1278 return;
1279 }
1280 else if (pos >= MIDI_SYSEX_BUFFER) {
1281 return;
1282 }
1283 midi_buffer[pos] = *data;
1284 }
1285 // SEND_STRING(" ");
1286 data++;
1287 pos++;
1288 }
1289 #endif
1290} 1012}
1291 1013
1292
1293#endif