diff --git a/examples/MIDIUSB_read_with_interrupts/MIDIUSB_read_with_interrupts.ino b/examples/MIDIUSB_read_with_interrupts/MIDIUSB_read_with_interrupts.ino new file mode 100644 index 0000000..b59f86a --- /dev/null +++ b/examples/MIDIUSB_read_with_interrupts/MIDIUSB_read_with_interrupts.ino @@ -0,0 +1,65 @@ +/* + * MIDIUSB_test.ino + * + * Modifed to use interrupts. 12/12/2020 + * Doug Clauder https://github.com/studiohsoftware + * + * Created: 4/6/2015 10:47:08 AM + * Author: gurbrinder grewal + * Modified by Arduino LLC (2015) + */ + +#include "MIDIUSB.h" + +// First parameter is the event type (0x09 = note on, 0x08 = note off). +// Second parameter is note-on/note-off, combined with the channel. +// Channel can be anything between 0-15. Typically reported to the user as 1-16. +// Third parameter is the note number (48 = middle C). +// Fourth parameter is the velocity (64 = normal, 127 = fastest). + +void noteOn(byte channel, byte pitch, byte velocity) { + midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity}; + MidiUSB.sendMIDI(noteOn); +} + +void noteOff(byte channel, byte pitch, byte velocity) { + midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity}; + MidiUSB.sendMIDI(noteOff); +} + +void setup() { + Serial1.begin(115200); + MidiUSB.attachInterrupt(onEvent); +} + +// First parameter is the event type (0x0B = control change). +// Second parameter is the event type, combined with the channel. +// Third parameter is the control number number (0-119). +// Fourth parameter is the control value (0-127). + +void controlChange(byte channel, byte control, byte value) { + midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value}; + MidiUSB.sendMIDI(event); +} + +void loop() { + +} + +void onEvent(int ep) { + USB->DEVICE.INTENCLR.reg = 0xFF; //SAMD interrupts continuously without this. + midiEventPacket_t rx; + do { + rx = MidiUSB.read(); + if (rx.header != 0) { + Serial1.print("Received: "); + Serial1.print(rx.header, HEX); + Serial1.print("-"); + Serial1.print(rx.byte1, HEX); + Serial1.print("-"); + Serial1.print(rx.byte2, HEX); + Serial1.print("-"); + Serial1.println(rx.byte3, HEX); + } + } while (rx.header != 0); +} diff --git a/src/MIDIUSB.h b/src/MIDIUSB.h index 487eaea..f797e87 100644 --- a/src/MIDIUSB.h +++ b/src/MIDIUSB.h @@ -224,6 +224,8 @@ class MIDI_ : public PluggableUSBModule bool setup(USBSetup& setup); /// MIDI Device short name, defaults to "MIDI" and returns a length of 4 chars uint8_t getShortName(char* name); + void (*interruptCallback)(int /* ep */); + virtual void handleEndpoint(int ep) { if (interruptCallback) interruptCallback(ep); } public: /// Creates a MIDI USB device with 2 endpoints @@ -240,6 +242,8 @@ class MIDI_ : public PluggableUSBModule size_t write(const uint8_t *buffer, size_t size); /// NIY operator bool(); + void attachInterrupt(void(*function)(int /* ep */)){interruptCallback = function;}; + }; extern MIDI_ MidiUSB;