mirror of
https://github.com/gdsports/USBHost_t36
synced 2024-11-13 12:45:04 -05:00
Update MIDI features to match USB MIDI in TD 1.41
This commit is contained in:
parent
fcfecee43b
commit
9488eb7280
325
USBHost_t36.h
325
USBHost_t36.h
@ -842,13 +842,164 @@ private:
|
||||
|
||||
class MIDIDevice : public USBDriver {
|
||||
public:
|
||||
enum { SYSEX_MAX_LEN = 60 };
|
||||
enum { SYSEX_MAX_LEN = 290 };
|
||||
|
||||
// Message type names for compatibility with Arduino MIDI library 4.3.1
|
||||
enum MidiType {
|
||||
InvalidType = 0x00, // For notifying errors
|
||||
NoteOff = 0x80, // Note Off
|
||||
NoteOn = 0x90, // Note On
|
||||
AfterTouchPoly = 0xA0, // Polyphonic AfterTouch
|
||||
ControlChange = 0xB0, // Control Change / Channel Mode
|
||||
ProgramChange = 0xC0, // Program Change
|
||||
AfterTouchChannel = 0xD0, // Channel (monophonic) AfterTouch
|
||||
PitchBend = 0xE0, // Pitch Bend
|
||||
SystemExclusive = 0xF0, // System Exclusive
|
||||
TimeCodeQuarterFrame = 0xF1, // System Common - MIDI Time Code Quarter Frame
|
||||
SongPosition = 0xF2, // System Common - Song Position Pointer
|
||||
SongSelect = 0xF3, // System Common - Song Select
|
||||
TuneRequest = 0xF6, // System Common - Tune Request
|
||||
Clock = 0xF8, // System Real Time - Timing Clock
|
||||
Start = 0xFA, // System Real Time - Start
|
||||
Continue = 0xFB, // System Real Time - Continue
|
||||
Stop = 0xFC, // System Real Time - Stop
|
||||
ActiveSensing = 0xFE, // System Real Time - Active Sensing
|
||||
SystemReset = 0xFF, // System Real Time - System Reset
|
||||
};
|
||||
MIDIDevice(USBHost &host) { init(); }
|
||||
MIDIDevice(USBHost *host) { init(); }
|
||||
bool read(uint8_t channel=0, uint8_t cable=0);
|
||||
|
||||
void sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) {
|
||||
send(0x80, note, velocity, channel, cable);
|
||||
}
|
||||
void sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) {
|
||||
send(0x90, note, velocity, channel, cable);
|
||||
}
|
||||
void sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) {
|
||||
send(0xA0, note, pressure, channel, cable);
|
||||
}
|
||||
void sendAfterTouch(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) {
|
||||
send(0xA0, note, pressure, channel, cable);
|
||||
}
|
||||
void sendControlChange(uint8_t control, uint8_t value, uint8_t channel, uint8_t cable=0) {
|
||||
send(0xB0, control, value, channel, cable);
|
||||
}
|
||||
void sendProgramChange(uint8_t program, uint8_t channel, uint8_t cable=0) {
|
||||
send(0xC0, program, 0, channel, cable);
|
||||
}
|
||||
void sendAfterTouch(uint8_t pressure, uint8_t channel, uint8_t cable=0) {
|
||||
send(0xD0, pressure, 0, channel, cable);
|
||||
}
|
||||
void sendPitchBend(uint16_t value, uint8_t channel, uint8_t cable=0) {
|
||||
// MIDI 4.3 takes -8192 to +8191. We take 0 to 16383
|
||||
// MIDI 4.3 also has version that takes float -1.0 to +1.0
|
||||
send(0xE0, value, value >> 7, channel, cable);
|
||||
}
|
||||
void sendSysEx(uint32_t length, const uint8_t *data, bool hasTerm=false, uint8_t cable=0) {
|
||||
//if (cable >= MIDI_NUM_CABLES) return;
|
||||
if (hasTerm) {
|
||||
send_sysex_buffer_has_term(data, length, cable);
|
||||
} else {
|
||||
send_sysex_add_term_bytes(data, length, cable);
|
||||
}
|
||||
}
|
||||
void sendRealTime(uint8_t type, uint8_t cable=0) {
|
||||
switch (type) {
|
||||
case 0xF8: // Clock
|
||||
case 0xFA: // Start
|
||||
case 0xFB: // Continue
|
||||
case 0xFC: // Stop
|
||||
case 0xFE: // ActiveSensing
|
||||
case 0xFF: // SystemReset
|
||||
send(type, 0, 0, 0, cable);
|
||||
break;
|
||||
default: // Invalid Real Time marker
|
||||
break;
|
||||
}
|
||||
}
|
||||
void sendTimeCodeQuarterFrame(uint8_t type, uint8_t value, uint8_t cable=0) {
|
||||
send(0xF1, ((type & 0x07) << 4) | (value & 0x0F), 0, 0, cable);
|
||||
}
|
||||
void sendSongPosition(uint16_t beats, uint8_t cable=0) {
|
||||
send(0xF2, beats, beats >> 7, 0, cable);
|
||||
}
|
||||
void sendSongSelect(uint8_t song, uint8_t cable=0) {
|
||||
send(0xF3, song, 0, 0, cable);
|
||||
}
|
||||
void sendTuneRequest(uint8_t cable=0) {
|
||||
send(0xF6, 0, 0, 0, cable);
|
||||
}
|
||||
void beginRpn(uint16_t number, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(101, number >> 7, channel, cable);
|
||||
sendControlChange(100, number, channel, cable);
|
||||
}
|
||||
void sendRpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(6, value >> 7, channel, cable);
|
||||
sendControlChange(38, value, channel, cable);
|
||||
}
|
||||
void sendRpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(6, msb, channel, cable);
|
||||
sendControlChange(38, lsb, channel, cable);
|
||||
}
|
||||
void sendRpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(96, amount, channel, cable);
|
||||
}
|
||||
void sendRpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(97, amount, channel, cable);
|
||||
}
|
||||
void endRpn(uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(101, 0x7F, channel, cable);
|
||||
sendControlChange(100, 0x7F, channel, cable);
|
||||
}
|
||||
void beginNrpn(uint16_t number, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(99, number >> 7, channel, cable);
|
||||
sendControlChange(98, number, channel, cable);
|
||||
}
|
||||
void sendNrpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(6, value >> 7, channel, cable);
|
||||
sendControlChange(38, value, channel, cable);
|
||||
}
|
||||
void sendNrpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(6, msb, channel, cable);
|
||||
sendControlChange(38, lsb, channel, cable);
|
||||
}
|
||||
void sendNrpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(96, amount, channel, cable);
|
||||
}
|
||||
void sendNrpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(97, amount, channel, cable);
|
||||
}
|
||||
void endNrpn(uint8_t channel, uint8_t cable=0) {
|
||||
sendControlChange(99, 0x7F, channel, cable);
|
||||
sendControlChange(98, 0x7F, channel, cable);
|
||||
}
|
||||
void send(uint8_t type, uint8_t data1, uint8_t data2, uint8_t channel, uint8_t cable) {
|
||||
//if (cable >= MIDI_NUM_CABLES) return;
|
||||
if (type < 0xF0) {
|
||||
if (type < 0x80) return;
|
||||
type &= 0xF0;
|
||||
write_packed((type << 8) | (type >> 4) | ((cable & 0x0F) << 4)
|
||||
| (((channel - 1) & 0x0F) << 8) | ((data1 & 0x7F) << 16)
|
||||
| ((data2 & 0x7F) << 24));
|
||||
} else if (type >= 0xF8 || type == 0xF6) {
|
||||
write_packed((type << 8) | 0x0F | ((cable & 0x0F) << 4));
|
||||
} else if (type == 0xF1 || type == 0xF3) {
|
||||
write_packed((type << 8) | 0x02 | ((cable & 0x0F) << 4)
|
||||
| ((data1 & 0x7F) << 16));
|
||||
} else if (type == 0xF2) {
|
||||
write_packed((type << 8) | 0x03 | ((cable & 0x0F) << 4)
|
||||
| ((data1 & 0x7F) << 16) | ((data2 & 0x7F) << 24));
|
||||
}
|
||||
}
|
||||
void send_now(void) __attribute__((always_inline)) {
|
||||
}
|
||||
bool read(uint8_t channel=0);
|
||||
uint8_t getType(void) {
|
||||
return msg_type;
|
||||
};
|
||||
uint8_t getCable(void) {
|
||||
return msg_cable;
|
||||
}
|
||||
uint8_t getChannel(void) {
|
||||
return msg_channel;
|
||||
};
|
||||
@ -858,85 +1009,100 @@ public:
|
||||
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;
|
||||
uint8_t * getSysExArray(void) {
|
||||
return msg_sysex;
|
||||
}
|
||||
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 setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
|
||||
// type: 0x80 NoteOff
|
||||
handleNoteOff = fptr;
|
||||
}
|
||||
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 setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
|
||||
// type: 0x90 NoteOn
|
||||
handleNoteOn = fptr;
|
||||
}
|
||||
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 setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
|
||||
// type: 0xA0 AfterTouchPoly
|
||||
handleVelocityChange = fptr;
|
||||
}
|
||||
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 setHandleAfterTouchPoly(void (*fptr)(uint8_t channel, uint8_t note, uint8_t pressure)) {
|
||||
// type: 0xA0 AfterTouchPoly
|
||||
handleVelocityChange = fptr;
|
||||
}
|
||||
void sendProgramChange(uint32_t program, uint32_t channel) {
|
||||
write_packed(0xC00C | (((channel - 1) & 0x0F) << 8)
|
||||
| ((program & 0x7F) << 16));
|
||||
void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
|
||||
// type: 0xB0 ControlChange
|
||||
handleControlChange = fptr;
|
||||
}
|
||||
void sendAfterTouch(uint32_t pressure, uint32_t channel) {
|
||||
write_packed(0xD00D | (((channel - 1) & 0x0F) << 8)
|
||||
| ((pressure & 0x7F) << 16));
|
||||
void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
|
||||
// type: 0xC0 ProgramChange
|
||||
handleProgramChange = fptr;
|
||||
}
|
||||
void sendPitchBend(uint32_t value, uint32_t channel) {
|
||||
write_packed(0xE00E | (((channel - 1) & 0x0F) << 8)
|
||||
| ((value & 0x7F) << 16) | ((value & 0x3F80) << 17));
|
||||
void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
|
||||
// type: 0xD0 AfterTouchChannel
|
||||
handleAfterTouch = fptr;
|
||||
}
|
||||
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 setHandleAfterTouchChannel(void (*fptr)(uint8_t channel, uint8_t pressure)) {
|
||||
// type: 0xD0 AfterTouchChannel
|
||||
handleAfterTouch = fptr;
|
||||
}
|
||||
void sendTimeCodeQuarterFrame(uint32_t type, uint32_t value) {
|
||||
uint32_t data = ( ((type & 0x07) << 4) | (value & 0x0F) );
|
||||
sendTimeCodeQuarterFrame(data);
|
||||
void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
|
||||
// type: 0xE0 PitchBend
|
||||
handlePitchChange = fptr;
|
||||
}
|
||||
void sendTimeCodeQuarterFrame(uint32_t data) {
|
||||
write_packed(0xF108 | ((data & 0x7F) << 16));
|
||||
void setHandleSysEx(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
|
||||
// type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
|
||||
handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
|
||||
}
|
||||
void setHandleSystemExclusive(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
|
||||
// type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
|
||||
handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
|
||||
}
|
||||
void setHandleSystemExclusive(void (*fptr)(uint8_t *data, unsigned int size)) {
|
||||
// type: 0xF0 SystemExclusive - single call, message larger than buffer is truncated
|
||||
handleSysExComplete = fptr;
|
||||
}
|
||||
void setHandleTimeCodeQuarterFrame(void (*fptr)(uint8_t data)) {
|
||||
// type: 0xF1 TimeCodeQuarterFrame
|
||||
handleTimeCodeQuarterFrame = fptr;
|
||||
}
|
||||
void setHandleSongPosition(void (*fptr)(uint16_t beats)) {
|
||||
// type: 0xF2 SongPosition
|
||||
handleSongPosition = fptr;
|
||||
}
|
||||
void setHandleSongSelect(void (*fptr)(uint8_t songnumber)) {
|
||||
// type: 0xF3 SongSelect
|
||||
handleSongSelect = fptr;
|
||||
}
|
||||
void setHandleTuneRequest(void (*fptr)(void)) {
|
||||
// type: 0xF6 TuneRequest
|
||||
handleTuneRequest = fptr;
|
||||
}
|
||||
void setHandleClock(void (*fptr)(void)) {
|
||||
// type: 0xF8 Clock
|
||||
handleClock = fptr;
|
||||
}
|
||||
void setHandleStart(void (*fptr)(void)) {
|
||||
// type: 0xFA Start
|
||||
handleStart = fptr;
|
||||
}
|
||||
void setHandleContinue(void (*fptr)(void)) {
|
||||
// type: 0xFB Continue
|
||||
handleContinue = fptr;
|
||||
}
|
||||
void setHandleStop(void (*fptr)(void)) {
|
||||
// type: 0xFC Stop
|
||||
handleStop = fptr;
|
||||
}
|
||||
void setHandleActiveSensing(void (*fptr)(void)) {
|
||||
// type: 0xFE ActiveSensing
|
||||
handleActiveSensing = fptr;
|
||||
}
|
||||
void setHandleSystemReset(void (*fptr)(void)) {
|
||||
// type: 0xFF SystemReset
|
||||
handleSystemReset = fptr;
|
||||
}
|
||||
void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
|
||||
// type: 0xF8-0xFF - if more specific handler not configured
|
||||
handleRealTimeSystem = fptr;
|
||||
}
|
||||
protected:
|
||||
virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
|
||||
@ -947,6 +1113,8 @@ protected:
|
||||
void tx_data(const Transfer_t *transfer);
|
||||
void init();
|
||||
void write_packed(uint32_t data);
|
||||
void send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable);
|
||||
void send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable);
|
||||
void sysex_byte(uint8_t b);
|
||||
private:
|
||||
Pipe_t *rxpipe;
|
||||
@ -966,6 +1134,7 @@ private:
|
||||
volatile uint8_t tx2_count;
|
||||
uint8_t rx_ep;
|
||||
uint8_t tx_ep;
|
||||
uint8_t msg_cable;
|
||||
uint8_t msg_channel;
|
||||
uint8_t msg_type;
|
||||
uint8_t msg_data1;
|
||||
@ -979,9 +1148,19 @@ private:
|
||||
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 (*handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete);
|
||||
void (*handleSysExComplete)(uint8_t *data, unsigned int size);
|
||||
void (*handleTimeCodeQuarterFrame)(uint8_t data);
|
||||
void (*handleSongPosition)(uint16_t beats);
|
||||
void (*handleSongSelect)(uint8_t songnumber);
|
||||
void (*handleTuneRequest)(void);
|
||||
void (*handleClock)(void);
|
||||
void (*handleStart)(void);
|
||||
void (*handleContinue)(void);
|
||||
void (*handleStop)(void);
|
||||
void (*handleActiveSensing)(void);
|
||||
void (*handleSystemReset)(void);
|
||||
void (*handleRealTimeSystem)(uint8_t rtb);
|
||||
void (*handleTimeCodeQuarterFrame)(uint16_t data);
|
||||
Pipe_t mypipes[3] __attribute__ ((aligned(32)));
|
||||
Transfer_t mytransfers[7] __attribute__ ((aligned(32)));
|
||||
strbuf_t mystring_bufs[1];
|
||||
|
227
midi.cpp
227
midi.cpp
@ -39,9 +39,19 @@ void MIDIDevice::init()
|
||||
handleProgramChange = NULL;
|
||||
handleAfterTouch = NULL;
|
||||
handlePitchChange = NULL;
|
||||
handleSysEx = NULL;
|
||||
handleRealTimeSystem = 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;
|
||||
@ -75,7 +85,6 @@ void MIDIDevice::init()
|
||||
|
||||
bool MIDIDevice::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
|
||||
{
|
||||
|
||||
// only claim at interface level
|
||||
if (type != 1) return false;
|
||||
println("MIDIDevice claim this=", (uint32_t)this, HEX);
|
||||
@ -293,8 +302,56 @@ void MIDIDevice::write_packed(uint32_t data)
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
bool MIDIDevice::read(uint8_t channel, uint8_t cable)
|
||||
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;
|
||||
|
||||
@ -312,63 +369,148 @@ bool MIDIDevice::read(uint8_t channel, uint8_t cable)
|
||||
__enable_irq();
|
||||
}
|
||||
}
|
||||
|
||||
println("read: ", n, HEX);
|
||||
|
||||
type1 = n & 15;
|
||||
type2 = (n >> 12) & 15;
|
||||
ch = ((n >> 8) & 15) + 1;
|
||||
msg_cable = (n >> 4) & 15;
|
||||
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)
|
||||
msg_type = 0x80; // 0x80 = 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)
|
||||
msg_type = 0x9; // 0x9 = Note on
|
||||
if (handleNoteOn) {
|
||||
(*handleNoteOn)(ch, (n >> 16), (n >> 24));
|
||||
}
|
||||
} else {
|
||||
msg_type = 8; // 8 = Note off
|
||||
if (handleNoteOff)
|
||||
msg_type = 0x8; // 0x8 = Note off
|
||||
if (handleNoteOff) {
|
||||
(*handleNoteOff)(ch, (n >> 16), (n >> 24));
|
||||
}
|
||||
}
|
||||
} else
|
||||
if (type1 == 0x0A && type2 == 0x0A) {
|
||||
msg_type = 10; // 10 = Poly Pressure
|
||||
if (handleVelocityChange)
|
||||
msg_type = 0xA0; // 0xA0 = AfterTouchPoly
|
||||
if (handleVelocityChange) {
|
||||
(*handleVelocityChange)(ch, (n >> 16), (n >> 24));
|
||||
}
|
||||
} else
|
||||
if (type1 == 0x0B && type2 == 0x0B) {
|
||||
msg_type = 11; // 11 = Control Change
|
||||
if (handleControlChange)
|
||||
if (type1 == 0x0B && type2 == 0x0B) {
|
||||
msg_type = 0xB0; // 0xB0 = 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));
|
||||
if (type1 == 0x0C && type2 == 0x0C) {
|
||||
msg_type = 0xC0; // 0xC0 = 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));
|
||||
if (type1 == 0x0D && type2 == 0x0D) {
|
||||
msg_type = 0xD0; // 0xD0 = After Touch
|
||||
if (handleAfterTouch) {
|
||||
(*handleAfterTouch)(ch, (n >> 16));
|
||||
}
|
||||
} else
|
||||
if (type1 == 0x0E && type2 == 0x0E) {
|
||||
msg_type = 14; // 14 = Pitch Bend
|
||||
if (handlePitchChange)
|
||||
if (type1 == 0x0E && type2 == 0x0E) {
|
||||
msg_type = 0xE0; // 0xE0 = Pitch Bend
|
||||
if (handlePitchChange) {
|
||||
(*handlePitchChange)(ch, ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
|
||||
}
|
||||
} 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 && type2 == 0x0F)) {
|
||||
// system common or system realtime message
|
||||
uint8_t type;
|
||||
system_common_or_realtime:
|
||||
type = n >> 8;
|
||||
switch (type) {
|
||||
case 0xF1: // usbMIDI.TimeCodeQuarterFrame
|
||||
if (handleTimeCodeQuarterFrame) {
|
||||
(*handleTimeCodeQuarterFrame)(n >> 16);
|
||||
}
|
||||
break;
|
||||
case 0xF2: // usbMIDI.SongPosition
|
||||
if (handleSongPosition) {
|
||||
(*handleSongPosition)(((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
|
||||
}
|
||||
break;
|
||||
case 0xF3: // usbMIDI.SongSelect
|
||||
if (handleSongSelect) {
|
||||
(*handleSongSelect)(n >> 16);
|
||||
}
|
||||
break;
|
||||
case 0xF6: // usbMIDI.TuneRequest
|
||||
if (handleTuneRequest) {
|
||||
(*handleTuneRequest)();
|
||||
}
|
||||
break;
|
||||
case 0xF8: // usbMIDI.Clock
|
||||
if (handleClock) {
|
||||
(*handleClock)();
|
||||
} else if (handleRealTimeSystem) {
|
||||
(*handleRealTimeSystem)(0xF8);
|
||||
}
|
||||
break;
|
||||
case 0xFA: // usbMIDI.Start
|
||||
if (handleStart) {
|
||||
(*handleStart)();
|
||||
} else if (handleRealTimeSystem) {
|
||||
(*handleRealTimeSystem)(0xFA);
|
||||
}
|
||||
break;
|
||||
case 0xFB: // usbMIDI.Continue
|
||||
if (handleContinue) {
|
||||
(*handleContinue)();
|
||||
} else if (handleRealTimeSystem) {
|
||||
(*handleRealTimeSystem)(0xFB);
|
||||
}
|
||||
break;
|
||||
case 0xFC: // usbMIDI.Stop
|
||||
if (handleStop) {
|
||||
(*handleStop)();
|
||||
} else if (handleRealTimeSystem) {
|
||||
(*handleRealTimeSystem)(0xFC);
|
||||
}
|
||||
break;
|
||||
case 0xFE: // usbMIDI.ActiveSensing
|
||||
if (handleActiveSensing) {
|
||||
(*handleActiveSensing)();
|
||||
} else if (handleRealTimeSystem) {
|
||||
(*handleRealTimeSystem)(0xFE);
|
||||
}
|
||||
break;
|
||||
case 0xFF: // usbMIDI.SystemReset
|
||||
if (handleSystemReset) {
|
||||
(*handleSystemReset)();
|
||||
} else if (handleRealTimeSystem) {
|
||||
(*handleRealTimeSystem)(0xFF);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return false; // unknown message, ignore it
|
||||
}
|
||||
msg_type = type;
|
||||
goto return_message;
|
||||
}
|
||||
if (type1 == 0x04) {
|
||||
sysex_byte(n >> 8);
|
||||
sysex_byte(n >> 16);
|
||||
@ -379,26 +521,37 @@ bool MIDIDevice::read(uint8_t channel, uint8_t cable)
|
||||
sysex_byte(n >> 8);
|
||||
if (type1 >= 0x06) sysex_byte(n >> 16);
|
||||
if (type1 == 0x07) sysex_byte(n >> 24);
|
||||
msg_data1 = msg_sysex_len;
|
||||
uint16_t len = msg_sysex_len;
|
||||
msg_data1 = len;
|
||||
msg_data2 = len >> 8;
|
||||
msg_sysex_len = 0;
|
||||
msg_type = 15; // 15 = Sys Ex
|
||||
if (handleSysEx)
|
||||
(*handleSysEx)(msg_sysex, msg_data1, 1);
|
||||
msg_type = 0xF0; // 0xF0 = SystemExclusive
|
||||
if (handleSysExPartial) {
|
||||
(*handleSysExPartial)(msg_sysex, len, 1);
|
||||
} else if (handleSysExComplete) {
|
||||
(*handleSysExComplete)(msg_sysex, len);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// TODO: single byte messages
|
||||
// TODO: time code messages?
|
||||
if (type1 == 0x0F) {
|
||||
uint8_t b = n >> 8;
|
||||
if (b >= 0xF8) {
|
||||
goto system_common_or_realtime;
|
||||
}
|
||||
if (msg_sysex_len > 0) {
|
||||
// Is this really needed? Mac OS-X does this, but do any devices?
|
||||
sysex_byte(n >> 8);
|
||||
}
|
||||
}
|
||||
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 (handleSysExPartial && msg_sysex_len >= SYSEX_MAX_LEN) {
|
||||
// when buffer is full, send another chunk to partial handler.
|
||||
(*handleSysExPartial)(msg_sysex, msg_sysex_len, 0);
|
||||
msg_sysex_len = 0;
|
||||
}
|
||||
if (msg_sysex_len < SYSEX_MAX_LEN) {
|
||||
msg_sysex[msg_sysex_len++] = b;
|
||||
|
Loading…
Reference in New Issue
Block a user