Control Surface  1.1.0
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 
13 /**
14  * @brief An abstract class for MIDI interfaces.
15  */
17  protected:
18  /**
19  * @brief Constructor.
20  */
22 
23  public:
24  /**
25  * @brief Destructor.
26  */
27  virtual ~MIDI_Interface();
28 
29  /**
30  * @brief Initialize the MIDI Interface.
31  */
32  virtual void begin() {}
33 
34  /**
35  * @brief Send a 3-byte MIDI packet.
36  *
37  * @param m
38  * MIDI message type. [0x80, 0xE0]
39  * @param c
40  * The MIDI channel. [1, 16]
41  * @param d1
42  * The first data byte. [0, 127]
43  * @param d2
44  * The second data byte. [0, 127]
45  */
46  void send(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2);
47 
48  /**
49  * @brief Send a 2-byte MIDI packet.
50  *
51  * @param m
52  * MIDI message type. [0x80, 0xE0]
53  * @param c
54  * The MIDI channel. [1, 16]
55  * @param d1
56  * The first data byte. [0, 127]
57  */
58  void send(uint8_t m, uint8_t c, uint8_t d1);
59 
60  /**
61  * @brief Send a 3-byte MIDI packet with cable number.
62  *
63  * @param m
64  * MIDI message type. [0x80, 0xE0]
65  * @param c
66  * The MIDI channel. [1, 16]
67  * @param d1
68  * The first data byte. [0, 127]
69  * @param d2
70  * The second data byte. [0, 127]
71  * @param cn
72  * The MIDI Cable Number. [0, 15]
73  */
74  void sendOnCable(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2, uint8_t cn);
75 
76  /**
77  * @brief Send a 2-byte MIDI packet with cable number.
78  *
79  * @param m
80  * MIDI message type. [0x80, 0xE0]
81  * @param c
82  * The MIDI channel. [1, 16]
83  * @param d1
84  * The first data byte. [0, 127]
85  * @param cn
86  * The MIDI Cable Number. [0, 15]
87  */
88  void sendOnCable(uint8_t m, uint8_t c, uint8_t d1, uint8_t cn);
89 
90  /**
91  * @brief Send a single-byte MIDI packet with cable number.
92  *
93  * @param r
94  * The MIDI byte to send.
95  * @param cn
96  * The MIDI Cable Number. [0, 15]
97  */
98  void sendOnCable(uint8_t r, uint8_t cn);
99 
100  /// Send a MIDI Note On event.
101  void sendNoteOn(MIDICNChannelAddress address, uint8_t velocity);
102  /// Send a MIDI Note Off event.
103  void sendNoteOff(MIDICNChannelAddress address, uint8_t velocity);
104  /// Send a MIDI Key Pressure event.
105  void sendKP(MIDICNChannelAddress address, uint8_t pressure);
106  /// Send a MIDI Control Change event.
107  void sendCC(MIDICNChannelAddress address, uint8_t value);
108  /// Send a MIDI Program Change event.
109  void sendPC(MIDICNChannelAddress address);
110  /// Send a MIDI Program Change event.
111  void sendPC(MIDICNChannel address, uint8_t value);
112  /// Send a MIDI Channel Pressure event.
113  void sendCP(MIDICNChannel address, uint8_t pressure);
114  /// Send a MIDI Pitch Bend event.
115  void sendPB(MIDICNChannel address, uint16_t value);
116  /// Send a MIDI System Exclusive message.
117  void send(SysExMessage message);
118  /// Send a MIDI System Exclusive 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  }
123  /// Send a single-byte MIDI message.
124  void send(uint8_t rt, uint8_t cn = 0);
125 
126  /**
127  * @brief Read the MIDI interface and call the callback if a message is
128  * received.
129  */
130  virtual void update() = 0;
131 
132  /**
133  * @brief Return the default MIDI interface.
134  */
135  static MIDI_Interface *getDefault();
136 
137  /**
138  * @brief Set this MIDI interface as the default interface.
139  */
140  void setAsDefault();
141 
142  /**
143  * @brief Set the callbacks that will be called when a MIDI message is
144  * received.
145  *
146  * @param cb
147  * A pointer to an object that implements the MIDI_Callbacks class.
148  */
149  virtual void setCallbacks(MIDI_Callbacks *cb) = 0;
150 
151  /**
152  * @brief Set the callbacks that will be called when a MIDI message is
153  * received.
154  *
155  * @param cb
156  * A reference to an object that implements the MIDI_Callbacks
157  * class.
158  */
160 
161  /**
162  * @brief Low-level function for sending a 3-byte MIDI message.
163  */
164  virtual void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2,
165  uint8_t cn) = 0;
166  /**
167  * @brief Low-level function for sending a 2-byte MIDI message.
168  */
169  virtual void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t cn) = 0;
170 
171  /**
172  * @brief Low-level function for sending a system exclusive MIDI message.
173  */
174  virtual void sendImpl(const uint8_t *data, size_t length, uint8_t cn) = 0;
175 
176  /**
177  * @brief Low-level function for sending a single-byte MIDI message.
178  */
179  virtual void sendImpl(uint8_t rt, uint8_t cn) = 0;
180 
181  private:
183 };
184 
185 /**
186  * @brief An abstract class for MIDI interfaces.
187  */
189  protected:
190  /**
191  * @brief Construct a MIDI interface with the given parser.
192  *
193  * Also set this interface as the default MIDI interface.
194  *
195  * @param parser
196  * The MIDI parser to use for the interface.
197  */
199 
200  public:
201  MIDI_Parser &getParser() { return parser; }
202 
203  /**
204  * @brief Return the received channel message.
205  */
207 
208  /**
209  * @brief Return the received system exclusive message.
210  */
212 
213  /**
214  * @brief Return the cable number of the received message.
215  */
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:
227  /**
228  * @todo Documentation
229  */
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
244 /**
245  * @brief A class for callbacks from MIDI input.
246  */
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
Parsing_MIDI_Interface::getCN
uint8_t getCN() const
Return the cable number of the received message.
Definition: MIDI_Interface.cpp:128
MIDI_Interface::setAsDefault
void setAsDefault()
Set this MIDI interface as the default interface.
Definition: MIDI_Interface.cpp:16
MIDI_Interface::getDefault
static MIDI_Interface * getDefault()
Return the default MIDI interface.
Definition: MIDI_Interface.cpp:18
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
Parsing_MIDI_Interface::getChannelMessage
ChannelMessage getChannelMessage()
Return the received channel message.
Definition: MIDI_Interface.cpp:120
SysExMessage
Definition: MIDI_Parser.hpp:64
Parsing_MIDI_Interface::dispatchMIDIEvent
bool dispatchMIDIEvent(MIDI_read_t event)
Definition: MIDI_Interface.cpp:143
Parsing_MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:188
MIDI_read_t
MIDI_read_t
Definition: MIDI_Parser.hpp:29
Parsing_MIDI_Interface::read
virtual MIDI_read_t read()=0
Parsing_MIDI_Interface::onSysExMessage
void onSysExMessage()
Definition: MIDI_Interface.cpp:164
MIDI_Callbacks::~MIDI_Callbacks
virtual ~MIDI_Callbacks()=default
MIDI_Parser
Definition: MIDI_Parser.hpp:84
Def.hpp
MIDI_Interface::sendPB
void sendPB(MIDICNChannel address, uint16_t value)
Send a MIDI Pitch Bend event.
Definition: MIDI_Interface.cpp:95
MIDI_Callbacks::onRealtimeMessage
virtual void onRealtimeMessage(Parsing_MIDI_Interface &midi, uint8_t message)
Definition: MIDI_Interface.hpp:251
MIDICNChannelAddress.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
MIDI_Interface::~MIDI_Interface
virtual ~MIDI_Interface()
Destructor.
Definition: MIDI_Interface.cpp:9
MIDI_Interface::sendCC
void sendCC(MIDICNChannelAddress address, uint8_t value)
Send a MIDI Control Change event.
Definition: MIDI_Interface.cpp:75
Parsing_MIDI_Interface::onRealtimeMessage
void onRealtimeMessage(uint8_t message)
Definition: MIDI_Interface.cpp:154
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
MIDI_Interface::update
virtual void update()=0
Read the MIDI interface and call the callback if a message is received.
MIDICNChannelAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDICNChannelAddress.hpp:82
MIDI_Interface::DefaultMIDI_Interface
static MIDI_Interface * DefaultMIDI_Interface
Definition: MIDI_Interface.hpp:182
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
Parsing_MIDI_Interface::parser
MIDI_Parser & parser
Definition: MIDI_Interface.hpp:239
MIDI_Interface::sendNoteOff
void sendNoteOff(MIDICNChannelAddress address, uint8_t velocity)
Send a MIDI Note Off event.
Definition: MIDI_Interface.cpp:64
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
MIDI_Interface::sendKP
void sendKP(MIDICNChannelAddress address, uint8_t pressure)
Send a MIDI Key Pressure event.
Definition: MIDI_Interface.cpp:70
Parsing_MIDI_Interface::callbacks
MIDI_Callbacks * callbacks
Definition: MIDI_Interface.hpp:240
MIDICNChannel
A class for saving a MIDI channel and cable number.
Definition: MIDICNChannelAddress.hpp:19
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
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
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
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_Interface::setCallbacks
virtual void setCallbacks(MIDI_Callbacks *cb)=0
Set the callbacks that will be called when a MIDI message is received.
MIDI_Interface::sendNoteOn
void sendNoteOn(MIDICNChannelAddress address, uint8_t velocity)
Send a MIDI Note On event.
Definition: MIDI_Interface.cpp:58
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
ChannelMessage
Definition: MIDI_Parser.hpp:47
MIDI_Callbacks::onChannelMessage
virtual void onChannelMessage(Parsing_MIDI_Interface &midi)
Definition: MIDI_Interface.hpp:249
Parsing_MIDI_Interface::getParser
MIDI_Parser & getParser()
Definition: MIDI_Interface.hpp:201
MIDI_Callbacks
A class for callbacks from MIDI input.
Definition: MIDI_Interface.hpp:247
MIDI_Interface::sendPC
void sendPC(MIDICNChannelAddress address)
Send a MIDI Program Change event.
Definition: MIDI_Interface.cpp:85
MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:16
MIDI_Callbacks::onSysExMessage
virtual void onSysExMessage(Parsing_MIDI_Interface &midi)
Definition: MIDI_Interface.hpp:250
MIDI_Interface::MIDI_Interface
MIDI_Interface()
Constructor.
Definition: MIDI_Interface.cpp:5
Parsing_MIDI_Interface::getSysExMessage
SysExMessage getSysExMessage() const
Return the received system exclusive message.
Definition: MIDI_Interface.cpp:124
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::begin
virtual void begin()
Initialize the MIDI Interface.
Definition: MIDI_Interface.hpp:32
MIDI_Parser.hpp