Control Surface  1.2.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"
4 #include <AH/Arduino-Wrapper.h> // Stream
5 #include <AH/STL/utility>
8 #include <Settings/SettingsWrapper.hpp>
9 
10 #if defined(ESP32) || !defined(ARDUINO)
11 #include <mutex>
12 #endif
13 
15 
23  public:
32 
34  : Parsing_MIDI_Interface(std::move(other)), stream(other.stream) {}
35  // TODO: should I move the mutex too?
36 
37  MIDIReadEvent read() override {
38  while (stream.available() > 0) {
39  uint8_t midiByte = stream.read();
40  MIDIReadEvent parseResult = parser.parse(midiByte);
41  if (parseResult != MIDIReadEvent::NO_MESSAGE)
42  return parseResult;
43  }
44  return MIDIReadEvent::NO_MESSAGE;
45  }
46 
47  protected:
49 
50  void sendImpl(uint8_t header, uint8_t d1, uint8_t d2,
51  uint8_t cn) override {
52 #if defined(ESP32) || !defined(ARDUINO)
53  std::lock_guard<std::mutex> lock(mutex);
54 #endif
55  (void)cn;
56  stream.write(header); // Send the MIDI message over the stream
57  stream.write(d1);
58  stream.write(d2);
59  // stream.flush(); // TODO
60  }
61 
62  void sendImpl(uint8_t header, uint8_t d1, uint8_t cn) override {
63 #if defined(ESP32) || !defined(ARDUINO)
64  std::lock_guard<std::mutex> lock(mutex);
65 #endif
66  (void)cn;
67  stream.write(header); // Send the MIDI message over the stream
68  stream.write(d1);
69  // stream.flush(); // TODO
70  }
71 
72  void sendImpl(const uint8_t *data, size_t length, uint8_t cn) override {
73 #if defined(ESP32) || !defined(ARDUINO)
74  std::lock_guard<std::mutex> lock(mutex);
75 #endif
76  (void)cn;
77  stream.write(data, length);
78  // stream.flush(); // TODO
79  }
80 
81  void sendImpl(uint8_t rt, uint8_t cn) override {
82 #if defined(ESP32) || !defined(ARDUINO)
83  std::lock_guard<std::mutex> lock(mutex);
84 #endif
85  (void)cn;
86  stream.write(rt); // Send the MIDI message over the stream
87  // stream.flush(); // TODO
88  }
89 
90  protected:
91  Stream &stream;
92 #if defined(ESP32) || !defined(ARDUINO)
93  std::mutex mutex;
94 #endif
95 };
96 
107 template <class T>
109  public:
119  SerialMIDI_Interface(T &serial, unsigned long baud = MIDI_BAUD)
120  : StreamMIDI_Interface(serial), baud(baud) {}
121 
125  void begin() override { static_cast<T &>(stream).begin(baud); }
126 
127  private:
128  const unsigned long baud;
129 };
130 
138  : public SerialMIDI_Interface<HardwareSerial> {
139  public:
149  HardwareSerialMIDI_Interface(HardwareSerial &serial,
150  unsigned long baud = MIDI_BAUD)
151  : SerialMIDI_Interface(serial, baud) {}
152 };
153 
160 class USBSerialMIDI_Interface : public SerialMIDI_Interface<decltype(Serial)> {
161  public:
169  : SerialMIDI_Interface(Serial, baud) {}
170 };
171 
172 #if !defined(TEENSYDUINO) || \
173  (defined(TEENSYDUINO) && defined(TEENSY_SERIALUSB_ENABLED))
174 
183  public:
191 };
192 #endif
193 
195 
196 // TODO: Teensy 4.0 SoftwareSerial bug
197 #if defined(__AVR__) || (defined(TEENSYDUINO) && TEENSYDUINO != 147) || \
198  (defined(TEENSYDUINO) && !defined(__IMXRT1052__) && \
199  !defined(__IMXRT1062__))
200 
201 #include <SoftwareSerial.h>
202 
204 
212  : public SerialMIDI_Interface<SoftwareSerial> {
213  public:
223  SoftwareSerialMIDI_Interface(SoftwareSerial &serial, unsigned long baud)
224  : SerialMIDI_Interface(serial, baud) {}
225 };
226 
228 
229 #endif
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:81
StreamMIDI_Interface::stream
Stream & stream
Definition: SerialMIDI_Interface.hpp:91
MIDI_BAUD
constexpr auto MIDI_BAUD
Definition: MIDI_Interface.hpp:11
SoftwareSerialMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over a SoftwareSerial interface.
Definition: SerialMIDI_Interface.hpp:212
SerialMIDI_Interface
A wrapper class for MIDI interfaces sending and receiving MIDI messages over a Serial port of generic...
Definition: SerialMIDI_Interface.hpp:108
HAIRLESS_BAUD
constexpr unsigned long HAIRLESS_BAUD
The baud rate to use for Hairless MIDI.
Definition: Settings/Settings.hpp:57
StreamMIDI_Interface::sendImpl
void sendImpl(uint8_t header, uint8_t d1, uint8_t d2, uint8_t cn) override
Low-level function for sending a 3-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:72
Parsing_MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:232
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:160
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
StreamMIDI_Interface::read
MIDIReadEvent read() override
Try reading and parsing a single incoming MIDI message.
Definition: SerialMIDI_Interface.hpp:37
SerialMIDI_Interface::SerialMIDI_Interface
SerialMIDI_Interface(T &serial, unsigned long baud=MIDI_BAUD)
Create a new MIDI Interface on the given Serial interface with the given baud rate.
Definition: SerialMIDI_Interface.hpp:119
AH::UpdatableCRTP< Updatable< MIDI_Interface, false >, false >::mutex
static Mutex mutex
Definition: Updatable.hpp:174
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:223
HairlessMIDI_Interface
A class for MIDI Interfaces sending and receiving data over the USB Serial CDC connection for the use...
Definition: SerialMIDI_Interface.hpp:182
StreamMIDI_Interface::StreamMIDI_Interface
StreamMIDI_Interface(StreamMIDI_Interface &&other)
Definition: SerialMIDI_Interface.hpp:33
Arduino-Wrapper.h
HardwareSerialMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over a Hardware Serial port.
Definition: SerialMIDI_Interface.hpp:138
TeensyUSBTypes.hpp
StreamMIDI_Interface::sendImpl
void sendImpl(uint8_t header, uint8_t d1, uint8_t cn) override
Low-level function for sending a 2-byte MIDI message.
Definition: SerialMIDI_Interface.hpp:62
SerialMIDI_Parser
Definition: SerialMIDI_Parser.hpp:8
StreamMIDI_Interface::parser
SerialMIDI_Parser parser
Definition: SerialMIDI_Interface.hpp:48
StreamMIDI_Interface::StreamMIDI_Interface
StreamMIDI_Interface(Stream &stream)
Construct a StreamMIDI_Interface on the given Stream.
Definition: SerialMIDI_Interface.hpp:30
SerialMIDI_Interface::baud
const unsigned long baud
Definition: SerialMIDI_Interface.hpp:128
USBSerialMIDI_Interface::USBSerialMIDI_Interface
USBSerialMIDI_Interface(unsigned long baud)
Construct a USBSerialMIDI_Interface with the given baud rate.
Definition: SerialMIDI_Interface.hpp:168
HairlessMIDI_Interface::HairlessMIDI_Interface
HairlessMIDI_Interface()
Construct a HairlessMIDI_Interface.
Definition: SerialMIDI_Interface.hpp:190
SerialMIDI_Interface::begin
void begin() override
Start the Serial interface at the predefined baud rate.
Definition: SerialMIDI_Interface.hpp:125
std
Definition: vector.cpp:5
SerialMIDI_Parser::parse
MIDIReadEvent parse(uint8_t midibyte)
Definition: SerialMIDI_Parser.cpp:5
MIDIReadEvent
MIDIReadEvent
Result of the MIDI interface read methods.
Definition: MIDI_Parser.hpp:15
StreamMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over a Stream.
Definition: SerialMIDI_Interface.hpp:22
MIDI_Interface.hpp
HardwareSerialMIDI_Interface::HardwareSerialMIDI_Interface
HardwareSerialMIDI_Interface(HardwareSerial &serial, unsigned long baud=MIDI_BAUD)
Construct a new MIDI Interface on the given HardwareSerial interface with the given baud rate.
Definition: SerialMIDI_Interface.hpp:149
SerialMIDI_Parser.hpp