From 3be85823c4f3cc375300ded60bbc4ba153358497 Mon Sep 17 00:00:00 2001 From: dougcl Date: Sat, 12 Dec 2020 22:40:45 -0800 Subject: [PATCH 1/2] Add interrupt handler and example sketch. --- .../MIDIUSB_read_with_interrupts.ino | 62 +++++++++++++++++++ src/MIDIUSB.h | 4 ++ 2 files changed, 66 insertions(+) create mode 100644 examples/MIDIUSB_read_with_interrupts/MIDIUSB_read_with_interrupts.ino 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..8482e79 --- /dev/null +++ b/examples/MIDIUSB_read_with_interrupts/MIDIUSB_read_with_interrupts.ino @@ -0,0 +1,62 @@ +/* + * MIDIUSB_test.ino + * + * 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; From 74bb374ecbb72d92a52db2822eb7bb7b34a84d09 Mon Sep 17 00:00:00 2001 From: dougcl Date: Sun, 13 Dec 2020 00:47:08 -0800 Subject: [PATCH 2/2] Add modification date to header. --- .../MIDIUSB_read_with_interrupts.ino | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/MIDIUSB_read_with_interrupts/MIDIUSB_read_with_interrupts.ino b/examples/MIDIUSB_read_with_interrupts/MIDIUSB_read_with_interrupts.ino index 8482e79..b59f86a 100644 --- a/examples/MIDIUSB_read_with_interrupts/MIDIUSB_read_with_interrupts.ino +++ b/examples/MIDIUSB_read_with_interrupts/MIDIUSB_read_with_interrupts.ino @@ -1,6 +1,9 @@ /* * 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)