Control Surface  1.1.0
MIDI Control Surface library for Arduino
SerialMIDI_Interface.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "MIDI_Interface.hpp"
5 #include <Arduino.h> // Stream
7 #include <Settings/SettingsWrapper.hpp>
8 
10 
11 /**
12  * @brief A class for MIDI interfaces sending and receiving MIDI messages
13  * over a Stream.
14  *
15  * @ingroup MIDIInterfaces
16  */
18  public:
19  /**
20  * @brief Construct a StreamMIDI_Interface on the given Stream.
21  *
22  * @param stream
23  * The Stream interface.
24  */
27 
28  MIDI_read_t read() override {
29  while (stream.available() > 0) {
30  uint8_t midiByte = stream.read();
31  MIDI_read_t parseResult = parser.parse(midiByte);
32  if (parseResult != NO_MESSAGE)
33  return parseResult;
34  }
35  return NO_MESSAGE;
36  }
37 
38  protected:
40 
41  void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2,
42  uint8_t cn) override {
43  (void)cn;
44  stream.write(m | c); // Send the MIDI message over the stream
45  stream.write(d1);
46  stream.write(d2);
47  // stream.flush(); // TODO
48  }
49 
50  void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t cn) override {
51  (void)cn;
52  stream.write(m | c); // Send the MIDI message over the stream
53  stream.write(d1);
54  // stream.flush(); // TODO
55  }
56 
57  void sendImpl(const uint8_t *data, size_t length, uint8_t cn) override {
58  (void)cn;
59  stream.write(data, length);
60  // stream.flush(); // TODO
61  }
62 
63  void sendImpl(uint8_t rt, uint8_t cn) override {
64  (void)cn;
65  stream.write(rt); // Send the MIDI message over the stream
66  // stream.flush(); // TODO
67  }
68 
69  protected:
70  Stream &stream;
71 };
72 
73 /**
74  * @brief A wrapper class for MIDI interfaces sending and receiving
75  * MIDI messages over a Serial port of class T.
76  *
77  * @note This is a template class because the class of the Serial object
78  * is completely different on different architectures, and they
79  * do not share a common super-class that has a `begin` method.
80  *
81  * @ingroup MIDIInterfaces
82  */
83 template <class T>
85  public:
86  /**
87  * @brief Create a new MIDI Interface on the given Serial interface
88  * with the given baud rate.
89  *
90  * @param serial
91  * The Serial interface.
92  * @param baud
93  * The baud rate for the Serial interface.
94  */
95  SerialMIDI_Interface(T &serial, unsigned long baud)
97  /**
98  * @brief Start the Serial interface at the predefined baud rate.
99  */
100  void begin() override { serial.begin(baud); }
101 
102  private:
103  T &serial;
104  const unsigned long baud;
105 };
106 
107 /**
108  * @brief A class for MIDI interfaces sending and receiving
109  * MIDI messages over a Serial port of class T.
110  *
111  * @ingroup MIDIInterfaces
112  */
114  : public SerialMIDI_Interface<HardwareSerial> {
115  public:
116  /**
117  * @brief Construct a new MIDI Interface on the given HardwareSerial
118  * interface with the given baud rate.
119  *
120  * @param serial
121  * The HardwareSerial interface.
122  * @param baud
123  * The baud rate for the serial interface.
124  */
125  HardwareSerialMIDI_Interface(HardwareSerial &serial, unsigned long baud)
127 };
128 
129 /**
130  * @brief A class for MIDI interfaces sending and receiving
131  * MIDI messages over the Serial port of the USB connection.
132  *
133  * @ingroup MIDIInterfaces
134  */
135 class USBSerialMIDI_Interface : public SerialMIDI_Interface<decltype(Serial)> {
136  public:
137  /**
138  * @brief Construct a USBSerialMIDI_Interface with the given baud rate.
139  *
140  * @param baud
141  * The baud rate to start the USB Serial connection with.
142  */
144  : SerialMIDI_Interface(Serial, baud) {}
145 };
146 
147 #if !defined(TEENSYDUINO) || \
148  (defined(TEENSYDUINO) && defined(TEENSY_SERIALUSB_ENABLED))
149 /**
150  * @brief A class for MIDI Interfaces sending and receiving
151  * data over the USB Serial CDC connection for the use
152  * with the [Hairless MIDI<->Serial Bridge]
153  * (http://projectgus.github.io/hairless-midiserial/).
154  *
155  * @ingroup MIDIInterfaces
156  */
158  public:
159  /**
160  * @brief Construct a HairlessMIDI_Interface.
161  *
162  * The default Hairless baud rate of 115200 baud is used.
163  * This can be changed in the Settings.hpp file.
164  */
166 };
167 #endif
168 
170 
171 // TODO: Teensy 4.0 SoftwareSerial bug
172 #if defined(__AVR__) || (defined(TEENSYDUINO) && TEENSYDUINO != 147) || \
173  (defined(TEENSYDUINO) && !defined(__IMXRT1052__) && \
174  !defined(__IMXRT1062__))
175 
176 #include <SoftwareSerial.h>
177 
179 
180 /**
181  * @brief A class for MIDI interfaces sending and receiving
182  * MIDI messages over a SoftwareSerial interface.
183  *
184  * @ingroup MIDIInterfaces
185  */
187  : public SerialMIDI_Interface<SoftwareSerial> {
188  public:
189  /**
190  * @brief Create a SoftwareSerialMIDI_Interface on the given
191  * SoftwareSerial interface with the given baud rate.
192  *
193  * @param serial
194  * The SoftwareSerial interface.
195  * @param baud
196  * The baud rate for the serial interface.
197  */
198  SoftwareSerialMIDI_Interface(SoftwareSerial &serial, unsigned long baud)
200 };
201 
203 
204 #endif
SoftwareSerialMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over a SoftwareSerial interface.
Definition: SerialMIDI_Interface.hpp:186
SerialMIDI_Interface
A wrapper class for MIDI interfaces sending and receiving MIDI messages over a Serial port of class T...
Definition: SerialMIDI_Interface.hpp:84
HAIRLESS_BAUD
constexpr unsigned long HAIRLESS_BAUD
The baud rate to use for Hairless MIDI.
Definition: Settings/Settings.hpp:56
SoftwareSerialMIDI_Interface::SoftwareSerialMIDI_Interface
SoftwareSerialMIDI_Interface(SoftwareSerial &serial, unsigned long baud)
Create a SoftwareSerialMIDI_Interface on the given SoftwareSerial interface with the given baud rate.
Definition: SerialMIDI_Interface.hpp:198
Parsing_MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:188
SerialMIDI_Parser::parse
MIDI_read_t parse(uint8_t midibyte)
Definition: SerialMIDI_Parser.cpp:5
MIDI_read_t
MIDI_read_t
Definition: MIDI_Parser.hpp:29
USBSerialMIDI_Interface::USBSerialMIDI_Interface
USBSerialMIDI_Interface(unsigned long baud)
Construct a USBSerialMIDI_Interface with the given baud rate.
Definition: SerialMIDI_Interface.hpp:143
StreamMIDI_Interface::sendImpl
void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t cn) override
Low-level function for sending a 2-byte MIDI message.
Definition: SerialMIDI_Interface.hpp:50
StreamMIDI_Interface::sendImpl
void sendImpl(const uint8_t *data, size_t length, uint8_t cn) override
Low-level function for sending a system exclusive MIDI message.
Definition: SerialMIDI_Interface.hpp:57
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
USBSerialMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over the Serial port of the USB conne...
Definition: SerialMIDI_Interface.hpp:135
StreamMIDI_Interface::StreamMIDI_Interface
StreamMIDI_Interface(Stream &stream)
Construct a StreamMIDI_Interface on the given Stream.
Definition: SerialMIDI_Interface.hpp:25
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
SerialMIDI_Interface::SerialMIDI_Interface
SerialMIDI_Interface(T &serial, unsigned long baud)
Create a new MIDI Interface on the given Serial interface with the given baud rate.
Definition: SerialMIDI_Interface.hpp:95
StreamMIDI_Interface::read
MIDI_read_t read() override
Definition: SerialMIDI_Interface.hpp:28
HairlessMIDI_Interface
A class for MIDI Interfaces sending and receiving data over the USB Serial CDC connection for the use...
Definition: SerialMIDI_Interface.hpp:157
HairlessMIDI_Interface::HairlessMIDI_Interface
HairlessMIDI_Interface()
Construct a HairlessMIDI_Interface.
Definition: SerialMIDI_Interface.hpp:165
HardwareSerialMIDI_Interface::HardwareSerialMIDI_Interface
HardwareSerialMIDI_Interface(HardwareSerial &serial, unsigned long baud)
Construct a new MIDI Interface on the given HardwareSerial interface with the given baud rate.
Definition: SerialMIDI_Interface.hpp:125
HardwareSerialMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over a Serial port of class T.
Definition: SerialMIDI_Interface.hpp:113
TeensyUSBTypes.hpp
SerialMIDI_Interface::baud
const unsigned long baud
Definition: SerialMIDI_Interface.hpp:104
NO_MESSAGE
Definition: MIDI_Parser.hpp:30
SerialMIDI_Parser
Definition: SerialMIDI_Parser.hpp:8
StreamMIDI_Interface::sendImpl
void sendImpl(uint8_t rt, uint8_t cn) override
Low-level function for sending a single-byte MIDI message.
Definition: SerialMIDI_Interface.hpp:63
StreamMIDI_Interface::parser
SerialMIDI_Parser parser
Definition: SerialMIDI_Interface.hpp:39
SerialMIDI_Interface::serial
T & serial
Definition: SerialMIDI_Interface.hpp:103
SerialMIDI_Interface::begin
void begin() override
Start the Serial interface at the predefined baud rate.
Definition: SerialMIDI_Interface.hpp:100
StreamMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over a Stream.
Definition: SerialMIDI_Interface.hpp:17
StreamMIDI_Interface::stream
Stream & stream
Definition: SerialMIDI_Interface.hpp:70
MIDI_Interface.hpp
SerialMIDI_Parser.hpp
StreamMIDI_Interface::sendImpl
void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2, uint8_t cn) override
Low-level function for sending a 3-byte MIDI message.
Definition: SerialMIDI_Interface.hpp:41