This is an old version of the documentation. View the latest version here.
Control Surface  1.0.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 <Arduino.h> // Stream
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:
95  SerialMIDI_Interface(T &serial, unsigned long baud)
100  void begin() override { serial.begin(baud); }
101 
102  private:
103  T &serial;
104  const unsigned long baud;
105 };
106 
114  : public SerialMIDI_Interface<HardwareSerial> {
115  public:
125  HardwareSerialMIDI_Interface(HardwareSerial &serial, unsigned long baud)
127 };
128 
135 class USBSerialMIDI_Interface : public SerialMIDI_Interface<decltype(Serial)> {
136  public:
144  : SerialMIDI_Interface(Serial, baud) {}
145 };
146 
147 #if !defined(TEENSYDUINO) || \
148  (defined(TEENSYDUINO) && defined(TEENSY_SERIALUSB_ENABLED))
149 
158  public:
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 
187  : public SerialMIDI_Interface<SoftwareSerial> {
188  public:
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
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
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
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
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
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
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
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
HAIRLESS_BAUD
constexpr unsigned long HAIRLESS_BAUD
The baud rate to use for Hairless MIDI.
Definition: Settings.hpp:108
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
SettingsWrapper.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