Control Surface  1.2.0
MIDI Control Surface library for Arduino
Custom-Note-LED-Input-Element-Callback.ino

Custom-Note-LED-Input-Element-Callback

This example demonstrates how to attach custom callbacks to Note or Control Change MIDI Input Elements.

Boards:
AVR, AVR USB, Due, Nano 33, Teensy 3.x

Connections

Behavior

If a MIDI Note On event for note 0x3C (C4 or middle C) is sent, the built-in LED will light up at full brightness, if a Note Off event for that note is sent, the LED will light dimly.
(A Note On event with a velocity of zero also counts as a Note Off event.)

Mapping

Route the MIDI output of a MIDI keyboard to the Arduino's MIDI input. Then play a middle C on the keyboard.

Written by PieterP, 2020-03-10
https://github.com/tttapa/Control-Surface

#include <Control_Surface.h> // Include the Control Surface library
// Instantiate a MIDI over USB interface.
using namespace MIDI_Notes;
// Callback for Note or CC range or value input that turns on the LED at
// full brightness if the note or controller value is above the threshold,
// and turns on the LED at a lower brightness if the value is below the
// threshold.
class CustomLEDCallback : public SimpleNoteCCValueCallback {
public:
// Constructor
CustomLEDCallback(pin_t ledPin, uint8_t lowBrightness)
: ledPin(ledPin), lowBrightness(lowBrightness) {}
// Called once upon initialization, set up the pin mode for the LED,
// and output the initial value.
void begin(const INoteCCValue &t) override {
updateAll(t);
}
// Called when the value of a note or controller value changes.
// A MIDI Input Element can listen to a range of notes or
// controllers, that's why the index of the note or controller
// that changed is passed as a parameter.
// In this example, only a single LED is used, so the index
// doesn't really matter, it's used only to get the value of
// the note/controller.
void update(const INoteCCValue &t, uint8_t index) override {
uint8_t value = t.getValue(index);
bool state = value > threshold;
state ? AH::ExtIO::digitalWrite(ledPin, HIGH)
: AH::ExtIO::analogWrite(ledPin, lowBrightness);
}
// If you have an action in your custom callback that can be implemented more
// efficiently when you need to update all values or outputs, you can override
// the `updateAll` method.
// It is called upon initialization (see the begin method) and when the bank
// of a MIDI Input Element changes.
// For example, turning off all LEDs in an LED strip can be done more
// efficiently than turning off each LED individually.
private:
pin_t ledPin;
uint8_t lowBrightness;
const static uint8_t threshold = 0x00;
};
// Create a type alias fore the MIDI Note Input Element that uses
// the custom callback defined above.
using CustomNoteValueLED =
// Instantiate the LED that will light up when middle C is playing
CustomNoteValueLED led = {
{note(C, 4), CHANNEL_1}, // Note C4 on MIDI channel 1
{3, 10}, // CustomLEDCallback constructor
//│ └─── intensity when off
//└────── pin with the LED connected (PWM capable)
};
void setup() {
Control_Surface.begin(); // Initialize Control Surface
}
void loop() {
Control_Surface.loop(); // Update the Control Surface
}
USBMIDI_Interface
A class for MIDI interfaces sending MIDI messages over a USB MIDI connection.
Definition: USBMIDI_Interface.hpp:41
AH::ExtIO::analogWrite
void analogWrite(pin_t pin, analog_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:86
GenericNoteCCRange
Definition: NoteCCRange.hpp:156
INoteCCValue
Interface for NoteCCValue objects: provides getters for the velocity or controller values.
Definition: NoteCCRange.hpp:13
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
SimpleNoteCCValueCallback
A callback for NoteCCRange with an action that can be implemented by the user.
Definition: NoteCCRange.hpp:46
AH::ExtIO::digitalWrite
void digitalWrite(pin_t pin, PinStatus_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:48
HIGH
const PinStatus_t HIGH
Definition: ExtendedInputOutput.hpp:56
Control_Surface.h
The main header file that includes all Control-Surface header files.
Control_Surface_::loop
void loop()
Update all MIDI elements, send MIDI events and read MIDI input.
Definition: Control_Surface_Class.cpp:68
Control_Surface
Control_Surface_ & Control_Surface
A predefined instance of the Control Surface to use in the Arduino sketches.
Definition: Control_Surface_Class.cpp:203
OUTPUT
const PinMode_t OUTPUT
Definition: ExtendedInputOutput.hpp:60
MIDI_Notes::note
constexpr int8_t note(int8_t note, int8_t numOctave)
Get the MIDI note in the given octave.
Definition: Notes.hpp:35
INoteCCValue::getValue
virtual uint8_t getValue(uint8_t index) const =0
Get the velocity or controller value for the given index in the range.
AH::ExtIO::pinMode
void pinMode(pin_t pin, PinMode_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:36
MIDI_Notes
MIDI note names.
Definition: Notes.hpp:16
CHANNEL_1
constexpr Channel CHANNEL_1
Definition: Channel.hpp:111
MIDI_Notes::C
constexpr int8_t C
Definition: Notes.hpp:18
Control_Surface_::begin
void begin()
Initialize the Control_Surface.
Definition: Control_Surface_Class.cpp:25