Control Surface  1.1.0
MIDI Control Surface library for Arduino
DebugMIDI_Interface.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include <ctype.h>
5 
7 
8 /**
9  * @brief A class for MIDI interfaces sending and receiving
10  * human-readable MIDI messages over a Stream.
11  *
12  * @ingroup MIDIInterfaces
13  */
15  public:
16  /**
17  * @brief Construct a debug MIDI interface on the given Stream.
18  *
19  * @param stream
20  * The Stream interface.
21  */
23 
24  MIDI_read_t read() override;
25 
26  protected:
27  void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2,
28  uint8_t cn) override;
29 
30  void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t cn) override;
31 
32  void sendImpl(const uint8_t *data, size_t length, uint8_t cn) override;
33 
34  void sendImpl(uint8_t rt, uint8_t cn) override;
35 
36  private:
37  char firstChar = '\0';
38  char secondChar = '\0';
39 
40  /**
41  * @brief Convert a hexadecimal character to a 4-bit nibble.
42  */
43  static uint8_t hexCharToNibble(char hex) {
44  return hex < 'a' ? hex - '0' : hex - 'a' + 10;
45  }
46 };
47 
48 /**
49  * @brief A wrapper class for debug MIDI interfaces sending and receiving
50  * human-readable MIDI messages over a Serial port of class T.
51  *
52  * @note This is a template class because the class of the Serial object
53  * is completely different on different architectures, and they
54  * do not share a common super-class that has a `begin` method.
55  *
56  * @ingroup MIDIInterfaces
57  */
58 template <typename T>
60  public:
61  /**
62  * @brief Construct a new Debug MIDI Interface on the given Serial
63  * interface with the given baud rate.
64  *
65  * @param serial
66  * The Serial interface.
67  * @param baud
68  * The baud rate for the Serial interface.
69  */
71  unsigned long baud = AH::defaultBaudRate)
73  /**
74  * @brief Start the Serial interface at the predefined baud rate.
75  */
76  void begin() override { serial.begin(baud); }
77 
78  private:
79  T &serial;
80  const unsigned long baud;
81 };
82 
83 /**
84  * @brief A class for debug MIDI interfaces sending and receiving
85  * human-readable MIDI messages over a HardwareSerial port.
86  *
87  * @ingroup MIDIInterfaces
88  */
90  : public SerialDebugMIDI_Interface<HardwareSerial> {
91  public:
92  /**
93  * @brief Construct a new Debug MIDI Interface on the given HardwareSerial
94  * interface with the given baud rate.
95  *
96  * @param serial
97  * The HardwareSerial interface.
98  * @param baud
99  * The baud rate for the serial interface.
100  */
102  unsigned long baud = AH::defaultBaudRate)
104 };
105 
106 /**
107  * @brief A class for debug MIDI interfaces sending and receiving
108  * human-readable MIDI messages over the USB CDC connection.
109  *
110  * Boards without a native USB connection (UNO, MEGA, Nano ...)
111  * use HardwareSerial0 for USB communication.
112  *
113  * @ingroup MIDIInterfaces
114  */
116  : public SerialDebugMIDI_Interface<decltype(Serial)> {
117  public:
118  /**
119  * @brief Construct a USBDebugMIDI_Interface with the given baud rate.
120  *
121  * @param baud
122  * The baud rate to start the USB Serial connection with.
123  */
125  : SerialDebugMIDI_Interface(Serial, baud) {}
126 };
127 
128 // TODO: Teensy 4.0 SoftwareSerial bug
129 #if defined(__AVR__) || (defined(TEENSYDUINO) && TEENSYDUINO != 147) || \
130  (defined(TEENSYDUINO) && !defined(__IMXRT1052__) && \
131  !defined(__IMXRT1062__))
132 #include <SoftwareSerial.h>
133 /**
134  * @brief A class for debug MIDI interfaces sending and receiving
135  * human-readable MIDI messages over a SoftwareSerial interface.
136  *
137  * @ingroup MIDIInterfaces
138  */
140  : public SerialDebugMIDI_Interface<SoftwareSerial> {
141  public:
142  /**
143  * @brief Construct a SoftwareSerialDebugMIDI_Interface on the given
144  * SoftwareSerial interface with the given baud rate.
145  *
146  * @param serial
147  * The SoftwareSerial interface.
148  * @param baud
149  * The baud rate for the serial interface.
150  */
152  unsigned long baud)
154 };
155 #endif
156 
StreamDebugMIDI_Interface::hexCharToNibble
static uint8_t hexCharToNibble(char hex)
Convert a hexadecimal character to a 4-bit nibble.
Definition: DebugMIDI_Interface.hpp:43
SerialDebugMIDI_Interface
A wrapper class for debug MIDI interfaces sending and receiving human-readable MIDI messages over a S...
Definition: DebugMIDI_Interface.hpp:59
HardwareSerialDebugMIDI_Interface
A class for debug MIDI interfaces sending and receiving human-readable MIDI messages over a HardwareS...
Definition: DebugMIDI_Interface.hpp:89
SerialMIDI_Interface.hpp
MIDI_read_t
MIDI_read_t
Definition: MIDI_Parser.hpp:29
USBDebugMIDI_Interface::USBDebugMIDI_Interface
USBDebugMIDI_Interface(unsigned long baud=AH::defaultBaudRate)
Construct a USBDebugMIDI_Interface with the given baud rate.
Definition: DebugMIDI_Interface.hpp:124
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
StreamDebugMIDI_Interface::StreamDebugMIDI_Interface
StreamDebugMIDI_Interface(Stream &stream)
Construct a debug MIDI interface on the given Stream.
Definition: DebugMIDI_Interface.hpp:22
StreamDebugMIDI_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: DebugMIDI_Interface.cpp:77
SerialDebugMIDI_Interface::serial
T & serial
Definition: DebugMIDI_Interface.hpp:79
StreamDebugMIDI_Interface::read
MIDI_read_t read() override
Definition: DebugMIDI_Interface.cpp:40
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
SerialDebugMIDI_Interface::begin
void begin() override
Start the Serial interface at the predefined baud rate.
Definition: DebugMIDI_Interface.hpp:76
StreamDebugMIDI_Interface::secondChar
char secondChar
Definition: DebugMIDI_Interface.hpp:38
StreamDebugMIDI_Interface
A class for MIDI interfaces sending and receiving human-readable MIDI messages over a Stream.
Definition: DebugMIDI_Interface.hpp:14
hex
Print & hex(Print &printer)
Definition: PrintStream.cpp:62
SoftwareSerialDebugMIDI_Interface
A class for debug MIDI interfaces sending and receiving human-readable MIDI messages over a SoftwareS...
Definition: DebugMIDI_Interface.hpp:139
SoftwareSerialDebugMIDI_Interface::SoftwareSerialDebugMIDI_Interface
SoftwareSerialDebugMIDI_Interface(SoftwareSerial &serial, unsigned long baud)
Construct a SoftwareSerialDebugMIDI_Interface on the given SoftwareSerial interface with the given ba...
Definition: DebugMIDI_Interface.hpp:151
StreamDebugMIDI_Interface::firstChar
char firstChar
Definition: DebugMIDI_Interface.hpp:37
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
USBDebugMIDI_Interface
A class for debug MIDI interfaces sending and receiving human-readable MIDI messages over the USB CDC...
Definition: DebugMIDI_Interface.hpp:115
HardwareSerialDebugMIDI_Interface::HardwareSerialDebugMIDI_Interface
HardwareSerialDebugMIDI_Interface(HardwareSerial &serial, unsigned long baud=AH::defaultBaudRate)
Construct a new Debug MIDI Interface on the given HardwareSerial interface with the given baud rate.
Definition: DebugMIDI_Interface.hpp:101
SerialDebugMIDI_Interface::baud
const unsigned long baud
Definition: DebugMIDI_Interface.hpp:80
AH::defaultBaudRate
constexpr unsigned long defaultBaudRate
The default baud rate for debug output.
Definition: AH/Settings/Settings.hpp:36
SerialDebugMIDI_Interface::SerialDebugMIDI_Interface
SerialDebugMIDI_Interface(T &serial, unsigned long baud=AH::defaultBaudRate)
Construct a new Debug MIDI Interface on the given Serial interface with the given baud rate.
Definition: DebugMIDI_Interface.hpp:70