Line data Source code
1 : #pragma once 2 : 3 : #include "SerialMIDI_Interface.hpp" 4 : #include <ctype.h> 5 : 6 : BEGIN_CS_NAMESPACE 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 : */ 14 9 : class StreamDebugMIDI_Interface : public StreamMIDI_Interface { 15 : public: 16 : /** 17 : * @brief Construct a debug MIDI interface on the given Stream. 18 : * 19 : * @param stream 20 : * The Stream interface. 21 : */ 22 9 : StreamDebugMIDI_Interface(Stream &stream) : StreamMIDI_Interface(stream) {} 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 9 : char firstChar = '\0'; 38 9 : char secondChar = '\0'; 39 : 40 : /** 41 : * @brief Convert a hexadecimal character to a 4-bit nibble. 42 : */ 43 29 : static uint8_t hexCharToNibble(char hex) { 44 29 : 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> 59 : class SerialDebugMIDI_Interface : public StreamDebugMIDI_Interface { 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 : */ 70 : SerialDebugMIDI_Interface(T &serial, 71 : unsigned long baud = AH::defaultBaudRate) 72 : : StreamDebugMIDI_Interface(serial), serial(serial), baud(baud) {} 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 : */ 89 : class HardwareSerialDebugMIDI_Interface 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 : */ 101 : HardwareSerialDebugMIDI_Interface(HardwareSerial &serial, 102 : unsigned long baud = AH::defaultBaudRate) 103 : : SerialDebugMIDI_Interface(serial, baud) {} 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 : */ 115 : class USBDebugMIDI_Interface 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 : */ 124 : USBDebugMIDI_Interface(unsigned long baud = AH::defaultBaudRate) 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 : */ 139 : class SoftwareSerialDebugMIDI_Interface 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 : */ 151 : SoftwareSerialDebugMIDI_Interface(SoftwareSerial &serial, 152 : unsigned long baud) 153 : : SerialDebugMIDI_Interface(serial, baud) {} 154 : }; 155 : #endif 156 : 157 : END_CS_NAMESPACE