]> 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 9b201374a2518e24c9155f240fffc59134f07214..aeb5f07815378f1da7f37c0f38080cabad30621d 100644 (file)
 
 #include "descriptor.h"
 #include "lufa.h"
+#include "quantum.h"
+
+#ifdef NKRO_ENABLE
+  #include "keycode_config.h"
+
+  extern keymap_config_t keymap_config;
+#endif
+
 
 #ifdef AUDIO_ENABLE
     #include <audio.h>
     #include "virtser.h"
 #endif
 
+#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;
@@ -72,9 +92,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 */
@@ -159,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
@@ -278,6 +372,8 @@ void EVENT_USB_Device_WakeUp()
 #endif
 }
 
+
+
 #ifdef CONSOLE_ENABLE
 static bool console_flush = false;
 #define CONSOLE_FLUSH_SET(b)   do { \
@@ -295,6 +391,7 @@ void EVENT_USB_Device_StartOfFrame(void)
     Console_Task();
     console_flush = false;
 }
+
 #endif
 
 /** Event handler for the USB_ConfigurationChanged event.
@@ -323,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,
@@ -502,7 +607,7 @@ static void send_keyboard(report_keyboard_t *report)
 
     /* Select the Keyboard Report Endpoint */
 #ifdef NKRO_ENABLE
-    if (keyboard_protocol && keyboard_nkro) {
+    if (keyboard_protocol && keymap_config.nkro) {
         /* Report protocol - NKRO */
         Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
 
@@ -702,7 +807,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;
@@ -762,7 +867,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)) {
 
@@ -792,12 +897,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();
 }
 
@@ -1032,20 +1137,30 @@ int main(void)
         }
         #endif
 
+        keyboard_task();
+
 #ifdef MIDI_ENABLE
         midi_device_process(&midi_device);
         // MIDI_Task();
 #endif
-        keyboard_task();
+        
+#if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
+        rgblight_task();
+#endif
 
 #ifdef VIRTSER_ENABLE
         virtser_task();
         CDC_Device_USBTask(&cdc_device);
 #endif
 
+#ifdef RAW_ENABLE
+        raw_hid_task();
+#endif
+
 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
         USB_USBTask();
 #endif
+
     }
 }
 
@@ -1070,15 +1185,38 @@ void fallthrough_callback(MidiDevice * device,
 #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);
+  // midi_send_cc(device, (chan + 1) % 16, num, val);
 }
 
-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));
+uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0};
+
+void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) {
+    #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) {
+                // 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++;
+        }
+    #endif
 }
+
+
 #endif