implemented parser for incoming messages
This commit is contained in:
parent
e91e4209b1
commit
26c4973e12
|
|
@ -132,9 +132,9 @@ public:
|
|||
|
||||
inline void read()
|
||||
{
|
||||
// n/a, data comes in async (see onWrite callbacks)
|
||||
}
|
||||
|
||||
inline void sendMIDI(StatusByte, DataByte data1 = 0, DataByte data2 = 0);
|
||||
inline void receive(uint8_t *buffer, uint8_t bufferSize);
|
||||
|
||||
void onConnected(void(*fptr)()) {
|
||||
|
|
@ -219,6 +219,39 @@ bool BleMidiInterface::begin(const char* deviceName)
|
|||
return true;
|
||||
}
|
||||
|
||||
void BleMidiInterface::sendMIDI(StatusByte status, DataByte data1, DataByte data2)
|
||||
{
|
||||
MidiType type = getTypeFromStatusByte(status);
|
||||
Channel channel = getChannelFromStatusByte(status);
|
||||
|
||||
switch (type) {
|
||||
case NoteOff:
|
||||
if (_noteOffCallback) _noteOffCallback(channel, data1, data2);
|
||||
break;
|
||||
case NoteOn:
|
||||
if (_noteOnCallback) _noteOnCallback(channel, data1, data2);
|
||||
break;
|
||||
case AfterTouchPoly:
|
||||
if (_afterTouchPolyCallback) _afterTouchPolyCallback(channel, data1, data2);
|
||||
break;
|
||||
case ControlChange:
|
||||
if (_controlChangeCallback) _controlChangeCallback(channel, data1, data2);
|
||||
break;
|
||||
case ProgramChange:
|
||||
if (_programChangeCallback) _programChangeCallback(channel, data1);
|
||||
break;
|
||||
case AfterTouchChannel:
|
||||
if (_afterTouchChannelCallback) _afterTouchChannelCallback(channel, data1);
|
||||
break;
|
||||
case PitchBend:
|
||||
if (_pitchBendCallback) {
|
||||
int value = (int) ((data1 & 0x7f) | ((data2 & 0x7f) << 7)) + MIDI_PITCHBEND_MIN;
|
||||
_pitchBendCallback(channel, value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void BleMidiInterface::receive(uint8_t *buffer, uint8_t bufferSize)
|
||||
{
|
||||
/*
|
||||
|
|
@ -251,26 +284,24 @@ void BleMidiInterface::receive(uint8_t *buffer, uint8_t bufferSize)
|
|||
MIDI messages. In the MIDI BLE protocol, the System Real-Time messages must be deinterleaved
|
||||
from other messages – except for System Exclusive messages.
|
||||
*/
|
||||
Channel channel;
|
||||
MidiType command;
|
||||
|
||||
//Pointers used to search through payload.
|
||||
uint8_t lPtr = 0;
|
||||
uint8_t rPtr = 0;
|
||||
|
||||
//lastStatus used to capture runningStatus
|
||||
uint8_t lastStatus;
|
||||
|
||||
//Decode first packet -- SHALL be "Full MIDI message"
|
||||
lPtr = 2; //Start at first MIDI status -- SHALL be "MIDI status"
|
||||
|
||||
//While statement contains incrementing pointers and breaks when buffer size exceeded.
|
||||
while(1){
|
||||
//lastStatus used to capture runningStatus
|
||||
auto lastStatus = buffer[lPtr];
|
||||
lastStatus = buffer[lPtr];
|
||||
if( (buffer[lPtr] < 0x80) ){
|
||||
//Status message not present, bail
|
||||
return;
|
||||
}
|
||||
|
||||
command = getTypeFromStatusByte(lastStatus);
|
||||
channel = getChannelFromStatusByte(lastStatus);
|
||||
|
||||
//Point to next non-data byte
|
||||
rPtr = lPtr;
|
||||
while( (buffer[rPtr + 1] < 0x80)&&(rPtr < (bufferSize - 1)) ){
|
||||
|
|
@ -279,36 +310,28 @@ void BleMidiInterface::receive(uint8_t *buffer, uint8_t bufferSize)
|
|||
//look at l and r pointers and decode by size.
|
||||
if( rPtr - lPtr < 1 ){
|
||||
//Time code or system
|
||||
// MIDI.send(command, 0, 0, channel);
|
||||
sendMIDI(lastStatus);
|
||||
} else if( rPtr - lPtr < 2 ) {
|
||||
// MIDI.send(command, buffer[lPtr + 1], 0, channel);
|
||||
sendMIDI(lastStatus, buffer[lPtr + 1]);
|
||||
} else if( rPtr - lPtr < 3 ) {
|
||||
|
||||
// TODO: switch for type
|
||||
|
||||
if (_noteOnCallback) // if an attached function exisist, call it here
|
||||
_noteOnCallback(0, 1, 2);
|
||||
|
||||
// MIDI.send(command, buffer[lPtr + 1], buffer[lPtr + 2], channel);
|
||||
sendMIDI(lastStatus, buffer[lPtr + 1], buffer[lPtr + 2]);
|
||||
} else {
|
||||
//Too much data
|
||||
//If not System Common or System Real-Time, send it as running status
|
||||
switch( buffer[lPtr] & 0xF0 )
|
||||
{
|
||||
case 0x80:
|
||||
case 0x90:
|
||||
case 0xA0:
|
||||
case 0xB0:
|
||||
case 0xE0:
|
||||
for (int i = lPtr; i < rPtr; i = i + 2) {
|
||||
// MIDI.send(command, buffer[i + 1], buffer[i + 2], channel);
|
||||
}
|
||||
case NoteOff:
|
||||
case NoteOn:
|
||||
case AfterTouchPoly:
|
||||
case ControlChange:
|
||||
case PitchBend:
|
||||
for(int i = lPtr; i < rPtr; i = i + 2)
|
||||
sendMIDI(lastStatus, buffer[i + 1], buffer[i + 2]);
|
||||
break;
|
||||
case 0xC0:
|
||||
case 0xD0:
|
||||
for (int i = lPtr; i < rPtr; i = i + 1) {
|
||||
// MIDI.send(command, buffer[i + 1], 0, channel);
|
||||
}
|
||||
case ProgramChange:
|
||||
case AfterTouchChannel:
|
||||
for(int i = lPtr; i < rPtr; i = i + 1)
|
||||
sendMIDI(lastStatus, buffer[i + 1]);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
|
@ -321,6 +344,7 @@ if (_noteOnCallback) // if an attached function exisist, call it here
|
|||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
END_BLEMIDI_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -277,24 +277,24 @@ protected:
|
|||
public:
|
||||
// sending
|
||||
void sendNoteOn(DataByte note, DataByte velocity, Channel channel) {
|
||||
send(MidiType::NoteOn, channel, note, velocity);
|
||||
sendChannelMessage(MidiType::NoteOn, channel, note, velocity);
|
||||
}
|
||||
|
||||
void sendNoteOff(DataByte note, DataByte velocity, Channel channel) {
|
||||
send(MidiType::NoteOff, channel, note, velocity);
|
||||
sendChannelMessage(MidiType::NoteOff, channel, note, velocity);
|
||||
}
|
||||
|
||||
void sendProgramChange(DataByte number, Channel channel) {
|
||||
send(MidiType::ProgramChange, number, 0, channel);
|
||||
sendChannelMessage(MidiType::ProgramChange, number, 0, channel);
|
||||
}
|
||||
|
||||
void sendControlChange(DataByte number, DataByte value, Channel channel) {
|
||||
send(MidiType::ControlChange, number, value, channel);
|
||||
sendChannelMessage(MidiType::ControlChange, number, value, channel);
|
||||
}
|
||||
|
||||
void sendPitchBend(int value, Channel channel) {
|
||||
const unsigned bend = unsigned(value - int(MIDI_PITCHBEND_MIN));
|
||||
send(MidiType::PitchBend, (bend & 0x7f), (bend >> 7) & 0x7f, channel);
|
||||
sendChannelMessage(MidiType::PitchBend, (bend & 0x7f), (bend >> 7) & 0x7f, channel);
|
||||
}
|
||||
|
||||
void sendPitchBend(double pitchValue, Channel channel) {
|
||||
|
|
@ -304,15 +304,15 @@ public:
|
|||
}
|
||||
|
||||
void sendPolyPressure(DataByte note, DataByte pressure, Channel channel) {
|
||||
send(MidiType::AfterTouchPoly, note, pressure, channel);
|
||||
sendChannelMessage(MidiType::AfterTouchPoly, note, pressure, channel);
|
||||
}
|
||||
|
||||
void sendAfterTouch(DataByte pressure, Channel channel) {
|
||||
send(MidiType::AfterTouchChannel, pressure, 0, channel);
|
||||
sendChannelMessage(MidiType::AfterTouchChannel, pressure, 0, channel);
|
||||
}
|
||||
|
||||
void sendAfterTouch(DataByte note, DataByte pressure, Channel channel) {
|
||||
send(MidiType::AfterTouchChannel, note, pressure, channel);
|
||||
sendChannelMessage(MidiType::AfterTouchChannel, note, pressure, channel);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -322,62 +322,66 @@ public:
|
|||
|
||||
|
||||
void sendTimeCodeQuarterFrame(DataByte typeNibble, DataByte valuesNibble) {
|
||||
// TODO f(typeNibble, valuesNibble);
|
||||
send(MidiType::TimeCodeQuarterFrame);
|
||||
const byte data = byte((((typeNibble & 0x07) << 4) | (valuesNibble & 0x0f)));
|
||||
sendTimeCodeQuarterFrame(data);
|
||||
}
|
||||
|
||||
void sendTimeCodeQuarterFrame(DataByte data) {
|
||||
send(MidiType::TimeCodeQuarterFrame, data);
|
||||
sendSystemCommonMessage(MidiType::TimeCodeQuarterFrame, data);
|
||||
}
|
||||
|
||||
void sendSongPosition(unsigned short beats) {
|
||||
send(MidiType::SongPosition, beats);
|
||||
byte data1 = beats & 0x7f;
|
||||
byte data2 = (beats >> 7) & 0x7f;
|
||||
|
||||
sendSystemCommonMessage(MidiType::SongPosition, data1, data2);
|
||||
}
|
||||
|
||||
void sendSongSelect(DataByte number) {
|
||||
send(MidiType::SongSelect, number);
|
||||
sendSystemCommonMessage(MidiType::SongSelect, number & 0x7f);
|
||||
}
|
||||
|
||||
void sendTuneRequest() {
|
||||
send(MidiType::TuneRequest);
|
||||
sendSystemCommonMessage(MidiType::TuneRequest);
|
||||
}
|
||||
|
||||
void sendActiveSensing() {
|
||||
send(MidiType::ActiveSensing);
|
||||
sendSystemCommonMessage(MidiType::ActiveSensing);
|
||||
}
|
||||
|
||||
|
||||
void sendStart() {
|
||||
send(MidiType::Start);
|
||||
sendRealTimeMessage(MidiType::Start);
|
||||
}
|
||||
|
||||
void sendContinue() {
|
||||
send(MidiType::Continue);
|
||||
sendRealTimeMessage(MidiType::Continue);
|
||||
}
|
||||
|
||||
void sendStop() {
|
||||
send(MidiType::Stop);
|
||||
sendRealTimeMessage(MidiType::Stop);
|
||||
}
|
||||
|
||||
void sendClock() {
|
||||
send(MidiType::Clock);
|
||||
sendRealTimeMessage(MidiType::Clock);
|
||||
}
|
||||
|
||||
void sendTick() {
|
||||
send(MidiType::Tick);
|
||||
sendRealTimeMessage(MidiType::Tick);
|
||||
}
|
||||
|
||||
void sendReset() {
|
||||
send(MidiType::SystemReset);
|
||||
sendRealTimeMessage(MidiType::SystemReset);
|
||||
}
|
||||
|
||||
|
||||
//receiving
|
||||
void setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity)) {
|
||||
_noteOnCallback = fptr;
|
||||
}
|
||||
void setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity)) {
|
||||
_noteOffCallback = fptr;
|
||||
}
|
||||
void setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity)) {
|
||||
_noteOnCallback = fptr;
|
||||
}
|
||||
void setHandleAfterTouchPoly(void (*fptr)(byte channel, byte note, byte pressure)) {
|
||||
_afterTouchPolyCallback = fptr;
|
||||
}
|
||||
|
|
@ -428,8 +432,9 @@ public:
|
|||
}
|
||||
|
||||
protected:
|
||||
|
||||
// Channel messages
|
||||
virtual void send(MidiType type, DataByte data1, DataByte data2, Channel channel)
|
||||
virtual void sendChannelMessage(MidiType type, DataByte data1, DataByte data2, Channel channel)
|
||||
{
|
||||
// Then test if channel is valid
|
||||
if (channel >= MIDI_CHANNEL_OFF ||
|
||||
|
|
@ -460,27 +465,42 @@ protected:
|
|||
}
|
||||
else if (type >= MidiType::Clock && type <= MidiType::SystemReset)
|
||||
{
|
||||
send(type); // System Real-time and 1 byte.
|
||||
sendRealTimeMessage(type); // System Real-time and 1 byte.
|
||||
}
|
||||
}
|
||||
|
||||
// SystemCommon message
|
||||
virtual void send(MidiType type, DataByte data1)
|
||||
virtual void sendSystemCommonMessage(MidiType type, DataByte data1 = 0, DataByte data2 = 0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// realTime messages
|
||||
virtual void send(MidiType type)
|
||||
// RealTime messages
|
||||
virtual void sendRealTimeMessage(MidiType type)
|
||||
{
|
||||
// Do not invalidate Running Status for real-time messages
|
||||
// as they can be interleaved within any message.
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case Clock:
|
||||
case Start:
|
||||
case Stop:
|
||||
case Continue:
|
||||
case ActiveSensing:
|
||||
case SystemReset:
|
||||
serialize(type);
|
||||
break;
|
||||
default:
|
||||
// Invalid Real Time marker
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool begin(const char*) = 0;
|
||||
|
||||
virtual void read() = 0;
|
||||
|
||||
// serialize from the hardware
|
||||
virtual void read() = 0;
|
||||
|
||||
// serialize towards to hardware
|
||||
virtual void serialize(DataByte) = 0;
|
||||
|
|
@ -491,3 +511,4 @@ protected:
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue