]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - tmk_core/protocol/lufa/lufa.c
Initial version of Raw HID interface
[qmk_firmware.git] / tmk_core / protocol / lufa / lufa.c
index a33a16599e2860112b71ebd06a3275db74ab95b1..aeb5f07815378f1da7f37c0f38080cabad30621d 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
+
+#ifdef RAW_ENABLE
+       #include "raw_hid.h"
+#endif
+
 uint8_t keyboard_idle = 0;
 /* 0: Boot Protocol, 1: Report Protocol(default) */
 uint8_t keyboard_protocol = 1;
@@ -171,6 +179,80 @@ USB_ClassInfo_CDC_Device_t cdc_device =
 };
 #endif
 
+#ifdef RAW_ENABLE
+
+void raw_hid_send( uint8_t *data, uint8_t length )
+{
+       // TODO: implement variable size packet
+       if ( length != RAW_EPSIZE )
+       {
+               return;
+       }
+
+       if (USB_DeviceState != DEVICE_STATE_Configured)
+       {
+               return;
+       }
+
+       // TODO: decide if we allow calls to raw_hid_send() in the middle
+       // of other endpoint usage.
+       uint8_t ep = Endpoint_GetCurrentEndpoint();
+
+       Endpoint_SelectEndpoint(RAW_IN_EPNUM);
+
+       // Check to see if the host is ready to accept another packet
+       if (Endpoint_IsINReady())
+       {
+               // Write data
+               Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
+               // Finalize the stream transfer to send the last packet
+               Endpoint_ClearIN();
+       }
+
+       Endpoint_SelectEndpoint(ep);
+}
+
+__attribute__ ((weak))
+void raw_hid_receive( uint8_t *data, uint8_t length )
+{
+       // Users should #include "raw_hid.h" in their own code
+       // and implement this function there. Leave this as weak linkage
+       // so users can opt to not handle data coming in.
+}
+
+static void raw_hid_task(void)
+{
+       // Create a temporary buffer to hold the read in data from the host
+       uint8_t data[RAW_EPSIZE];
+       bool data_read = false;
+
+       // Device must be connected and configured for the task to run
+       if (USB_DeviceState != DEVICE_STATE_Configured)
+       return;
+
+       Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
+
+       // Check to see if a packet has been sent from the host
+       if (Endpoint_IsOUTReceived())
+       {
+               // Check to see if the packet contains data
+               if (Endpoint_IsReadWriteAllowed())
+               {
+                       /* Read data */
+                       Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
+                       data_read = true;
+               }
+
+               // Finalize the stream transfer to receive the last packet
+               Endpoint_ClearOUT();
+
+               if ( data_read )
+               {
+                       raw_hid_receive( data, sizeof(data) );
+               }
+       }
+}
+#endif
 
 /*******************************************************************************
  * Console
@@ -290,6 +372,8 @@ void EVENT_USB_Device_WakeUp()
 #endif
 }
 
+
+
 #ifdef CONSOLE_ENABLE
 static bool console_flush = false;
 #define CONSOLE_FLUSH_SET(b)   do { \
@@ -307,6 +391,7 @@ void EVENT_USB_Device_StartOfFrame(void)
     Console_Task();
     console_flush = false;
 }
+
 #endif
 
 /** Event handler for the USB_ConfigurationChanged event.
@@ -335,6 +420,14 @@ void EVENT_USB_Device_ConfigurationChanged(void)
                                      EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
 #endif
 
+#ifdef RAW_ENABLE
+    /* Setup Raw HID Report Endpoints */
+    ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
+                                                                        RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
+    ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
+                                                                        RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
+#endif
+
 #ifdef CONSOLE_ENABLE
     /* Setup Console HID Report Endpoints */
     ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
@@ -1051,7 +1144,7 @@ int main(void)
         // MIDI_Task();
 #endif
         
-#ifdef RGBLIGHT_ANIMATIONS
+#if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
         rgblight_task();
 #endif
 
@@ -1060,9 +1153,14 @@ int main(void)
         CDC_Device_USBTask(&cdc_device);
 #endif
 
+#ifdef RAW_ENABLE
+        raw_hid_task();
+#endif
+
 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
         USB_USBTask();
 #endif
+
     }
 }
 
@@ -1087,196 +1185,38 @@ void fallthrough_callback(MidiDevice * device,
 #endif
 }
 
-#ifdef RGB_MIDI
-    rgblight_config_t rgblight_config;
-#endif
 
 void cc_callback(MidiDevice * device,
     uint8_t chan, uint8_t num, uint8_t val) {
   //sending it back on the next channel
   // midi_send_cc(device, (chan + 1) % 16, num, val);
-    #ifdef RGB_MIDI
-        rgblight_config.raw = eeconfig_read_rgblight();
-        switch (num) {
-            case 14:
-                rgblight_config.hue = val * 360 / 127;
-            break;
-            case 15:
-                rgblight_config.sat = val << 1;
-            break;
-            case 16:
-                rgblight_config.val = val << 1;
-            break;
-        }
-        rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
-    #endif
 }
 
 uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0};
 
 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) {
+    #ifdef API_SYSEX_ENABLE
         // SEND_STRING("\n");
         // send_word(start);
         // SEND_STRING(": ");
         for (uint8_t place = 0; place < length; place++) {
             // send_byte(*data);
             midi_buffer[start + place] = *data;
-            if (*data == 0xF7 && midi_buffer[0] == 0xF0)
-                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);
+                process_api(decode_length, decoded);
+            }
             // SEND_STRING(" ");
             data++;
         }
-    // }
-
-}
-
-uint32_t decode_uint32_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;
-}
-
-uint32_t decode_uint8_chunk(uint8_t * data) {
-    uint32_t part4 = *data++;
-    uint32_t part5 = *data++;
-    return (part4 << 7) | part5;
-}
-
-void encode_uint32_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;
-}
-
-void encode_uint8_chunk(uint8_t data, uint8_t * pointer) {
-    *pointer++ = (data >> 7) & 0x7F;
-    *pointer++ = (data) & 0x7F;
-}
-
-void sysex_buffer_callback(MidiDevice * device, uint8_t length, uint8_t * data) {
-    // uint8_t * pointer_copy = data; // use for debugging
-
-    //data++; // i'm 98% sure there's a better way to do this
-    data++;
-    data++;
-    data++;
-    data++;
-
-    switch (*data++) {
-        case 0x07: ; // Quantum action
-            break;
-        case 0x08: ; // Keyboard acion
-            break;
-        case 0x09: ; // User action
-            break;
-        case 0x12: ; // Set info on keyboard
-            switch (*data++) {
-                case 0x02: ; // set default layer
-                    uint8_t default_layer = decode_uint8_chunk(data);
-                    eeconfig_update_default_layer(default_layer);
-                    default_layer_set((uint32_t)default_layer);
-                    break;
-                case 0x08: ; // set keymap options
-                    uint8_t keymap_options = decode_uint8_chunk(data);
-                    eeconfig_update_keymap(keymap_options);
-                    break;
-            }
-            break;
-        case 0x13: ; // Get info from keyboard
-            switch (*data++) {
-                case 0x00: ; // Handshake
-                    send_bytes_sysex(0x00, NULL, 0);
-                    break;
-                case 0x01: ; // Get debug state
-                    uint8_t debug[2];
-                    encode_uint8_chunk(eeprom_read_byte(EECONFIG_DEBUG), debug);
-                    send_bytes_sysex(0x01, debug, 2);
-                    break;
-                case 0x02: ; // Get default layer
-                    uint8_t default_layer[2];
-                    encode_uint8_chunk(eeprom_read_byte(EECONFIG_DEFAULT_LAYER), default_layer);
-                    send_bytes_sysex(0x02, default_layer, 2);
-                    break;
-                #ifdef AUDIO_ENABLE
-                case 0x03: ; // Get backlight state
-                    uint8_t audio[2];
-                    encode_uint8_chunk(eeprom_read_byte(EECONFIG_AUDIO), audio);
-                    send_bytes_sysex(0x03, audio, 2);
-                #endif
-                case 0x04: ; // Get layer state
-                    uint8_t layers[5];
-                    encode_uint32_chunk(layer_state, layers);
-                    send_bytes_sysex(0x04, layers, 5);
-                    break;
-                #ifdef BACKLIGHT_ENABLE
-                case 0x06: ; // Get backlight state
-                    uint8_t backlight[2];
-                    encode_uint8_chunk(eeprom_read_byte(EECONFIG_BACKLIGHT), backlight);
-                    send_bytes_sysex(0x06, backlight, 2);
-                #endif
-                #ifdef RGBLIGHT_ENABLE
-                case 0x07: ; // Get rgblight state
-                    uint8_t rgblight[2];
-                    encode_uint32_chunk(eeprom_read_dword(EECONFIG_RGBLIGHT), rgblight);
-                    send_bytes_sysex(0x07, rgblight, 5);
-                #endif
-                case 0x08: ; // Keymap options
-                    uint8_t keymap_options[2];
-                    encode_uint8_chunk(eeconfig_read_keymap(), keymap_options);
-                    send_bytes_sysex(0x08, keymap_options, 2);
-                    break;
-            }
-            break;
-        #ifdef RGBLIGHT_ENABLE
-        case 0x27: ; // RGB LED functions
-            switch (*data++) {
-                case 0x00: ; // Update HSV
-                    uint32_t hsv = decode_uint32_chunk(data);
-                    rgblight_sethsv(((hsv >> 16) & 0xFFFF) % 360, (hsv >> 8) & 0xFF, hsv & 0xFF);
-                    break;
-                case 0x01: ; // Update RGB
-                    break;
-                case 0x02: ; // Update mode
-                    uint8_t rgb_mode = decode_uint8_chunk(data);
-                    rgblight_mode(rgb_mode);
-                    break;
-            }
-            break;
-        #endif
-    }
-
-    // SEND_STRING("\nDATA:\n");
-    // while (*pointer_copy != 0xF7) {
-    //     send_byte(*pointer_copy++);
-    //     SEND_STRING(" ");
-    // }
-
-}
-
-void send_unicode_midi(uint32_t unicode) {
-    uint8_t chunk[5];
-    encode_uint32_chunk(unicode, chunk);
-    send_bytes_sysex(0x05, chunk, 5);
+    #endif
 }
 
-void send_bytes_sysex(uint8_t type, uint8_t * bytes, uint8_t length) {
-    uint8_t * array = malloc(sizeof(uint8_t) * (length + 6));
-    array[0] = 0xF0;
-    array[1] = 0x00;
-    array[2] = 0x00;
-    array[3] = 0x00;
-    array[4] = type;
-    array[length + 5] = 0xF7;
-    memcpy(array + 5, bytes, length);
-    midi_send_array(&midi_device, length + 6, array);
-}
 
 #endif