Skip to content

Commit

Permalink
Add Custom-MIDI-Input-Element example
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Feb 20, 2024
1 parent 2621008 commit d6a5fb1
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 8 deletions.
3 changes: 2 additions & 1 deletion doxygen/scripts/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@
("Custom-MIDI-Sender", "Customize the type of messages sent by the provided MIDI output elements"),
("Custom-MIDI-Output-Element", "Adding your own MIDI output elements, recreating and then customizing the @ref NoteButton class"),
("Custom-MIDI-Output-Element-Bankable", "Same as previous, but for the @ref Bankable::NoteButton class"),
("Custom-Note-LED-Input-Element-Callback", "Adding your own MIDI input elements, recreating and then customizing the @ref NoteLED class"),
("Custom-MIDI-Input-Element", "Adding your own customizable MIDI input elements, recreating the @ref NoteLED class"),
("Custom-Note-LED-Input-Element-Callback", "Similar to previous, but with PWM output"),
("Custom-Note-LED-Input-Element-Callback-FastLED", "Similar to previous, but with support for FastLED addressable LED strips"),
],
"Other examples": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* This example demonstrates how to create a custom MIDI Input Element similar
* to the NoteLED class.
*
* @boards AVR, AVR USB, Due, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, Teensy 3.x
*
* Connections
* -----------
*
* - None (built-in LED is used)
*
* Behavior
* --------
*
* If a MIDI Note On event for note 0x3C (C4 or middle C) is sent, the built-in
* LED will light up,
* if a Note Off event for that note is sent, the LED will turn off.
* (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.
*
* Alternatively, replace @ref USBMIDI_Interface by @ref USBDebugMIDI_Interface
* and send `90 3C 7F` and/or `80 3C 7F` in the Serial monitor.
*
* Written by PieterP, 2024-02-20
* https://github.com/tttapa/Control-Surface
*/

#include <Control_Surface.h> // Include the Control Surface library

// Instantiate a MIDI over USB interface.
USBMIDI_Interface midi;

// MIDI Input Element that listens for MIDI Note events and that turns on the
// LED if the note value (velocity) is above the threshold, and turns off the
// LED if the value is below the threshold.

class CustomNoteLED
// First, inherit from the MatchingMIDIInputElement base class. Indicate that
// you want to listen to MIDI Note events by specifying the MIDI message type,
// and use a simple 2-byte message matcher, since MIDI Note events have two
// data bytes.
: public MatchingMIDIInputElement<MIDIMessageType::NoteOn,
TwoByteMIDIMatcher> {
public:
// Constructor
CustomNoteLED(pin_t ledPin, MIDIAddress address)
: MatchingMIDIInputElement(address), ledPin(ledPin) {}

// Called once upon initialization, set up the pin mode for the LED.
void begin() override { ExtIO::pinMode(ledPin, OUTPUT); }

// Called when an incoming MIDI Note message matches this element's matcher
// (i.e. it has the right MIDI address, channel and cable).
// The match object that's passed as a parameter contains the velocity value
// of the Note message that matched.
void handleUpdate(TwoByteMIDIMatcher::Result match) override {
ExtIO::digitalWrite(ledPin, match.value >= threshold ? HIGH : LOW);
}

// Called when the input element is reset (e.g. by an "All notes off" MIDI
// event).
void reset() override { ExtIO::digitalWrite(ledPin, LOW); }

private:
pin_t ledPin;
const static uint8_t threshold = 0x01;
};

// Instantiate the LED that will light up when middle C is playing.
CustomNoteLED led {
LED_BUILTIN, // Pin with the LED connected
{MIDI_Notes::C[4], Channel_1}, // Note C4 on MIDI channel 1
};

void setup() {
Control_Surface.begin(); // Initialize Control Surface
}

void loop() {
Control_Surface.loop(); // Update the Control Surface
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* 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 MIDI Note On event for note 0x3C (C4 or middle C) is sent, the 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.)
*
Expand All @@ -25,7 +25,7 @@
*
* Written by PieterP, 2020-03-10
* https://github.com/tttapa/Control-Surface
*/
*/

#include <Control_Surface.h> // Include the Control Surface library

Expand Down
47 changes: 43 additions & 4 deletions examples/examples.dox
Original file line number Diff line number Diff line change
Expand Up @@ -3363,8 +3363,8 @@
* 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 MIDI Note On event for note 0x3C (C4 or middle C) is sent, the 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.)
*
Expand All @@ -3376,7 +3376,7 @@
*
* Written by PieterP, 2020-03-10
* https://github.com/tttapa/Control-Surface
*/
*/

/**
* @example "Custom-USB-MIDI-Backend.ino"
Expand Down Expand Up @@ -3415,6 +3415,43 @@
* https://github.com/tttapa/Control-Surface
*/

/**
* @example "Custom-MIDI-Input-Element.ino"
*
* Custom-MIDI-Input-Element
* =========================
*
* This example demonstrates how to create a custom MIDI Input Element similar
* to the NoteLED class.
*
* @boards AVR, AVR USB, Due, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, Teensy 3.x
*
* Connections
* -----------
*
* - None (built-in LED is used)
*
* Behavior
* --------
*
* If a MIDI Note On event for note 0x3C (C4 or middle C) is sent, the built-in
* LED will light up,
* if a Note Off event for that note is sent, the LED will turn off.
* (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.
*
* Alternatively, replace @ref USBMIDI_Interface by @ref USBDebugMIDI_Interface
* and send `90 3C 7F` and/or `80 3C 7F` in the Serial monitor.
*
* Written by PieterP, 2024-02-20
* https://github.com/tttapa/Control-Surface
*/

/**
* @example "Keyboard-Matrix-BCD.ino"
*
Expand Down Expand Up @@ -3728,8 +3765,10 @@
* (Adding your own MIDI output elements, recreating and then customizing the @ref NoteButton class)
* - @ref Custom-MIDI-Output-Element-Bankable.ino
* (Same as previous, but for the @ref Bankable::NoteButton class)
* - @ref Custom-MIDI-Input-Element.ino
* (Adding your own customizable MIDI input elements, recreating the @ref NoteLED class)
* - @ref Custom-Note-LED-Input-Element-Callback.ino
* (Adding your own MIDI input elements, recreating and then customizing the @ref NoteLED class)
* (Similar to previous, but with PWM output)
* - @ref Custom-Note-LED-Input-Element-Callback-FastLED.ino
* (Similar to previous, but with support for FastLED addressable LED strips)
*
Expand Down

0 comments on commit d6a5fb1

Please sign in to comment.