Control Surface  1.1.1
MIDI Control Surface library for Arduino
Control_Surface_Class.cpp
Go to the documentation of this file.
2 #include <AH/Debug/Debug.hpp>
12 #include <Selectors/Selector.hpp>
13 
14 #include <Arduino.h>
15 
17 
19 
21  static Control_Surface_ instance;
22  return instance;
23 }
24 
26 #if defined(ARDUINO) && defined(DEBUG_OUT)
28  delay(250);
29 #endif
31  ExtendedIOElement::beginAll();
32  MIDI().begin(); // initialize the MIDI interface
33  MIDI().setCallbacks(this);
34  DisplayInterface::beginAll(); // initialize all displays
46 }
47 
53  updateInputs();
54  if (displayTimer)
56 }
57 
60  if (midi == nullptr)
61  FATAL_ERROR(F("No default MIDI interface is selected."), 0xDEAD);
62 
63  return *midi;
64 }
65 
67  MIDI_Interface &midi = MIDI();
68  midi.update();
69 }
70 
72  ChannelMessage midichmsg = midi.getChannelMessage();
73  ChannelMessageMatcher midimsg = {midichmsg};
74 
75 #ifdef DEBUG_MIDI_PACKETS
76  // TODO: print CN
77  if (midimsg.type != PROGRAM_CHANGE && midimsg.type != CHANNEL_PRESSURE)
78  DEBUG(">>> " << hex << midichmsg.header << ' ' << midimsg.data1 << ' '
79  << midimsg.data2 << dec);
80  else
81  DEBUG(">>> " << hex << midichmsg.header << ' ' << midimsg.data1 << dec);
82 #endif
83 
84  // If the Channel Message callback exists, call it to see if we have to
85  // continue handling it.
87  return;
88 
89  if (midimsg.type == CC && midimsg.data1 == 0x79) {
90  // Reset All Controllers
91  DEBUG(F("Reset All Controllers"));
94  } else if (midimsg.type == CC && midimsg.data1 == MIDI_CC::All_Notes_Off) {
96  } else {
97  if (midimsg.type == CC) {
98  // Control Change
99  DEBUGFN(F("Updating CC elements with new MIDI message."));
101 
102  } else if (midimsg.type == NOTE_OFF || midimsg.type == NOTE_ON) {
103  // Note
104  DEBUGFN(F("Updating Note elements with new MIDI message."));
106 
107  } else if (midimsg.type == CHANNEL_PRESSURE) {
108  // Channel Pressure
109  DEBUGFN(F("Updating Channel Pressure elements with new "
110  "MIDI message."));
112  } else if (midimsg.type == PROGRAM_CHANGE) {
113  // Channel Pressure
114  DEBUGFN(F("Updating Program Change elements with new "
115  "MIDI message."));
117  }
118  }
119 }
120 
122  // System Exclusive
123  SysExMessage msg = midi.getSysExMessage();
124 #ifdef DEBUG_MIDI_PACKETS
125  const uint8_t *data = msg.data;
126  size_t len = msg.length;
127  // TODO: print CN
128  DEBUG_OUT << hex;
129  for (size_t i = 0; i < len; i++)
130  DEBUG_OUT << data[i] << ' ';
131  DEBUG_OUT << dec << endl;
132 #endif
133  // If the SysEx Message callback exists, call it to see if we have to
134  // continue handling it.
136  return;
138 }
139 
141  uint8_t message) {
142  RealTimeMessage rtMessage = {message, midi.getCN()};
143  // If the Real-Time Message callback exists, call it to see if we have to
144  // continue handling it.
146  return;
147  // TODO: handle Real-Time input
148 }
149 
156 }
157 
159  DisplayInterface *previousDisplay = nullptr;
160  for (DisplayElement &displayElement : DisplayElement::getAll()) {
161  DisplayInterface *thisDisplay = &displayElement.getDisplay();
162  if (!thisDisplay->isEnabled())
163  continue;
164  if (thisDisplay != previousDisplay) {
165  if (previousDisplay)
166  previousDisplay->display();
167  previousDisplay = thisDisplay;
168  thisDisplay->clearAndDrawBackground();
169  }
170  displayElement.draw();
171  }
172  if (previousDisplay)
173  previousDisplay->display();
174 }
175 
177 
ExtendedIOElement.hpp
MIDIInputElementPC::updateAllWith
static void updateAllWith(const ChannelMessageMatcher &midimsg)
Update all MIDIInputElementPC elements with a new MIDI message.
Definition: MIDIInputElementPC.hpp:70
ChannelMessageMatcher::data1
uint8_t data1
Definition: ChannelMessageMatcher.hpp:20
AH::Updatable
A super class for object that have to be updated regularly.
Definition: Updatable.hpp:25
DisplayInterface::display
virtual void display()=0
Write the frame buffer to the display.
CHANNEL_PRESSURE
const uint8_t CHANNEL_PRESSURE
Definition: MIDI_Parser.hpp:21
DisplayElement
An interface for elements that draw to a display.
Definition: DisplayElement.hpp:11
MIDI_Interface::getDefault
static MIDI_Interface * getDefault()
Return the default MIDI interface.
Definition: MIDI_Interface.cpp:18
DisplayInterface::isEnabled
bool isEnabled()
Check if this display is enabled.
Definition: DisplayInterface.hpp:110
Control_Surface_::updateDisplays
void updateDisplays()
Clear, draw and display all displays.
Definition: Control_Surface_Class.cpp:158
ChannelMessageMatcher::type
uint8_t type
Definition: ChannelMessageMatcher.hpp:18
MIDIInputElementCC::updateAllWith
static void updateAllWith(const ChannelMessageMatcher &midimsg)
Update all MIDIInputElementCC elements with a new MIDI message.
Definition: MIDIInputElementCC.hpp:74
SysExMessage
Definition: MIDI_Parser.hpp:64
MIDI_Interface::update
virtual void update()=0
Read the MIDI interface and call the callback if a message is received.
Parsing_MIDI_Interface::getCN
uint8_t getCN() const
Return the cable number of the received message.
Definition: MIDI_Interface.cpp:128
MIDIInputElementSysEx.hpp
SysExMessage::data
const uint8_t * data
Definition: MIDI_Parser.hpp:72
Parsing_MIDI_Interface::getSysExMessage
SysExMessage getSysExMessage() const
Return the received system exclusive message.
Definition: MIDI_Interface.cpp:124
MIDIInputElementNote::beginAll
static void beginAll()
Initialize all MIDIInputElementNote elements.
Definition: MIDIInputElementNote.hpp:48
Parsing_MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:188
DisplayInterface
An interface for displays.
Definition: DisplayInterface.hpp:14
ChannelMessageMatcher::data2
uint8_t data2
Definition: ChannelMessageMatcher.hpp:21
MIDIOutputElement.hpp
DisplayInterface::beginAll
static void beginAll()
Initialize all displays.
Definition: DisplayInterface.cpp:61
MIDIInputElementCC::resetAll
static void resetAll()
Reset all MIDIInputElementCC elements to their initial state.
Definition: MIDIInputElementCC.hpp:66
MIDIInputElementCC::updateAll
static void updateAll()
Update all MIDIInputElementCC elements.
Definition: MIDIInputElementCC.hpp:58
MIDI_Interface::setCallbacks
virtual void setCallbacks(MIDI_Callbacks *cb)=0
Set the callbacks that will be called when a MIDI message is received.
MIDI_CC::All_Notes_Off
constexpr uint8_t All_Notes_Off
Definition: Control_Change.hpp:107
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
MIDIInputElementChannelPressure::updateAllWith
static void updateAllWith(const ChannelMessageMatcher &midimsg)
Update all MIDIInputElementChannelPressure elements with a new MIDI message.
Definition: MIDIInputElementChannelPressure.hpp:71
ChannelMessage::header
uint8_t header
Definition: MIDI_Parser.hpp:48
MIDIInputElementPC.hpp
ChannelMessageMatcher
Struct for easily matching MIDI messages.
Definition: ChannelMessageMatcher.hpp:10
AH::FilteredAnalog
A class that reads and filters an analog input.
Definition: FilteredAnalog.hpp:55
Control_Change.hpp
Control_Surface_::sysExMessageCallback
SysExMessageCallback sysExMessageCallback
Definition: Control_Surface_Class.hpp:128
Control_Surface_::MIDI
MIDI_Interface & MIDI()
Get the MIDI interface of the Control Surface.
Definition: Control_Surface_Class.cpp:58
Control_Surface_::potentiometerTimer
Timer< micros > potentiometerTimer
A timer to know when to update the analog inputs.
Definition: Control_Surface_Class.hpp:98
AH::ExtendedIOElement
An abstract base class for Extended Input/Output elements.
Definition: ExtendedIOElement.hpp:62
FATAL_ERROR
#define FATAL_ERROR(msg, errc)
Print the error message and error code, and stop the execution.
Definition: Error.hpp:60
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
AH::Timer::begin
void begin()
Initialize the timer.
Definition: MillisMicrosTimer.hpp:41
Control_Surface_::updateMidiInput
void updateMidiInput()
Update all MIDI interfaces to receive new MIDI events.
Definition: Control_Surface_Class.cpp:66
AH::defaultBaudRate
constexpr unsigned long defaultBaudRate
The default baud rate for debug output.
Definition: AH/Settings/Settings.hpp:36
Control_Surface_Class.hpp
Control_Surface_::loop
void loop()
Update all MIDI elements, send MIDI events and read MIDI input.
Definition: Control_Surface_Class.cpp:48
Control_Surface_::getInstance
static Control_Surface_ & getInstance()
Return the static Control_Surface_ instance.
Definition: Control_Surface_Class.cpp:20
Control_Surface_::onSysExMessage
void onSysExMessage(Parsing_MIDI_Interface &midi) override
The callback to be called when a MIDI System Exclusive message is received.
Definition: Control_Surface_Class.cpp:121
MIDIInputElementNote::resetAll
static void resetAll()
Reset all MIDIInputElementNote elements to their initial state.
Definition: MIDIInputElementNote.hpp:70
Parsing_MIDI_Interface::getChannelMessage
ChannelMessage getChannelMessage()
Return the received channel message.
Definition: MIDI_Interface.cpp:120
MIDIInputElementNote.hpp
Control_Surface
Control_Surface_ & Control_Surface
A predefined instance of the Control Surface to use in the Arduino sketches.
Definition: Control_Surface_Class.cpp:176
RealTimeMessage
Definition: MIDI_Parser.hpp:77
Control_Surface_::displayTimer
Timer< micros > displayTimer
A timer to know when to refresh the displays.
Definition: Control_Surface_Class.hpp:100
MIDIInputElementCC.hpp
MIDI_Interface::begin
virtual void begin()
Initialize the MIDI Interface.
Definition: MIDI_Interface.hpp:32
NOTE_ON
const uint8_t NOTE_ON
Definition: MIDI_Parser.hpp:17
MIDIInputElementNote::updateAllWith
static void updateAllWith(const ChannelMessageMatcher &midimsg)
Update all MIDIInputElementNote elements with a new MIDI message.
Definition: MIDIInputElementNote.hpp:82
PROGRAM_CHANGE
const uint8_t PROGRAM_CHANGE
Definition: MIDI_Parser.hpp:20
MIDIInputElementCC::beginAll
static void beginAll()
Initialize all MIDIInputElementCC elements.
Definition: MIDIInputElementCC.hpp:50
DEBUG_OUT
#define DEBUG_OUT
The debug output.
Definition: AH/Settings/Settings.hpp:26
MIDIInputElementPC::updateAll
static void updateAll()
Update all MIDIInputElementPC elements.
Definition: MIDIInputElementPC.hpp:58
Selector.hpp
NOTE_OFF
const uint8_t NOTE_OFF
Definition: MIDI_Parser.hpp:16
hex
Print & hex(Print &printer)
Definition: PrintStream.cpp:62
MIDIInputElementSysEx::updateAll
static void updateAll()
Update all MIDIInputElementSysEx elements.
Definition: MIDIInputElementSysEx.hpp:67
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23
Control_Surface_::onRealtimeMessage
void onRealtimeMessage(Parsing_MIDI_Interface &midi, uint8_t message) override
The callback to be called when a MIDI Real-Time message is received.
Definition: Control_Surface_Class.cpp:140
DEBUGFN
#define DEBUGFN(x)
Print an expression and its function (function name and line number) to the debug output if debugging...
Definition: Debug.hpp:93
Control_Surface_::onChannelMessage
void onChannelMessage(Parsing_MIDI_Interface &midi) override
The callback to be called when a MIDI channel message is received.
Definition: Control_Surface_Class.cpp:71
DisplayInterface::clearAndDrawBackground
void clearAndDrawBackground()
Clear the frame buffer, and draw the custom background.
Definition: DisplayInterface.hpp:117
endl
Print & endl(Print &printer)
Definition: PrintStream.cpp:27
DisplayElement::getAll
static DoublyLinkedList< DisplayElement > & getAll()
Get the list of all DisplayElement instances.
Definition: DisplayElement.hpp:41
MIDIInputElementSysEx::updateAllWith
static void updateAllWith(SysExMessage midimsg)
Update all MIDIInputElementSysEx elements with a new MIDI message.
Definition: MIDIInputElementSysEx.hpp:90
CC
const uint8_t CC
Definition: MIDI_Parser.hpp:19
MIDIInputElementChannelPressure::resetAll
static void resetAll()
Reset all MIDIInputElementChannelPressure elements to their initial state.
Definition: MIDIInputElementChannelPressure.hpp:50
ChannelMessage
Definition: MIDI_Parser.hpp:47
Debug.hpp
Control_Surface_::updateInputs
void updateInputs()
Update all MIDIInputElements.
Definition: Control_Surface_Class.cpp:150
MIDIInputElementSysEx::beginAll
static void beginAll()
Initialize all MIDIInputElementSysEx elements.
Definition: MIDIInputElementSysEx.hpp:56
MIDIInputElementPC::beginAll
static void beginAll()
Definition: MIDIInputElementPC.hpp:37
Control_Surface_::realTimeMessageCallback
RealTimeMessageCallback realTimeMessageCallback
Definition: Control_Surface_Class.hpp:129
MIDIInputElementChannelPressure::beginAll
static void beginAll()
Definition: MIDIInputElementChannelPressure.hpp:38
MIDIInputElementNote::updateAll
static void updateAll()
Update all MIDIInputElementNote elements.
Definition: MIDIInputElementNote.hpp:59
MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:16
SysExMessage::length
uint8_t length
Definition: MIDI_Parser.hpp:73
dec
Print & dec(Print &printer)
Definition: PrintStream.cpp:77
MIDIInputElementChannelPressure::updateAll
static void updateAll()
Update all MIDIInputElementChannelPressure elements.
Definition: MIDIInputElementChannelPressure.hpp:59
DEBUG
#define DEBUG(x)
Print an expression to the debug output if debugging is enabled.
Definition: Debug.hpp:75
FilteredAnalog.hpp
Control_Surface_
This class ensures initialization, updating, and interaction between all other classes,...
Definition: Control_Surface_Class.hpp:26
Control_Surface_::channelMessageCallback
ChannelMessageCallback channelMessageCallback
Definition: Control_Surface_Class.hpp:127
MIDIInputElementChannelPressure.hpp
Control_Surface_::begin
void begin()
Initialize the Control_Surface.
Definition: Control_Surface_Class.cpp:25