Skip to content

Instantly share code, notes, and snippets.

@tttapa
Created January 4, 2020 23:27
Show Gist options
  • Save tttapa/2de36cc99364728cf01b56e5e7dc9465 to your computer and use it in GitHub Desktop.
Save tttapa/2de36cc99364728cf01b56e5e7dc9465 to your computer and use it in GitHub Desktop.
#include <Control_Surface.h>
USBMIDI_Interface midi; // MIDI Interface to use
// Struct with callbacks that are called when MIDI data comes in
struct MyCallback final : MIDI_Callbacks {
// Function is called when MIDI Channel Message is received.
void onChannelMessage(Parsing_MIDI_Interface &midi) override {
// Handler for messages of type:
// - B0 0z mm (most significant 7 bits)
// - B0 2z ll (least significant 7 bits)
// Get the MIDI message from the MIDI interface
ChannelMessageMatcher msg = midi.getChannelMessage();
if (msg.type != CC) // Check message type
return; // only interrested in Control Change
if (msg.channel != 0) // Check channel
return; // only interrested in channel 1
// Check that controller number (data1) is either 0x2z or 0x0z
uint8_t high_nibble = msg.data1 & 0xF0;
if (high_nibble != 0x00 && high_nibble != 0x20)
return; // data1 isn't 0z or 2z
uint8_t z = msg.data1 & 0x0F;
bool is_msb = high_nibble == 0x00;
uint8_t value = msg.data2;
if (is_msb) // replace MSB with value
values[z] = (values[z] & 0x007F) | (value << 7);
else // replace LSB with value
values[z] = (values[z] & 0x3F80) | (value << 0);
if (!is_msb) // print full value if LSB received
Serial << "z=" << z << ", data=" << values[z] << endl;
}
uint16_t values[16] = {}; // save the received values
};
MyCallback callback;
void setup() {
midi.setCallbacks(callback);
midi.begin();
Serial.begin(115200);
}
void loop() {
midi.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment