|
|
@@ -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 setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) { |
|
|
|
// type: 0xE0 PitchBend |
|
|
|
handlePitchChange = fptr; |
|
|
|
} |
|
|
|
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 sendTimeCodeQuarterFrame(uint32_t type, uint32_t value) { |
|
|
|
uint32_t data = ( ((type & 0x07) << 4) | (value & 0x0F) ); |
|
|
|
sendTimeCodeQuarterFrame(data); |
|
|
|
void setHandleSystemReset(void (*fptr)(void)) { |
|
|
|
// type: 0xFF SystemReset |
|
|
|
handleSystemReset = fptr; |
|
|
|
} |
|
|
|
void sendTimeCodeQuarterFrame(uint32_t data) { |
|
|
|
write_packed(0xF108 | ((data & 0x7F) << 16)); |
|
|
|
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]; |