1
0
mirror of https://github.com/gdsports/USBHost_t36 synced 2024-11-24 01:52:23 -05:00

Add public API for MIDI driver

This commit is contained in:
PaulStoffregen 2017-03-07 05:42:29 -08:00
parent 3355eab2a2
commit 650ff7eeec
3 changed files with 331 additions and 11 deletions

View File

@ -500,8 +500,102 @@ private:
class MIDIDevice : public USBDriver { class MIDIDevice : public USBDriver {
public: public:
enum { SYSEX_MAX_LEN = 60 };
MIDIDevice(USBHost &host) { init(); } MIDIDevice(USBHost &host) { init(); }
MIDIDevice(USBHost *host) { init(); } MIDIDevice(USBHost *host) { init(); }
bool read(uint8_t channel=0, uint8_t cable=0);
uint8_t getType(void) {
return msg_type;
};
uint8_t getChannel(void) {
return msg_channel;
};
uint8_t getData1(void) {
return msg_data1;
};
uint8_t getData2(void) {
return msg_data2;
};
void setHandleNoteOff(void (*f)(uint8_t channel, uint8_t note, uint8_t velocity)) {
handleNoteOff = f;
};
void setHandleNoteOn(void (*f)(uint8_t channel, uint8_t note, uint8_t velocity)) {
handleNoteOn = f;
};
void setHandleVelocityChange(void (*f)(uint8_t channel, uint8_t note, uint8_t velocity)) {
handleVelocityChange = f;
};
void setHandleControlChange(void (*f)(uint8_t channel, uint8_t control, uint8_t value)) {
handleControlChange = f;
};
void setHandleProgramChange(void (*f)(uint8_t channel, uint8_t program)) {
handleProgramChange = f;
};
void setHandleAfterTouch(void (*f)(uint8_t channel, uint8_t pressure)) {
handleAfterTouch = f;
};
void setHandlePitchChange(void (*f)(uint8_t channel, int pitch)) {
handlePitchChange = f;
};
void setHandleSysEx(void (*f)(const uint8_t *data, uint16_t length, bool complete)) {
handleSysEx = (void (*)(const uint8_t *, uint16_t, uint8_t))f;
}
void setHandleRealTimeSystem(void (*f)(uint8_t realtimebyte)) {
handleRealTimeSystem = f;
};
void setHandleTimeCodeQuarterFrame(void (*f)(uint16_t data)) {
handleTimeCodeQuarterFrame = f;
};
void sendNoteOff(uint32_t note, uint32_t velocity, uint32_t channel) {
write_packed(0x8008 | (((channel - 1) & 0x0F) << 8)
| ((note & 0x7F) << 16) | ((velocity & 0x7F) << 24));
}
void sendNoteOn(uint32_t note, uint32_t velocity, uint32_t channel) {
write_packed(0x9009 | (((channel - 1) & 0x0F) << 8)
| ((note & 0x7F) << 16) | ((velocity & 0x7F) << 24));
}
void sendPolyPressure(uint32_t note, uint32_t pressure, uint32_t channel) {
write_packed(0xA00A | (((channel - 1) & 0x0F) << 8)
| ((note & 0x7F) << 16) | ((pressure & 0x7F) << 24));
}
void sendControlChange(uint32_t control, uint32_t value, uint32_t channel) {
write_packed(0xB00B | (((channel - 1) & 0x0F) << 8)
| ((control & 0x7F) << 16) | ((value & 0x7F) << 24));
}
void sendProgramChange(uint32_t program, uint32_t channel) {
write_packed(0xC00C | (((channel - 1) & 0x0F) << 8)
| ((program & 0x7F) << 16));
}
void sendAfterTouch(uint32_t pressure, uint32_t channel) {
write_packed(0xD00D | (((channel - 1) & 0x0F) << 8)
| ((pressure & 0x7F) << 16));
}
void sendPitchBend(uint32_t value, uint32_t channel) {
write_packed(0xE00E | (((channel - 1) & 0x0F) << 8)
| ((value & 0x7F) << 16) | ((value & 0x3F80) << 17));
}
void sendSysEx(uint32_t length, const void *data);
void sendRealTime(uint32_t type) {
switch (type) {
case 0xF8: // Clock
case 0xFA: // Start
case 0xFC: // Stop
case 0xFB: // Continue
case 0xFE: // ActiveSensing
case 0xFF: // SystemReset
write_packed((type << 8) | 0x0F);
break;
default: // Invalid Real Time marker
break;
}
}
void sendTimeCodeQuarterFrame(uint32_t type, uint32_t value) {
uint32_t data = ( ((type & 0x07) << 4) | (value & 0x0F) );
sendTimeCodeQuarterFrame(data);
}
void sendTimeCodeQuarterFrame(uint32_t data) {
write_packed(0xF108 | ((data & 0x7F) << 16));
}
protected: protected:
virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len); virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
virtual void disconnect(); virtual void disconnect();
@ -510,15 +604,39 @@ protected:
void rx_data(const Transfer_t *transfer); void rx_data(const Transfer_t *transfer);
void tx_data(const Transfer_t *transfer); void tx_data(const Transfer_t *transfer);
void init(); void init();
void write_packed(uint32_t data);
void sysex_byte(uint8_t b);
private: private:
Pipe_t *rxpipe; Pipe_t *rxpipe;
Pipe_t *txpipe; Pipe_t *txpipe;
enum { BUFFERSIZE = 64 }; enum { MAX_PACKET_SIZE = 64 };
uint8_t buffer[BUFFERSIZE * 2]; enum { RX_QUEUE_SIZE = 80 }; // must be more than MAX_PACKET_SIZE/4
uint8_t rx_ep; uint32_t rx_buffer[MAX_PACKET_SIZE/4];
uint8_t tx_ep; uint32_t tx_buffer[MAX_PACKET_SIZE/4];
uint16_t rx_size; uint16_t rx_size;
uint16_t tx_size; uint16_t tx_size;
uint32_t rx_queue[RX_QUEUE_SIZE];
bool rx_packet_queued;
uint16_t rx_head;
uint16_t rx_tail;
uint8_t rx_ep;
uint8_t tx_ep;
uint8_t msg_channel;
uint8_t msg_type;
uint8_t msg_data1;
uint8_t msg_data2;
uint8_t msg_sysex[SYSEX_MAX_LEN];
uint8_t msg_sysex_len;
void (*handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel);
void (*handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel);
void (*handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel);
void (*handleControlChange)(uint8_t ch, uint8_t control, uint8_t value);
void (*handleProgramChange)(uint8_t ch, uint8_t program);
void (*handleAfterTouch)(uint8_t ch, uint8_t pressure);
void (*handlePitchChange)(uint8_t ch, int pitch);
void (*handleSysEx)(const uint8_t *data, uint16_t length, uint8_t complete);
void (*handleRealTimeSystem)(uint8_t rtb);
void (*handleTimeCodeQuarterFrame)(uint16_t data);
Pipe_t mypipes[3] __attribute__ ((aligned(32))); Pipe_t mypipes[3] __attribute__ ((aligned(32)));
Transfer_t mytransfers[7] __attribute__ ((aligned(32))); Transfer_t mytransfers[7] __attribute__ ((aligned(32)));
}; };

View File

@ -55,12 +55,17 @@ void setup()
USBHS_USBCMD |= USBHS_USBCMD_IAA; USBHS_USBCMD |= USBHS_USBCMD_IAA;
if (rootdev) print(rootdev->control_pipe); if (rootdev) print(rootdev->control_pipe);
#endif #endif
midi1.setHandleNoteOff(OnNoteOff);
midi1.setHandleNoteOn(OnNoteOn);
midi1.setHandleControlChange(OnControlChange);
} }
void loop() void loop()
{ {
myusb.Task(); myusb.Task();
midi1.read();
} }
@ -73,3 +78,37 @@ void pulse(int usec)
} }
void OnNoteOn(byte channel, byte note, byte velocity)
{
Serial.print("Note On, ch=");
Serial.print(channel);
Serial.print(", note=");
Serial.print(note);
Serial.print(", velocity=");
Serial.print(velocity);
Serial.println();
}
void OnNoteOff(byte channel, byte note, byte velocity)
{
Serial.print("Note Off, ch=");
Serial.print(channel);
Serial.print(", note=");
Serial.print(note);
Serial.print(", velocity=");
Serial.print(velocity);
Serial.println();
}
void OnControlChange(byte channel, byte control, byte value)
{
Serial.print("Control Change, ch=");
Serial.print(channel);
Serial.print(", control=");
Serial.print(control);
Serial.print(", value=");
Serial.print(value);
Serial.println();
}

177
midi.cpp
View File

@ -29,6 +29,18 @@ void MIDIDevice::init()
{ {
contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t)); contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t)); contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
handleNoteOff = NULL;
handleNoteOn = NULL;
handleVelocityChange = NULL;
handleControlChange = NULL;
handleProgramChange = NULL;
handleAfterTouch = NULL;
handlePitchChange = NULL;
handleSysEx = NULL;
handleRealTimeSystem = NULL;
handleTimeCodeQuarterFrame = NULL;
rx_head = 0;
rx_tail = 0;
driver_ready_for_device(this); driver_ready_for_device(this);
} }
@ -136,24 +148,32 @@ bool MIDIDevice::claim(Device_t *dev, int type, const uint8_t *descriptors, uint
p += len; p += len;
} }
// if an IN endpoint was found, create its pipe // if an IN endpoint was found, create its pipe
if (rx_ep && rx_size <= BUFFERSIZE) { if (rx_ep && rx_size <= MAX_PACKET_SIZE) {
rxpipe = new_Pipe(dev, 2, rx_ep, 1, rx_size); rxpipe = new_Pipe(dev, 2, rx_ep, 1, rx_size);
if (rxpipe) { if (rxpipe) {
rxpipe->callback_function = rx_callback; rxpipe->callback_function = rx_callback;
queue_Data_Transfer(rxpipe, buffer, rx_size, this); queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
rx_packet_queued = true;
} }
} else { } else {
rxpipe = NULL; rxpipe = NULL;
} }
// if an OUT endpoint was found, create its pipe // if an OUT endpoint was found, create its pipe
if (tx_ep && tx_size <= BUFFERSIZE) { if (tx_ep && tx_size <= MAX_PACKET_SIZE) {
txpipe = new_Pipe(dev, 2, tx_ep, 0, tx_size); txpipe = new_Pipe(dev, 2, tx_ep, 0, tx_size);
if (txpipe) { if (txpipe) {
txpipe->callback_function = tx_callback; txpipe->callback_function = tx_callback;
} }
} else { } else {
rxpipe = NULL; txpipe = NULL;
} }
rx_head = 0;
rx_tail = 0;
msg_channel = 0;
msg_type = 0;
msg_data1 = 0;
msg_data2 = 0;
msg_sysex_len = 0;
// claim if either pipe created // claim if either pipe created
return (rxpipe || txpipe); return (rxpipe || txpipe);
} }
@ -177,8 +197,32 @@ void MIDIDevice::rx_data(const Transfer_t *transfer)
println("MIDIDevice Receive"); println("MIDIDevice Receive");
print(" MIDI Data: "); print(" MIDI Data: ");
print_hexbytes(transfer->buffer, rx_size); print_hexbytes(transfer->buffer, rx_size);
// TODO: parse the new data uint32_t head = rx_head;
queue_Data_Transfer(rxpipe, buffer, rx_size, this); uint32_t tail = rx_tail;
uint32_t len = rx_size >> 2; // TODO: use actual received length
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("rx_size = ", rx_size);
println("avail = ", avail);
if (avail >= (uint32_t)(rx_size>>2)) {
// enough space to accept another full packet
println("queue another receive packet");
queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
rx_packet_queued = true;
} else {
// queue can't accept another packet's data, so leave
// the data waiting on the device until we can accept it
println("wait to receive more packets");
rx_packet_queued = false;
}
} }
void MIDIDevice::tx_data(const Transfer_t *transfer) void MIDIDevice::tx_data(const Transfer_t *transfer)
@ -192,8 +236,127 @@ void MIDIDevice::tx_data(const Transfer_t *transfer)
void MIDIDevice::disconnect() void MIDIDevice::disconnect()
{ {
// TODO: free resources // should rx_queue be cleared?
// as-is, the user can still read MIDI messages
// which arrived before the device disconnected.
rxpipe = NULL;
txpipe = NULL;
} }
bool MIDIDevice::read(uint8_t channel, uint8_t cable)
{
uint32_t n, head, tail, avail, ch, type1, type2;
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;
ch = ((n >> 8) & 15) + 1;
if (type1 >= 0x08 && type1 <= 0x0E) {
if (channel && channel != ch) {
// ignore other channels when user wants single channel read
return false;
}
if (type1 == 0x08 && type2 == 0x08) {
msg_type = 8; // 8 = Note off
if (handleNoteOff)
(*handleNoteOff)(ch, (n >> 16), (n >> 24));
} else
if (type1 == 0x09 && type2 == 0x09) {
if ((n >> 24) > 0) {
msg_type = 9; // 9 = Note on
if (handleNoteOn)
(*handleNoteOn)(ch, (n >> 16), (n >> 24));
} else {
msg_type = 8; // 8 = Note off
if (handleNoteOff)
(*handleNoteOff)(ch, (n >> 16), (n >> 24));
}
} else
if (type1 == 0x0A && type2 == 0x0A) {
msg_type = 10; // 10 = Poly Pressure
if (handleVelocityChange)
(*handleVelocityChange)(ch, (n >> 16), (n >> 24));
} else
if (type1 == 0x0B && type2 == 0x0B) {
msg_type = 11; // 11 = Control Change
if (handleControlChange)
(*handleControlChange)(ch, (n >> 16), (n >> 24));
} else
if (type1 == 0x0C && type2 == 0x0C) {
msg_type = 12; // 12 = Program Change
if (handleProgramChange) (*handleProgramChange)(ch, (n >> 16));
} else
if (type1 == 0x0D && type2 == 0x0D) {
msg_type = 13; // 13 = After Touch
if (handleAfterTouch) (*handleAfterTouch)(ch, (n >> 16));
} else
if (type1 == 0x0E && type2 == 0x0E) {
msg_type = 14; // 14 = Pitch Bend
if (handlePitchChange)
(*handlePitchChange)(ch, ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
} else {
return false;
}
msg_channel = ch;
msg_data1 = (n >> 16);
msg_data2 = (n >> 24);
return true;
}
if (type1 == 0x04) {
sysex_byte(n >> 8);
sysex_byte(n >> 16);
sysex_byte(n >> 24);
return false;
}
if (type1 >= 0x05 && type1 <= 0x07) {
sysex_byte(n >> 8);
if (type1 >= 0x06) sysex_byte(n >> 16);
if (type1 == 0x07) sysex_byte(n >> 24);
msg_data1 = msg_sysex_len;
msg_sysex_len = 0;
msg_type = 15; // 15 = Sys Ex
if (handleSysEx)
(*handleSysEx)(msg_sysex, msg_data1, 1);
return true;
}
// TODO: single byte messages
// TODO: time code messages?
return false;
}
void MIDIDevice::sysex_byte(uint8_t b)
{
// when buffer is full, send another chunk to handler.
if (msg_sysex_len >= SYSEX_MAX_LEN) {
if (handleSysEx) {
(*handleSysEx)(msg_sysex, msg_sysex_len, 0);
msg_sysex_len = 0;
}
}
if (msg_sysex_len < SYSEX_MAX_LEN) {
msg_sysex[msg_sysex_len++] = b;
}
}