Control Surface  1.1.1
MIDI Control Surface library for Arduino
MIDI_Interface.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Def/Def.hpp>
6 
8 
9 constexpr auto MIDI_BAUD = 31250;
10 
11 class MIDI_Callbacks;
12 
17  protected:
22 
23  public:
27  virtual ~MIDI_Interface();
28 
32  virtual void begin() {}
33 
46  void send(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2);
47 
58  void send(uint8_t m, uint8_t c, uint8_t d1);
59 
74  void sendOnCable(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2, uint8_t cn);
75 
88  void sendOnCable(uint8_t m, uint8_t c, uint8_t d1, uint8_t cn);
89 
98  void sendOnCable(uint8_t r, uint8_t cn);
99 
101  void sendNoteOn(MIDICNChannelAddress address, uint8_t velocity);
103  void sendNoteOff(MIDICNChannelAddress address, uint8_t velocity);
105  void sendKP(MIDICNChannelAddress address, uint8_t pressure);
107  void sendCC(MIDICNChannelAddress address, uint8_t value);
109  void sendPC(MIDICNChannelAddress address);
111  void sendPC(MIDICNChannel address, uint8_t value);
113  void sendCP(MIDICNChannel address, uint8_t pressure);
115  void sendPB(MIDICNChannel address, uint16_t value);
117  void send(SysExMessage message);
119  template <size_t N>
120  void send(const uint8_t (&sysexdata)[N], uint8_t cn = 0) {
121  send(SysExMessage{sysexdata, N, cn});
122  }
124  void send(uint8_t rt, uint8_t cn = 0);
125 
130  virtual void update() = 0;
131 
135  static MIDI_Interface *getDefault();
136 
140  void setAsDefault();
141 
149  virtual void setCallbacks(MIDI_Callbacks *cb) = 0;
150 
160 
164  virtual void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2,
165  uint8_t cn) = 0;
169  virtual void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t cn) = 0;
170 
174  virtual void sendImpl(const uint8_t *data, size_t length, uint8_t cn) = 0;
175 
179  virtual void sendImpl(uint8_t rt, uint8_t cn) = 0;
180 
181  private:
183 };
184 
189  protected:
199 
200  public:
201  MIDI_Parser &getParser() { return parser; }
202 
207 
212 
216  uint8_t getCN() const;
217 
218  void update() override;
219 
220  void setCallbacks(MIDI_Callbacks *cb) override { this->callbacks = cb; }
222 
223  protected:
224  bool dispatchMIDIEvent(MIDI_read_t event);
225 
226  private:
230  virtual MIDI_read_t read() = 0;
231 
232  void onRealtimeMessage(uint8_t message);
233 
234  void onChannelMessage();
235 
236  void onSysExMessage();
237 
238  private:
241 };
242 
243 // LCOV_EXCL_START
248  public:
249  virtual void onChannelMessage(Parsing_MIDI_Interface &midi) { (void)midi; }
250  virtual void onSysExMessage(Parsing_MIDI_Interface &midi) { (void)midi; }
252  uint8_t message) {
253  (void)midi;
254  (void)message;
255  }
256 
257  virtual ~MIDI_Callbacks() = default;
258 };
259 // LCOV_EXCL_STOP
260 
MIDI_BAUD
constexpr auto MIDI_BAUD
Definition: MIDI_Interface.hpp:9
MIDI_Interface::DefaultMIDI_Interface
static MIDI_Interface * DefaultMIDI_Interface
Definition: MIDI_Interface.hpp:182
MIDI_Interface::getDefault
static MIDI_Interface * getDefault()
Return the default MIDI interface.
Definition: MIDI_Interface.cpp:18
MIDI_Interface::update
virtual void update()=0
Read the MIDI interface and call the callback if a message is received.
SysExMessage
Definition: MIDI_Parser.hpp:64
Parsing_MIDI_Interface::getCN
uint8_t getCN() const
Return the cable number of the received message.
Definition: MIDI_Interface.cpp:128
MIDI_Interface::send
void send(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2)
Send a 3-byte MIDI packet.
Definition: MIDI_Interface.cpp:22
Parsing_MIDI_Interface::parser
MIDI_Parser & parser
Definition: MIDI_Interface.hpp:239
Parsing_MIDI_Interface::read
virtual MIDI_read_t read()=0
Parsing_MIDI_Interface::getSysExMessage
SysExMessage getSysExMessage() const
Return the received system exclusive message.
Definition: MIDI_Interface.cpp:124
MIDI_Interface::MIDI_Interface
MIDI_Interface()
Constructor.
Definition: MIDI_Interface.cpp:5
Parsing_MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:188
Parsing_MIDI_Interface::dispatchMIDIEvent
bool dispatchMIDIEvent(MIDI_read_t event)
Definition: MIDI_Interface.cpp:143
MIDI_read_t
MIDI_read_t
Definition: MIDI_Parser.hpp:29
MIDI_Parser
Definition: MIDI_Parser.hpp:84
Def.hpp
MIDICNChannelAddress.hpp
MIDI_Interface::setCallbacks
virtual void setCallbacks(MIDI_Callbacks *cb)=0
Set the callbacks that will be called when a MIDI message is received.
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
Parsing_MIDI_Interface::onChannelMessage
void onChannelMessage()
Definition: MIDI_Interface.cpp:159
MIDI_Interface::sendCP
void sendCP(MIDICNChannel address, uint8_t pressure)
Send a MIDI Channel Pressure event.
Definition: MIDI_Interface.cpp:90
MIDI_Interface::sendKP
void sendKP(MIDICNChannelAddress address, uint8_t pressure)
Send a MIDI Key Pressure event.
Definition: MIDI_Interface.cpp:70
MIDI_Interface::sendNoteOn
void sendNoteOn(MIDICNChannelAddress address, uint8_t velocity)
Send a MIDI Note On event.
Definition: MIDI_Interface.cpp:58
MIDI_Callbacks::onSysExMessage
virtual void onSysExMessage(Parsing_MIDI_Interface &midi)
Definition: MIDI_Interface.hpp:250
MIDI_Interface::setCallbacks
void setCallbacks(MIDI_Callbacks &cb)
Set the callbacks that will be called when a MIDI message is received.
Definition: MIDI_Interface.hpp:159
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
MIDI_Callbacks::onRealtimeMessage
virtual void onRealtimeMessage(Parsing_MIDI_Interface &midi, uint8_t message)
Definition: MIDI_Interface.hpp:251
Parsing_MIDI_Interface::update
void update() override
Read the MIDI interface and call the callback if a message is received.
Definition: MIDI_Interface.cpp:132
Parsing_MIDI_Interface::Parsing_MIDI_Interface
Parsing_MIDI_Interface(MIDI_Parser &parser)
Construct a MIDI interface with the given parser.
Definition: MIDI_Interface.cpp:117
MIDICNChannelAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDICNChannelAddress.hpp:82
Parsing_MIDI_Interface::getChannelMessage
ChannelMessage getChannelMessage()
Return the received channel message.
Definition: MIDI_Interface.cpp:120
MIDI_Callbacks::onChannelMessage
virtual void onChannelMessage(Parsing_MIDI_Interface &midi)
Definition: MIDI_Interface.hpp:249
MIDI_Interface::sendImpl
virtual void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2, uint8_t cn)=0
Low-level function for sending a 3-byte MIDI message.
MIDI_Interface::sendPB
void sendPB(MIDICNChannel address, uint16_t value)
Send a MIDI Pitch Bend event.
Definition: MIDI_Interface.cpp:95
MIDI_Interface::begin
virtual void begin()
Initialize the MIDI Interface.
Definition: MIDI_Interface.hpp:32
Parsing_MIDI_Interface::callbacks
MIDI_Callbacks * callbacks
Definition: MIDI_Interface.hpp:240
Parsing_MIDI_Interface::getParser
MIDI_Parser & getParser()
Definition: MIDI_Interface.hpp:201
MIDICNChannel
A class for saving a MIDI channel and cable number.
Definition: MIDICNChannelAddress.hpp:19
MIDI_Interface::sendNoteOff
void sendNoteOff(MIDICNChannelAddress address, uint8_t velocity)
Send a MIDI Note Off event.
Definition: MIDI_Interface.cpp:64
MIDI_Callbacks::~MIDI_Callbacks
virtual ~MIDI_Callbacks()=default
MIDI_Interface::sendOnCable
void sendOnCable(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2, uint8_t cn)
Send a 3-byte MIDI packet with cable number.
Definition: MIDI_Interface.cpp:30
Parsing_MIDI_Interface::onSysExMessage
void onSysExMessage()
Definition: MIDI_Interface.cpp:164
MIDI_Interface::setAsDefault
void setAsDefault()
Set this MIDI interface as the default interface.
Definition: MIDI_Interface.cpp:16
ChannelMessage
Definition: MIDI_Parser.hpp:47
MIDI_Callbacks
A class for callbacks from MIDI input.
Definition: MIDI_Interface.hpp:247
MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:16
Parsing_MIDI_Interface::setCallbacks
void setCallbacks(MIDI_Callbacks *cb) override
Set the callbacks that will be called when a MIDI message is received.
Definition: MIDI_Interface.hpp:220
MIDI_Interface::sendCC
void sendCC(MIDICNChannelAddress address, uint8_t value)
Send a MIDI Control Change event.
Definition: MIDI_Interface.cpp:75
MIDI_Interface::~MIDI_Interface
virtual ~MIDI_Interface()
Destructor.
Definition: MIDI_Interface.cpp:9
MIDI_Interface::sendPC
void sendPC(MIDICNChannelAddress address)
Send a MIDI Program Change event.
Definition: MIDI_Interface.cpp:85
Parsing_MIDI_Interface::onRealtimeMessage
void onRealtimeMessage(uint8_t message)
Definition: MIDI_Interface.cpp:154
MIDI_Interface::send
void send(const uint8_t(&sysexdata)[N], uint8_t cn=0)
Send a MIDI System Exclusive message.
Definition: MIDI_Interface.hpp:120
MIDI_Parser.hpp