Control Surface  1.1.1
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 
18  public:
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 
83 template <class T>
85  public:
97 
101  void begin() override { serial.begin(baud); }
102 
103  private:
104  T &serial;
105  const unsigned long baud;
106 };
107 
115  : public SerialMIDI_Interface<HardwareSerial> {
116  public:
127  unsigned long baud = MIDI_BAUD)
129 };
130 
137 class USBSerialMIDI_Interface : public SerialMIDI_Interface<decltype(Serial)> {
138  public:
146  : SerialMIDI_Interface(Serial, baud) {}
147 };
148 
149 #if !defined(TEENSYDUINO) || \
150  (defined(TEENSYDUINO) && defined(TEENSY_SERIALUSB_ENABLED))
151 
160  public:
168 };
169 #endif
170 
172 
173 // TODO: Teensy 4.0 SoftwareSerial bug
174 #if defined(__AVR__) || (defined(TEENSYDUINO) && TEENSYDUINO != 147) || \
175  (defined(TEENSYDUINO) && !defined(__IMXRT1052__) && \
176  !defined(__IMXRT1062__))
177 
178 #include <SoftwareSerial.h>
179 
181 
189  : public SerialMIDI_Interface<SoftwareSerial> {
190  public:
200  SoftwareSerialMIDI_Interface(SoftwareSerial &serial, unsigned long baud)
202 };
203 
205 
206 #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:63
StreamMIDI_Interface::stream
Stream & stream
Definition: SerialMIDI_Interface.hpp:70
MIDI_BAUD
constexpr auto MIDI_BAUD
Definition: MIDI_Interface.hpp:9
SoftwareSerialMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over a SoftwareSerial interface.
Definition: SerialMIDI_Interface.hpp:188
SerialMIDI_Interface
A wrapper class for MIDI interfaces sending and receiving MIDI messages over a Serial port of generic...
Definition: SerialMIDI_Interface.hpp:84
SerialMIDI_Parser::parse
MIDI_read_t parse(uint8_t midibyte)
Definition: SerialMIDI_Parser.cpp:5
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(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
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
SerialMIDI_Interface::serial
T & serial
Definition: SerialMIDI_Interface.hpp:104
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:137
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
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:95
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:200
HairlessMIDI_Interface
A class for MIDI Interfaces sending and receiving data over the USB Serial CDC connection for the use...
Definition: SerialMIDI_Interface.hpp:159
HardwareSerialMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over a Hardware Serial port.
Definition: SerialMIDI_Interface.hpp:114
TeensyUSBTypes.hpp
NO_MESSAGE
Definition: MIDI_Parser.hpp:30
SerialMIDI_Parser
Definition: SerialMIDI_Parser.hpp:8
StreamMIDI_Interface::parser
SerialMIDI_Parser parser
Definition: SerialMIDI_Interface.hpp:39
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
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::StreamMIDI_Interface
StreamMIDI_Interface(Stream &stream)
Construct a StreamMIDI_Interface on the given Stream.
Definition: SerialMIDI_Interface.hpp:25
SerialMIDI_Interface::baud
const unsigned long baud
Definition: SerialMIDI_Interface.hpp:105
USBSerialMIDI_Interface::USBSerialMIDI_Interface
USBSerialMIDI_Interface(unsigned long baud)
Construct a USBSerialMIDI_Interface with the given baud rate.
Definition: SerialMIDI_Interface.hpp:145
HairlessMIDI_Interface::HairlessMIDI_Interface
HairlessMIDI_Interface()
Construct a HairlessMIDI_Interface.
Definition: SerialMIDI_Interface.hpp:167
SerialMIDI_Interface::begin
void begin() override
Start the Serial interface at the predefined baud rate.
Definition: SerialMIDI_Interface.hpp:101
StreamMIDI_Interface
A class for MIDI interfaces sending and receiving MIDI messages over a Stream.
Definition: SerialMIDI_Interface.hpp:17
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:126
SerialMIDI_Parser.hpp
StreamMIDI_Interface::read
MIDI_read_t read() override
Definition: SerialMIDI_Interface.hpp:28