A simple 5-pin MIDI helper library.
Revision 0:a6b78fdc6619, committed 2015-10-22
- Comitter:
- mpetrut
- Date:
- Thu Oct 22 17:15:51 2015 +0000
- Commit message:
- Initial commit;
Changed in this revision
MIDI_5Pin.cpp | Show annotated file Show diff for this revision Revisions of this file |
MIDI_5Pin.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r a6b78fdc6619 MIDI_5Pin.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MIDI_5Pin.cpp Thu Oct 22 17:15:51 2015 +0000 @@ -0,0 +1,46 @@ +/* Simple mbed 5-pin DIN to USB MIDI Converter library +Marius Petrut, 2015 +*/ + +#include "MIDI_5Pin.h" +#include "mbed.h" + +#define NOTE_ON 144 +#define CONT_CTRL 176 + +MIDI_5Pin::MIDI_5Pin(PinName txPin, PinName rxPin) : + _pc(USBTX, USBRX), _uart5pin(txPin, rxPin) { + _pc.baud(115200); + // 31250 is the official specification + _uart5pin.baud(31250); + } + +void MIDI_5Pin::write(char command, char param1, char param2) { + _uart5pin.putc(command); + _uart5pin.putc(param1); + _uart5pin.putc(param2); +} + +void MIDI_5Pin::noteOn(char note, char velocity) { + _uart5pin.putc(NOTE_ON); + _uart5pin.putc(note); + _uart5pin.putc(velocity); +} + +void MIDI_5Pin::noteOff(char note) { + _uart5pin.putc(NOTE_ON); + _uart5pin.putc(note); + _uart5pin.putc(0); +} + +void MIDI_5Pin::contCtrl(char ccNumber, char value) { + _uart5pin.putc(CONT_CTRL); + _uart5pin.putc(ccNumber); + _uart5pin.putc(value); +} + +void MIDI_5Pin::read() { + if (_uart5pin.readable()) { + _pc.putc(_uart5pin.getc()); + } +} \ No newline at end of file
diff -r 000000000000 -r a6b78fdc6619 MIDI_5Pin.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MIDI_5Pin.h Thu Oct 22 17:15:51 2015 +0000 @@ -0,0 +1,62 @@ +/** Simple mbed 5-pin DIN to USB MIDI Converter library + * Marius Petrut, 2015 + */ + +#ifndef MBED_MIDI_5PIN_H +#define MBED_MIDI_5PIN_H + +#include "mbed.h" + +/** The 5-pin MIDI helper class + */ +class MIDI_5Pin { +public: + /** Create a Midi5Pin object with the specified + * transmit and receive pins + * + * @param txPin Transmit pin + * @param rxPin Receive pin + */ + MIDI_5Pin(PinName txPin, PinName rxPin); + + /** Send a command to the 5-pin output port + * + * @param command The MIDI command byte + * @param param1 First parameter (depends on command) + * @param param2 Second parameter (depends on command) + */ + void write(char command, char param1, char param2); + + /** Send a noteOn MIDI message to the 5-pin output + * + * @param note MIDI note number + * @param velocity MIDI note velocity (loudness) + */ + void noteOn(char note, char velocity); + + /** Send a noteOff MIDI message to the 5-pin output + * + * @param note MIDI note number + */ + void noteOff(char note); + + /** Send a continuous control message to the 5-pin output + * + * @param ccNumber The continuous control message number + * @param value Value of the continuous control message + */ + void contCtrl(char ccNumber, char value); + + /** Read from the 5-pin input connection and send + * it to the PC through the USB virtual com port + * + */ + void read(); + +private: + Serial _pc; + Serial _uart5pin; +}; + +#endif + \ No newline at end of file