mbed USBHostMIDI improved. function pointer initialization and null checking
Diff: USBHostMIDI/USBHostMIDI.cpp
- Revision:
- 7:264794e7fed1
- Parent:
- 1:ab240722d7ef
diff -r d3ac9e1c0035 -r 264794e7fed1 USBHostMIDI/USBHostMIDI.cpp --- a/USBHostMIDI/USBHostMIDI.cpp Wed Apr 26 20:08:31 2017 +0000 +++ b/USBHostMIDI/USBHostMIDI.cpp Fri May 10 11:23:49 2019 +0000 @@ -24,11 +24,24 @@ #define SET_LINE_CODING 0x20 -USBHostMIDI::USBHostMIDI() { - host = USBHost::getHostInst(); - size_bulk_in = 0; - size_bulk_out = 0; - init(); +USBHostMIDI::USBHostMIDI() : + miscellaneousFunctionCode(NULL), + cableEvent(NULL), + systemCommonTwoBytes(NULL), + systemCommonThreeBytes(NULL), + systemExclusive(NULL), + noteOff(NULL), + noteOn(NULL), + polyKeyPress(NULL), + controlChange(NULL), + programChange(NULL), + channelPressure(NULL), + pitchBend(NULL), + singleByte(NULL) { + host = USBHost::getHostInst(); + size_bulk_in = 0; + size_bulk_out = 0; + init(); } void USBHostMIDI::init() { @@ -105,26 +118,38 @@ // switch by code index number switch (midi[0] & 0xf) { case 0: // miscellaneous function codes - miscellaneousFunctionCode(midi[1], midi[2], midi[3]); + if (miscellaneousFunctionCode) { + miscellaneousFunctionCode(midi[1], midi[2], midi[3]); + } break; case 1: // cable events - cableEvent(midi[1], midi[2], midi[3]); + if (cableEvent) { + cableEvent(midi[1], midi[2], midi[3]); + } break; case 2: // two bytes system common messages - systemCommonTwoBytes(midi[1], midi[2]); + if (systemCommonTwoBytes) { + systemCommonTwoBytes(midi[1], midi[2]); + } break; case 3: // three bytes system common messages - systemCommonThreeBytes(midi[1], midi[2], midi[3]); + if (systemCommonThreeBytes) { + systemCommonThreeBytes(midi[1], midi[2], midi[3]); + } break; case 4: // SysEx starts or continues sysExBuffer[sysExBufferPos++] = midi[1]; if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); + if (systemExclusive) { + systemExclusive(sysExBuffer, sysExBufferPos, true); + } sysExBufferPos = 0; } sysExBuffer[sysExBufferPos++] = midi[2]; if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); + if (systemExclusive) { + systemExclusive(sysExBuffer, sysExBufferPos, true); + } sysExBufferPos = 0; } sysExBuffer[sysExBufferPos++] = midi[3]; @@ -132,61 +157,93 @@ break; case 5: // SysEx ends with single byte sysExBuffer[sysExBufferPos++] = midi[1]; - systemExclusive(sysExBuffer, sysExBufferPos, false); + if (systemExclusive) { + systemExclusive(sysExBuffer, sysExBufferPos, false); + } sysExBufferPos = 0; break; case 6: // SysEx ends with two bytes sysExBuffer[sysExBufferPos++] = midi[1]; if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); + if (systemExclusive) { + systemExclusive(sysExBuffer, sysExBufferPos, true); + } sysExBufferPos = 0; } sysExBuffer[sysExBufferPos++] = midi[2]; - systemExclusive(sysExBuffer, sysExBufferPos, false); + + if (systemExclusive) { + systemExclusive(sysExBuffer, sysExBufferPos, false); + } sysExBufferPos = 0; break; case 7: // SysEx ends with three bytes sysExBuffer[sysExBufferPos++] = midi[1]; if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); + if (systemExclusive) { + systemExclusive(sysExBuffer, sysExBufferPos, true); + } sysExBufferPos = 0; } sysExBuffer[sysExBufferPos++] = midi[2]; if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); + if (systemExclusive) { + systemExclusive(sysExBuffer, sysExBufferPos, true); + } sysExBufferPos = 0; } sysExBuffer[sysExBufferPos++] = midi[3]; - systemExclusive(sysExBuffer, sysExBufferPos, false); + + if (systemExclusive) { + systemExclusive(sysExBuffer, sysExBufferPos, false); + } sysExBufferPos = 0; break; case 8: - noteOff(midi[1] & 0xf, midi[2], midi[3]); - break; - case 9: - if (midi[3]) { - noteOn(midi[1] & 0xf, midi[2], midi[3]); - } else { + if (noteOff) { noteOff(midi[1] & 0xf, midi[2], midi[3]); } break; + case 9: + if (midi[3]) { + if (noteOn) { + noteOn(midi[1] & 0xf, midi[2], midi[3]); + } + } else { + if (noteOff) { + noteOff(midi[1] & 0xf, midi[2], midi[3]); + } + } + break; case 10: - polyKeyPress(midi[1] & 0xf, midi[2], midi[3]); + if (polyKeyPress) { + polyKeyPress(midi[1] & 0xf, midi[2], midi[3]); + } break; case 11: - controlChange(midi[1] & 0xf, midi[2], midi[3]); + if (controlChange) { + controlChange(midi[1] & 0xf, midi[2], midi[3]); + } break; case 12: - programChange(midi[1] & 0xf, midi[2]); + if (programChange) { + programChange(midi[1] & 0xf, midi[2]); + } break; case 13: - channelPressure(midi[1] & 0xf, midi[2]); + if (channelPressure) { + channelPressure(midi[1] & 0xf, midi[2]); + } break; case 14: - pitchBend(midi[1] & 0xf, midi[2] | (midi[3] << 7)); + if (pitchBend) { + pitchBend(midi[1] & 0xf, midi[2] | (midi[3] << 7)); + } break; case 15: - singleByte(midi[1]); + if (singleByte) { + singleByte(midi[1]); + } break; } } @@ -359,4 +416,4 @@ return false; } -#endif +#endif \ No newline at end of file