]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - tmk_core/protocol/lufa/lufa.c
travis pls
[qmk_firmware.git] / tmk_core / protocol / lufa / lufa.c
index ae9cc2f962f631ef941d310a69b5beeeb85b028b..eae3e8f298085de22e0bf896dbb6375a23771013 100644 (file)
     #include "virtser.h"
 #endif
 
-#ifdef RGB_MIDI
+#if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
     #include "rgblight.h"        
 #endif
 
+#ifdef MIDI_ENABLE
+  #include "sysex_tools.h"
+#endif
+
 uint8_t keyboard_idle = 0;
 /* 0: Boot Protocol, 1: Report Protocol(default) */
 uint8_t keyboard_protocol = 1;
@@ -84,9 +88,9 @@ static uint8_t keyboard_led_stats = 0;
 static report_keyboard_t keyboard_report_sent;
 
 #ifdef MIDI_ENABLE
-void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
-void usb_get_midi(MidiDevice * device);
-void midi_usb_init(MidiDevice * device);
+static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
+static void usb_get_midi(MidiDevice * device);
+static void midi_usb_init(MidiDevice * device);
 #endif
 
 /* Host driver */
@@ -714,7 +718,7 @@ int8_t sendchar(uint8_t c)
  ******************************************************************************/
 
 #ifdef MIDI_ENABLE
-void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
+static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
   MIDI_EventPacket_t event;
   event.Data1 = byte0;
   event.Data2 = byte1;
@@ -774,7 +778,7 @@ void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byt
   USB_USBTask();
 }
 
-void usb_get_midi(MidiDevice * device) {
+static void usb_get_midi(MidiDevice * device) {
   MIDI_EventPacket_t event;
   while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) {
 
@@ -804,12 +808,12 @@ void usb_get_midi(MidiDevice * device) {
   USB_USBTask();
 }
 
-void midi_usb_init(MidiDevice * device){
+static void midi_usb_init(MidiDevice * device){
   midi_device_init(device);
   midi_device_set_send_func(device, usb_send_func);
   midi_device_set_pre_input_process_func(device, usb_get_midi);
 
-  SetupHardware();
+  // SetupHardware();
   sei();
 }
 
@@ -1051,7 +1055,7 @@ int main(void)
         // MIDI_Task();
 #endif
         
-#ifdef RGBLIGHT_ENABLE
+#ifdef RGBLIGHT_ANIMATIONS
         rgblight_task();
 #endif
 
@@ -1112,45 +1116,9 @@ void cc_callback(MidiDevice * device,
     #endif
 }
 
-void send_dword(uint32_t number) {
-    uint16_t word = (number >> 16);
-    send_word(word);
-    send_word(number & 0xFFFFUL);
-}
-
-void send_word(uint16_t number) {
-    uint8_t byte = number >> 8;
-    send_byte(byte);
-    send_byte(number & 0xFF);
-}
-
-void send_byte(uint8_t number) {
-    uint8_t nibble = number >> 4;
-    send_nibble(nibble);
-    send_nibble(number & 0xF);
-}
-
-void send_nibble(uint8_t number) {
-    switch (number) {
-        case 0:
-            register_code(KC_0);
-            unregister_code(KC_0);
-            break;
-        case 1 ... 9:
-            register_code(KC_1 + (number - 1));
-            unregister_code(KC_1 + (number - 1));
-            break;
-        case 0xA ... 0xF:
-            register_code(KC_A + (number - 0xA));
-            unregister_code(KC_A + (number - 0xA));
-            break;
-    }
-}
-
-uint8_t midi_buffer[16] = {0};
+uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0};
 
-void sysex_callback(MidiDevice * device,
-    uint16_t start, uint8_t length, uint8_t * data) {
+void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) {
   // for (int i = 0; i < length; i++)
   //   midi_send_cc(device, 15, 0x7F & data[i], 0x7F & (start + i));
     // if (start == 0x27) {
@@ -1160,8 +1128,16 @@ void sysex_callback(MidiDevice * device,
         for (uint8_t place = 0; place < length; place++) {
             // send_byte(*data);
             midi_buffer[start + place] = *data;
-            if (*data == 0xF7)
-                sysex_buffer_callback(device, start + place, &midi_buffer);
+            if (*data == 0xF7) {
+                // SEND_STRING("\nRD: ");
+                // for (uint8_t i = 0; i < start + place + 1; i++){
+                //     send_byte(midi_buffer[i]);
+                // SEND_STRING(" ");
+                // }
+                uint8_t * decoded = malloc(sizeof(uint8_t) * (sysex_decoded_length(start + place - 4)));
+                uint16_t decode_length = sysex_decode(decoded, midi_buffer + 4, start + place - 4);
+                sysex_buffer_callback(device, decode_length, decoded);
+            }
             // SEND_STRING(" ");
             data++;
         }
@@ -1169,75 +1145,181 @@ void sysex_callback(MidiDevice * device,
 
 }
 
-uint32_t decode_4byte_chunk(uint8_t * data) {
-    uint32_t part1 = *data++;
-    uint32_t part2 = *data++;
-    uint32_t part3 = *data++;
-    uint32_t part4 = *data++;
-    uint32_t part5 = *data++;
-    return ((part1 & 0x1FUL) << 28) | (part2 << 21) | (part3 << 14) | (part4 << 7) | part5;
+void dword_to_bytes(uint32_t dword, uint8_t * bytes) {
+    bytes[0] = (dword >> 24) & 0xFF;
+    bytes[1] = (dword >> 16) & 0xFF; 
+    bytes[2] = (dword >> 8) & 0xFF; 
+    bytes[3] = (dword >> 0) & 0xFF; 
 }
 
-void encode_4byte_chunk(uint32_t data, uint8_t * pointer) {
-    *pointer++ = (data >> 28) & 0x7F;
-    *pointer++ = (data >> 21) & 0x7F;
-    *pointer++ = (data >> 14) & 0x7F;
-    *pointer++ = (data >> 7) & 0x7F;
-    *pointer++ = (data) & 0x7F;
+uint32_t bytes_to_dword(uint8_t * bytes, uint8_t index) {
+    return ((uint32_t)bytes[index + 0] << 24) | ((uint32_t)bytes[index + 1] << 16) | ((uint32_t)bytes[index + 2] << 8) | (uint32_t)bytes[index + 3];
+}
+
+void send_bytes_sysex(uint8_t message_type, uint8_t data_type, uint8_t * bytes, uint8_t length) {
+    // SEND_STRING("\nTX: ");
+    // for (uint8_t i = 0; i < length; i++) {
+    //     send_byte(bytes[i]);
+    //     SEND_STRING(" ");
+    // }
+    uint8_t * precode = malloc(sizeof(uint8_t) * (length + 2));
+    precode[0] = message_type;
+    precode[1] = data_type;
+    memcpy(precode + 2, bytes, length);
+    uint8_t * encoded = malloc(sizeof(uint8_t) * (sysex_encoded_length(length + 2)));
+    uint16_t encoded_length = sysex_encode(encoded, precode, length + 2);
+    uint8_t * array = malloc(sizeof(uint8_t) * (encoded_length + 5));
+    array[0] = 0xF0;
+    array[1] = 0x00;
+    array[2] = 0x00;
+    array[3] = 0x00;
+    array[encoded_length + 4] = 0xF7;
+    memcpy(array + 4, encoded, encoded_length);
+    midi_send_array(&midi_device, encoded_length + 5, array);
+
+    // SEND_STRING("\nTD: ");
+    // for (uint8_t i = 0; i < encoded_length + 5; i++) {
+    //     send_byte(array[i]);
+    //     SEND_STRING(" ");
+    // }
+}
+
+__attribute__ ((weak))
+bool sysex_process_quantum(uint8_t length, uint8_t * data) {
+    return sysex_process_keyboard(length, data);
+}
+
+__attribute__ ((weak))
+bool sysex_process_keyboard(uint8_t length, uint8_t * data) {
+    return sysex_process_user(length, data);
+}
+
+__attribute__ ((weak))
+bool sysex_process_user(uint8_t length, uint8_t * data) {
+    return true;
 }
 
 void sysex_buffer_callback(MidiDevice * device, uint8_t length, uint8_t * data) {
-    uint8_t * pointer_copy = data;
-
-    if (*data++ != 0xF0)
-        return
-    data++;
-    data++;
-    data++;
-    data++;
-
-    switch (*data++) {
-        case 0x13: ; // Get info from keyboard
-            switch (*data++) {
-                case 0x00: ; // Get layer state
-                    // send_dword(layer_state);
-                    uint8_t chunk[5];
-                    encode_4byte_chunk(layer_state | default_layer_state, &chunk);
-
-                    uint8_t array[] = {0xF0, 0x00, 0x00, 0x00, 0x00, chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], 0xF7};
-                    midi_send_array(&midi_device, 11, &array);
-                    // midi_send_data(device, 3, 0x00, layer_state >> 24 & 0x7f, layer_state >> 16 & 0x7f);
-                    // midi_send_data(device, 6, layer_state >> 8 & 0x7f, layer_state & 0x7f, 0xF7);
+    // SEND_STRING("\nRX: ");
+    // for (uint8_t i = 0; i < length; i++) {
+    //     send_byte(data[i]);
+    //     SEND_STRING(" ");
+    // }
+    if (!sysex_process_quantum(length, data))
+        return;
+
+    switch (data[0]) {
+        case MT_SET_DATA:
+            switch (data[1]) {
+                case DT_DEFAULT_LAYER: {
+                    eeconfig_update_default_layer(data[2]);
+                    default_layer_set((uint32_t)(data[2]));
                     break;
+                }
+                case DT_KEYMAP_OPTIONS: {
+                    eeconfig_update_keymap(data[2]);
+                    break;
+                }
+                case DT_RGBLIGHT: {
+                    #ifdef RGBLIGHT_ENABLE
+                        uint32_t rgblight = bytes_to_dword(data, 2);
+                        rgblight_update_dword(rgblight);
+                    #endif
+                    break;
+                }
             }
-        #ifdef RGBLIGHT_ENABLE
-        case 0x27: ; // RGB LED functions
-            switch (*data++) {
-                case 0x00: ; // Update HSV
-                    uint32_t chunk = decode_4byte_chunk(data);
-                    rgblight_sethsv(((chunk >> 16) & 0xFFFF) % 360, (chunk >> 8) & 0xFF, chunk & 0xFF);
+        case MT_GET_DATA:
+            switch (data[1]) {
+                case DT_HANDSHAKE: {
+                    MT_GET_DATA_ACK(DT_HANDSHAKE, NULL, 0);
+                    break;
+                }
+                case DT_DEBUG: {
+                    uint8_t debug_bytes[1] = { eeprom_read_byte(EECONFIG_DEBUG) };
+                    MT_GET_DATA_ACK(DT_DEBUG, debug_bytes, 1);
+                    break;
+                }
+                case DT_DEFAULT_LAYER: {
+                    uint8_t default_bytes[1] = { eeprom_read_byte(EECONFIG_DEFAULT_LAYER) };
+                    MT_GET_DATA_ACK(DT_DEFAULT_LAYER, default_bytes, 1);
+                    break;
+                }
+                case DT_CURRENT_LAYER: {
+                    uint8_t layer_state_bytes[4];
+                    dword_to_bytes(layer_state, layer_state_bytes);
+                    MT_GET_DATA_ACK(DT_CURRENT_LAYER, layer_state_bytes, 4);
+                    break;
+                }
+                case DT_AUDIO: {
+                    #ifdef AUDIO_ENABLE
+                        uint8_t audio_bytes[1] = { eeprom_read_byte(EECONFIG_AUDIO) };
+                        MT_GET_DATA_ACK(DT_AUDIO, audio_bytes, 1);
+                    #else
+                        MT_GET_DATA_ACK(DT_AUDIO, NULL, 0);
+                    #endif
+                    break;
+                }
+                case DT_BACKLIGHT: {
+                    #ifdef BACKLIGHT_ENABLE
+                        uint8_t backlight_bytes[1] = { eeprom_read_byte(EECONFIG_BACKLIGHT) };
+                        MT_GET_DATA_ACK(DT_BACKLIGHT, backlight_bytes, 1);
+                    #else
+                        MT_GET_DATA_ACK(DT_BACKLIGHT, NULL, 0);
+                    #endif
+                    break;
+                }
+                case DT_RGBLIGHT: {
+                    #ifdef RGBLIGHT_ENABLE
+                        uint8_t rgblight_bytes[4];
+                        dword_to_bytes(eeconfig_read_rgblight(), rgblight_bytes);
+                        MT_GET_DATA_ACK(DT_RGBLIGHT, rgblight_bytes, 4);
+                    #else
+                        MT_GET_DATA_ACK(DT_RGBLIGHT, NULL, 0);
+                    #endif
+                    break;
+                }
+                case DT_KEYMAP_OPTIONS: {
+                    uint8_t keymap_bytes[1] = { eeconfig_read_keymap() };
+                    MT_GET_DATA_ACK(DT_KEYMAP_OPTIONS, keymap_bytes, 1);
                     break;
-                case 0x01: ; // Update RGB
+                }
+                default:
                     break;
             }
             break;
-        #endif
-    }
-
-    // SEND_STRING("\nDATA:\n");
-    // while (*pointer_copy != 0xF7) {
-    //     send_byte(*pointer_copy++);
-    //     SEND_STRING(" ");
-    // }
-
-}
+        case MT_SET_DATA_ACK:
+        case MT_GET_DATA_ACK:
+            break;
+        case MT_SEND_DATA:
+            break;
+        case MT_SEND_DATA_ACK:
+            break;
+        case MT_EXE_ACTION:
+            break;
+        case MT_EXE_ACTION_ACK:
+            break;
+        case MT_TYPE_ERROR:
+            break;
+        default: ; // command not recognised
+            send_bytes_sysex(MT_TYPE_ERROR, DT_NONE, data, length);
+            break;
 
-void send_unicode_midi(uint32_t unicode) {
-    uint8_t chunk[5];
-    encode_4byte_chunk(unicode, &chunk);
+        // #ifdef RGBLIGHT_ENABLE
+        // case 0x27: ; // RGB LED functions
+        //     switch (*data++) {
+        //         case 0x00: ; // Update HSV
+        //             rgblight_sethsv((data[0] << 8 | data[1]) % 360, data[2], data[3]);
+        //             break;
+        //         case 0x01: ; // Update RGB
+        //             break;
+        //         case 0x02: ; // Update mode
+        //             rgblight_mode(data[0]);
+        //             break;
+        //     }
+        //     break;
+        // #endif
+    }
 
-    uint8_t array[] = {0xF0, 0x00, 0x00, 0x00, 0x05, chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], 0xF7};
-    midi_send_array(&midi_device, 11, &array);
 }
 
 #endif