Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attachInterrupt to MIDIUSB.h #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 4 additions & 0 deletions src/MIDIUSB.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down