void usb_send_func(MidiDevice * device, uint8_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { MIDI_EventPacket_t event; event.CableNumber = 0; event.Data1 = byte0; event.Data2 = byte1; event.Data3 = byte2; //if the length is undefined we assume it is a SYSEX message if (midi_packet_length(byte0) == UNDEFINED) { switch(cnt) { case 3: if (byte2 == SYSEX_END) event.Command = SYSEX_ENDS_IN_3; else event.Command = SYSEX_START_OR_CONT; break; case 2: if (byte1 == SYSEX_END) event.Command = SYSEX_ENDS_IN_2; else event.Command = SYSEX_START_OR_CONT; break; case 1: if (byte0 == SYSEX_END) event.Command = SYSEX_ENDS_IN_1; else event.Command = SYSEX_START_OR_CONT; break; default: return; //invalid cnt } } else { //deal with 'system common' messages //TODO are there any more? switch(byte0 & 0xF0){ case MIDI_SONGPOSITION: event.Command = SYS_COMMON_3; break; case MIDI_SONGSELECT: case MIDI_TC_QUATERFRAME: event.Command = SYS_COMMON_2; break; default: event.Command = byte0 >> 4; break; } } MIDI_Device_SendEventPacket(&USB_MIDI_Interface, &event); MIDI_Device_Flush(&USB_MIDI_Interface); MIDI_Device_USBTask(&USB_MIDI_Interface); USB_USBTask(); }
void usb_get_midi(MidiDevice * device) { /* Select the MIDI OUT stream */ Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPNUM); MIDI_EventPacket_t event; while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) { midi_packet_length_t length = midi_packet_length(event.Data1); //pass the data to the device input function //not dealing with sysex yet if (length != UNDEFINED) midi_device_input(device, length, event.Data1, event.Data2, event.Data3); } MIDI_Device_USBTask(&USB_MIDI_Interface); USB_USBTask(); }
void usb_get_midi(MidiDevice * device) { MIDI_EventPacket_t event; while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) { midi_packet_length_t length = midi_packet_length(event.Data1); //pass the data to the device input function //not dealing with sysex yet if (length != UNDEFINED) { uint8_t input[3]; input[0] = event.Data1; input[1] = event.Data2; input[2] = event.Data3; midi_device_input(device, length, input); } } MIDI_Device_USBTask(&USB_MIDI_Interface); USB_USBTask(); }
void midi_process_byte(MidiDevice * device, uint8_t input) { if (midi_is_realtime(input)) { //call callback, store and restore state input_state_t state = device->input_state; device->input_state = ONE_BYTE_MESSAGE; midi_input_callbacks(device, 1, input, 0, 0); device->input_state = state; } else if (midi_is_statusbyte(input)) { //store the byte if (device->input_state != SYSEX_MESSAGE) { device->input_buffer[0] = input; device->input_count = 1; } switch (midi_packet_length(input)) { case ONE: device->input_state = ONE_BYTE_MESSAGE;; midi_input_callbacks(device, 1, input, 0, 0); device->input_state = IDLE; break; case TWO: device->input_state = TWO_BYTE_MESSAGE; break; case THREE: device->input_state = THREE_BYTE_MESSAGE; break; case UNDEFINED: switch(input) { case SYSEX_BEGIN: device->input_state = SYSEX_MESSAGE; device->input_buffer[0] = input; device->input_count = 1; break; case SYSEX_END: //send what is left in the input buffer, set idle device->input_buffer[device->input_count % 3] = input; device->input_count += 1; //call the callback midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); device->input_state = IDLE; break; default: device->input_state = IDLE; device->input_count = 0; } break; default: device->input_state = IDLE; device->input_count = 0; break; } } else { if (device->input_state != IDLE) { //store the byte device->input_buffer[device->input_count % 3] = input; //increment count uint16_t prev = device->input_count; device->input_count += 1; switch(prev % 3) { case 2: //call callback midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); if (device->input_state != SYSEX_MESSAGE) { //set to 1, keeping status byte, allowing for running status device->input_count = 1; } break; case 1: if (device->input_state == TWO_BYTE_MESSAGE) { //call callback midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], 0); if (device->input_state != SYSEX_MESSAGE) { //set to 1, keeping status byte, allowing for running status device->input_count = 1; } } break; case 0: default: //one byte messages are dealt with directly break; } } } }