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