Control Surface  1.2.0
MIDI Control Surface library for Arduino
Custom-MIDI-Output-Element.ino

Custom-MIDI-Output-Element

This is an example that demonstrates how to extend the library using your own MIDI Output Elements. The example declares a MIDI Output Element that sends MIDI Note events when a push button is pressed or released. It's a simplified version of the NoteButton class.

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

Connections

The internal pull-up resistor for the button will be enabled automatically.

Behavior

Mapping

Select the Arduino as a custom MIDI controller in your DAW, and use the MIDI learn option to assign the button to a function.

Written by PieterP, 2020-02-04
https://github.com/tttapa/Control-Surface

/*
* @brief A class for momentary buttons and switches that send MIDI events.
*
* The button is debounced, and the internal pull-up resistor is enabled.
*
* @see NoteButton
* @see Button
*/
class MyNoteButton : public MIDIOutputElement {
public:
/*
* @brief Create a new MyNoteButton object on the given pin, with the
* given address and velocity.
*
* @param pin
* The digital input pin to read from.
* The internal pull-up resistor will be enabled.
* @param address
* The MIDI address to send to.
* @param velocity
* The MIDI note velocity [0, 127].
*/
MyNoteButton(pin_t pin, const MIDIAddress &address, uint8_t velocity)
: button{pin}, address{address}, velocity{velocity} {}
public:
// Initialize: enable the pull-up resistor for the button
// This method is called once by `Control_Surface.begin()`.
void begin() final override { button.begin(); }
// Update: read the button and send MIDI messages when appropriate.
// This method is called continuously by `Control_Surface.loop()`.
void update() final override {
AH::Button::State state = button.update(); // Read the button
if (state == AH::Button::Falling) { // if pressed
Control_Surface.sendNoteOn(address, velocity); // → note on
} else if (state == AH::Button::Rising) { // if released
Control_Surface.sendNoteOff(address, velocity); // → note off
}
}
private:
AH::Button button;
const MIDIAddress address;
uint8_t velocity;
};
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //
// Instantiate a MIDI over USB interface.
using namespace MIDI_Notes;
// Instantiate a MyNoteButton object
MyNoteButton button = {
5, // Push button on pin 5
{note(C, 4), CHANNEL_1}, // Note C4 on MIDI channel 1
0x7F, // Maximum velocity
};
void setup() {
Control_Surface.begin(); // Initialize Control Surface (calls button.begin())
}
void loop() {
Control_Surface.loop(); // Update the Control Surface (calls button.update())
}
USBMIDI_Interface
A class for MIDI interfaces sending MIDI messages over a USB MIDI connection.
Definition: USBMIDI_Interface.hpp:41
MIDIAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDIAddress.hpp:91
AH::Updatable<>
AH::Button::Falling
@ Falling
Input went from high to low (1,0)
Definition: Button.hpp:56
AH::Button::State
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:53
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::Button
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:18
MIDI_Sender::sendNoteOn
void sendNoteOn(MIDIAddress address, uint8_t velocity)
Send a MIDI Note On event.
Definition: MIDI_Interface.ipp:49
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
AH::Updatable<>::begin
virtual void begin()=0
Initialize this updatable.
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
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
AH::Button::Rising
@ Rising
Input went from low to high (0,1)
Definition: Button.hpp:57
MIDI_Sender::sendNoteOff
void sendNoteOff(MIDIAddress address, uint8_t velocity)
Send a MIDI Note Off event.
Definition: MIDI_Interface.ipp:55
AH::Updatable<>::update
virtual void update()=0
Update this updatable.
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