#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;
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 */
};
#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
#endif
}
+
+
#ifdef CONSOLE_ENABLE
static bool console_flush = false;
#define CONSOLE_FLUSH_SET(b) do { \
Console_Task();
console_flush = false;
}
+
#endif
/** Event handler for the USB_ConfigurationChanged event.
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,
******************************************************************************/
#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;
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)) {
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();
}
// MIDI_Task();
#endif
-#ifdef RGBLIGHT_ENABLE
+#if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
rgblight_task();
#endif
CDC_Device_USBTask(&cdc_device);
#endif
+#ifdef RAW_ENABLE
+ raw_hid_task();
+#endif
+
#if !defined(INTERRUPT_CONTROL_ENDPOINT)
USB_USBTask();
#endif
+
}
}
#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
-}
-
-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) {
- // for (int i = 0; i < length; i++)
- // midi_send_cc(device, 15, 0x7F & data[i], 0x7F & (start + i));
- // if (start == 0x27) {
+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)
- sysex_buffer_callback(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++;
}
- // }
-
+ #endif
}
-void sysex_buffer_callback(uint8_t length, uint8_t * data) {
- uint8_t * pointer_copy = data;
-
- if (*data++ != 0xF0)
- return
- data++;
- data++;
- data++;
- data++;
-
- switch (*data++) {
- case 0x27: ; // RGB LED functions
- switch (*data++)
- case 0x00: ; // Update HSV
- uint32_t part1 = *data++;
- uint32_t part2 = *data++;
- uint32_t part3 = *data++;
- uint32_t part4 = *data++;
- uint32_t part5 = *data++;
- uint32_t chunk = ((part1 & 0x1FUL) << 28) | (part2 << 21) | (part3 << 14) | (part4 << 7) | part5;
- // SEND_STRING("\nCHUNK: ");
- // send_dword(chunk);
- rgblight_sethsv(((chunk >> 16) & 0xFFFF) % 360, (chunk >> 8) & 0xFF, chunk & 0xFF);
- // SEND_STRING("\nHUE: ");
- // send_word(((chunk >> 16) & 0xFFFF) % 360);
- // SEND_STRING("\nSAT: ");
- // send_word((chunk >> 8) & 0xFF);
- // SEND_STRING("\nVAL: ");
- // send_word(chunk & 0xFF);
- break;
- case 0x01: ; // Update RGB
- break;
- break;
- }
-
- // SEND_STRING("\nDATA:\n");
- // while (*pointer_copy != 0xF7) {
- // send_byte(*pointer_copy++);
- // SEND_STRING(" ");
- // }
-
-}
#endif