|
-
-
- #include <Arduino.h>
- #include "USBHost_t36.h"
-
- #define print USBHost::print_
- #define println USBHost::println_
-
- void MIDIDevice::init()
- {
- contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
- contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
- contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
- handleNoteOff = NULL;
- handleNoteOn = NULL;
- handleVelocityChange = NULL;
- handleControlChange = NULL;
- handleProgramChange = NULL;
- handleAfterTouch = NULL;
- handlePitchChange = NULL;
- handleSysExPartial = NULL;
- handleSysExComplete = NULL;
- handleTimeCodeQuarterFrame = NULL;
- handleSongPosition = NULL;
- handleSongSelect = NULL;
- handleTuneRequest = NULL;
- handleClock = NULL;
- handleStart = NULL;
- handleContinue = NULL;
- handleStop = NULL;
- handleActiveSensing = NULL;
- handleSystemReset = NULL;
- handleRealTimeSystem = NULL;
- rx_head = 0;
- rx_tail = 0;
- rxpipe = NULL;
- txpipe = NULL;
- driver_ready_for_device(this);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bool MIDIDevice::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
- {
-
- if (type != 1) return false;
- println("MIDIDevice claim this=", (uint32_t)this, HEX);
- println("len = ", len);
-
- const uint8_t *p = descriptors;
- const uint8_t *end = p + len;
-
- if (p[0] != 9 || p[1] != 4) return false;
-
-
- if (p[5] != 1) return false;
- if (p[6] != 3) return false;
- p += 9;
- println(" Interface is MIDI");
- rx_ep = 0;
- tx_ep = 0;
-
- while (p < end) {
- len = *p;
- if (len < 4) return false;
- if (p + len > end) return false;
- uint32_t type = p[1];
- print("type: ", type);
- println(", len: ", len);
- if (type == 4 || type == 11) break;
- if (type == 0x24) {
- uint32_t subtype = p[2];
-
- if (subtype == 1) {
-
- println(" MIDI Header (ignored)");
- } else if (subtype == 2) {
-
- println(" MIDI IN Jack (ignored)");
- } else if (subtype == 3) {
-
- println(" MIDI OUT Jack (ignored)");
- } else if (subtype == 4) {
-
- println(" MIDI Element (ignored)");
- } else {
- return false;
- }
- } else if (type == 5) {
-
- if (p[0] < 7) return false;
- if (p[3] != 2 && p[3] != 3) return false;
- println(" MIDI Endpoint: ", p[2], HEX);
- switch (p[2] & 0xF0) {
- case 0x80:
-
- if (rx_ep == 0) {
- rx_ep = p[2] & 0x0F;
- rx_ep_type = p[3];
- rx_size = p[4] | (p[5] << 8);
- println(" rx_size = ", rx_size);
- }
- break;
- case 0x00:
-
- if (tx_ep == 0) {
- tx_ep = p[2];
- tx_ep_type = p[3];
- tx_size = p[4] | (p[5] << 8);
- println(" tx_size = ", tx_size);
- }
- break;
- default:
- return false;
- }
- } else if (type == 37) {
-
- println(" MIDI Endpoint Jack Association (ignored)");
- } else {
- return false;
- }
- p += len;
- }
-
- if (rx_ep && rx_size <= MAX_PACKET_SIZE) {
- rxpipe = new_Pipe(dev, rx_ep_type, rx_ep, 1, rx_size);
- if (rxpipe) {
- rxpipe->callback_function = rx_callback;
- queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
- rx_packet_queued = true;
- }
- } else {
- rxpipe = NULL;
- }
-
- if (tx_ep && tx_size <= MAX_PACKET_SIZE) {
- txpipe = new_Pipe(dev, tx_ep_type, tx_ep, 0, tx_size);
- if (txpipe) {
- txpipe->callback_function = tx_callback;
- tx1_count = 0;
- tx2_count = 0;
- }
- } else {
- txpipe = NULL;
- }
- rx_head = 0;
- rx_tail = 0;
- msg_channel = 0;
- msg_type = 0;
- msg_data1 = 0;
- msg_data2 = 0;
- msg_sysex_len = 0;
-
- return (rxpipe || txpipe);
- }
-
- void MIDIDevice::rx_callback(const Transfer_t *transfer)
- {
- if (transfer->driver) {
- ((MIDIDevice *)(transfer->driver))->rx_data(transfer);
- }
- }
-
- void MIDIDevice::tx_callback(const Transfer_t *transfer)
- {
- if (transfer->driver) {
- ((MIDIDevice *)(transfer->driver))->tx_data(transfer);
- }
- }
-
- void MIDIDevice::rx_data(const Transfer_t *transfer)
- {
- println("MIDIDevice Receive");
- print(" MIDI Data: ");
- uint32_t len = (transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF)) >> 2;
- print_hexbytes(transfer->buffer, len * 4);
- uint32_t head = rx_head;
- uint32_t tail = rx_tail;
- for (uint32_t i=0; i < len; i++) {
- uint32_t msg = rx_buffer[i];
- if (msg) {
- if (++head >= RX_QUEUE_SIZE) head = 0;
- rx_queue[head] = msg;
- }
- }
- rx_head = head;
- rx_tail = tail;
- uint32_t avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
-
- println("avail = ", avail);
- if (avail >= (uint32_t)(rx_size>>2)) {
-
- println("queue another receive packet");
- queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
- rx_packet_queued = true;
- } else {
-
-
- println("wait to receive more packets");
- rx_packet_queued = false;
- }
- }
-
- void MIDIDevice::tx_data(const Transfer_t *transfer)
- {
- println("MIDIDevice transmit complete");
- print(" MIDI Data: ");
- print_hexbytes(transfer->buffer, tx_size);
- if (transfer->buffer == tx_buffer1) {
- tx1_count = 0;
- } else if (transfer->buffer == tx_buffer2) {
- tx2_count = 0;
- }
- }
-
-
- void MIDIDevice::disconnect()
- {
-
-
-
- rxpipe = NULL;
- txpipe = NULL;
- }
-
-
- void MIDIDevice::write_packed(uint32_t data)
- {
- if (!txpipe) return;
- uint32_t tx_max = tx_size / 4;
- while (1) {
- uint32_t tx1 = tx1_count;
- uint32_t tx2 = tx2_count;
- if (tx1 < tx_max && (tx2 == 0 || tx2 >= tx_max)) {
-
- tx_buffer1[tx1++] = data;
- tx1_count = tx1;
- if (tx1 >= tx_max) {
- queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
- } else {
-
-
- tx1_count = tx_max;
- queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
- }
- return;
- }
- if (tx2 < tx_max) {
-
- tx_buffer2[tx2++] = data;
- tx2_count = tx2;
- if (tx2 >= tx_max) {
- queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
- } else {
-
-
- tx2_count = tx_max;
- queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
- }
- return;
- }
- }
- }
-
- void MIDIDevice::send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable)
- {
- cable = (cable & 0x0F) << 4;
- while (length > 3) {
- write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
- data += 3;
- length -= 3;
- }
- if (length == 3) {
- write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
- } else if (length == 2) {
- write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
- } else if (length == 1) {
- write_packed(0x05 | cable | (data[0] << 8));
- }
- }
-
- void MIDIDevice::send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable)
- {
- cable = (cable & 0x0F) << 4;
-
- if (length == 0) {
- write_packed(0x06 | cable | (0xF0 << 8) | (0xF7 << 16));
- return;
- } else if (length == 1) {
- write_packed(0x07 | cable | (0xF0 << 8) | (data[0] << 16) | (0xF7 << 24));
- return;
- } else {
- write_packed(0x04 | cable | (0xF0 << 8) | (data[0] << 16) | (data[1] << 24));
- data += 2;
- length -= 2;
- }
- while (length >= 3) {
- write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
- data += 3;
- length -= 3;
- }
- if (length == 2) {
- write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (0xF7 << 24));
- } else if (length == 1) {
- write_packed(0x06 | cable | (data[0] << 8) | (0xF7 << 16));
- } else {
- write_packed(0x05 | cable | (0xF7 << 8));
- }
- }
-
-
-
-
- bool MIDIDevice::read(uint8_t channel)
- {
- uint32_t n, head, tail, avail, ch, type1, type2, b1;
-
- head = rx_head;
- tail = rx_tail;
- if (head == tail) return false;
- if (++tail >= RX_QUEUE_SIZE) tail = 0;
- n = rx_queue[tail];
- rx_tail = tail;
- if (!rx_packet_queued && rxpipe) {
- avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
- if (avail >= (uint32_t)(rx_size>>2)) {
- __disable_irq();
- queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
- __enable_irq();
- }
- }
- println("read: ", n, HEX);
-
- type1 = n & 15;
- type2 = (n >> 12) & 15;
- b1 = (n >> 8) & 0xFF;
- ch = (b1 & 15) + 1;
- msg_cable = (n >> 4) & 15;
- if (type1 >= 0x08 && type1 <= 0x0E) {
- if (channel && channel != ch) {
-
- return false;
- }
- if (type1 == 0x08 && type2 == 0x08) {
- msg_type = 0x80;
- if (handleNoteOff) {
- (*handleNoteOff)(ch, (n >> 16), (n >> 24));
- }
- } else
- if (type1 == 0x09 && type2 == 0x09) {
- if ((n >> 24) > 0) {
- msg_type = 0x90;
- if (handleNoteOn) {
- (*handleNoteOn)(ch, (n >> 16), (n >> 24));
- }
- } else {
- msg_type = 0x80;
- if (handleNoteOff) {
- (*handleNoteOff)(ch, (n >> 16), (n >> 24));
- }
- }
- } else
- if (type1 == 0x0A && type2 == 0x0A) {
- msg_type = 0xA0;
- if (handleVelocityChange) {
- (*handleVelocityChange)(ch, (n >> 16), (n >> 24));
- }
- } else
- if (type1 == 0x0B && type2 == 0x0B) {
- msg_type = 0xB0;
- if (handleControlChange) {
- (*handleControlChange)(ch, (n >> 16), (n >> 24));
- }
- } else
- if (type1 == 0x0C && type2 == 0x0C) {
- msg_type = 0xC0;
- if (handleProgramChange) {
- (*handleProgramChange)(ch, (n >> 16));
- }
- } else
- if (type1 == 0x0D && type2 == 0x0D) {
- msg_type = 0xD0;
- if (handleAfterTouch) {
- (*handleAfterTouch)(ch, (n >> 16));
- }
- } else
- if (type1 == 0x0E && type2 == 0x0E) {
- msg_type = 0xE0;
- if (handlePitchChange) {
- int value = ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80);
- value -= 8192;
- (*handlePitchChange)(ch, value);
- }
- } else {
- return false;
- }
- return_message:
- msg_channel = ch;
- msg_data1 = (n >> 16);
- msg_data2 = (n >> 24);
- return true;
- }
- if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && b1 >= 0xF1 && b1 != 0xF7)) {
-
- system_common_or_realtime:
- switch (b1) {
- case 0xF1:
- if (handleTimeCodeQuarterFrame) {
- (*handleTimeCodeQuarterFrame)(n >> 16);
- }
- break;
- case 0xF2:
- if (handleSongPosition) {
- (*handleSongPosition)(((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
- }
- break;
- case 0xF3:
- if (handleSongSelect) {
- (*handleSongSelect)(n >> 16);
- }
- break;
- case 0xF6:
- if (handleTuneRequest) {
- (*handleTuneRequest)();
- }
- break;
- case 0xF8:
- if (handleClock) {
- (*handleClock)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xF8);
- }
- break;
- case 0xFA:
- if (handleStart) {
- (*handleStart)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFA);
- }
- break;
- case 0xFB:
- if (handleContinue) {
- (*handleContinue)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFB);
- }
- break;
- case 0xFC:
- if (handleStop) {
- (*handleStop)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFC);
- }
- break;
- case 0xFE:
- if (handleActiveSensing) {
- (*handleActiveSensing)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFE);
- }
- break;
- case 0xFF:
- if (handleSystemReset) {
- (*handleSystemReset)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFF);
- }
- break;
- default:
- return false;
- }
- msg_type = b1;
- goto return_message;
- }
- if (type1 == 0x04) {
- sysex_byte(n >> 8);
- sysex_byte(n >> 16);
- sysex_byte(n >> 24);
- return false;
- }
- if (type1 >= 0x05 && type1 <= 0x07) {
- sysex_byte(b1);
-
-
- if (type1 == 0x05 && b1 != 0xF7) return false;
- if (type1 >= 0x06) sysex_byte(n >> 16);
- if (type1 == 0x07) sysex_byte(n >> 24);
- uint16_t len = msg_sysex_len;
- msg_data1 = len;
- msg_data2 = len >> 8;
- msg_sysex_len = 0;
- msg_type = 0xF0;
- if (handleSysExPartial) {
- (*handleSysExPartial)(msg_sysex, len, 1);
- } else if (handleSysExComplete) {
- (*handleSysExComplete)(msg_sysex, len);
- }
- return true;
- }
- if (type1 == 0x0F) {
- if (b1 >= 0xF8) {
- goto system_common_or_realtime;
- }
- if (b1 == 0xF0 || msg_sysex_len > 0) {
-
- sysex_byte(b1);
- }
- }
- return false;
- }
-
- void MIDIDevice::sysex_byte(uint8_t b)
- {
- if (handleSysExPartial && msg_sysex_len >= SYSEX_MAX_LEN) {
-
- (*handleSysExPartial)(msg_sysex, msg_sysex_len, 0);
- msg_sysex_len = 0;
- }
- if (msg_sysex_len < SYSEX_MAX_LEN) {
- msg_sysex[msg_sysex_len++] = b;
- }
- }
-
-
-
|