Line data Source code
1 : #pragma once 2 : 3 : #include "MIDI_Interface.hpp" 4 : #include <AH/Arduino-Wrapper.h> // Stream 5 : #include <AH/STL/utility> 6 : #include <AH/Teensy/TeensyUSBTypes.hpp> 7 : #include <MIDI_Parsers/SerialMIDI_Parser.hpp> 8 : #include <Settings/SettingsWrapper.hpp> 9 : 10 : BEGIN_CS_NAMESPACE 11 : 12 : /** 13 : * @brief A class for MIDI interfaces sending and receiving MIDI messages 14 : * over a Stream. 15 : * 16 : * @ingroup MIDIInterfaces 17 : */ 18 : class StreamMIDI_Interface : public MIDI_Interface { 19 : public: 20 : /// Constructor. 21 : /// @param stream 22 : /// Reference to the Stream interface to read and send MIDI data 23 : /// from/to. 24 13 : StreamMIDI_Interface(Stream &stream) : stream(stream) {} 25 : 26 : /// Try reading and parsing a single incoming MIDI message. 27 : /// @return Returns the type of the read message, or 28 : /// `MIDIReadEvent::NO_MESSAGE` if no MIDI message was available. 29 : MIDIReadEvent read(); 30 : 31 : /// Return the received channel voice message. 32 : ChannelMessage getChannelMessage() const; 33 : /// Return the received system common message. 34 : SysCommonMessage getSysCommonMessage() const; 35 : /// Return the received real-time message. 36 : RealTimeMessage getRealTimeMessage() const; 37 : /// Return the received system exclusive message. 38 : SysExMessage getSysExMessage() const; 39 : 40 : void update() override; 41 : 42 : protected: 43 : void sendChannelMessageImpl(ChannelMessage) override; 44 : void sendSysCommonImpl(SysCommonMessage) override; 45 : void sendSysExImpl(SysExMessage) override; 46 : void sendRealTimeImpl(RealTimeMessage) override; 47 0 : void sendNowImpl() override {} 48 : 49 : protected: 50 : #if !DISABLE_PIPES 51 1 : void handleStall() override { MIDI_Interface::handleStall(this); } 52 : #ifdef DEBUG_OUT 53 : const char *getName() const override { return "ser"; } 54 : #endif 55 : #endif 56 : 57 : protected: 58 : Stream &stream; 59 : SerialMIDI_Parser parser; 60 : }; 61 : 62 : // -------------------------------------------------------------------------- // 63 : 64 : /** 65 : * @brief A wrapper class for MIDI interfaces sending and receiving 66 : * MIDI messages over a Serial port of generic class S. 67 : * 68 : * @note This is a template class because the type of the Serial object 69 : * is completely different on different architectures, and they 70 : * do not share a common super-class that has a `begin` method. 71 : * 72 : * @ingroup MIDIInterfaces 73 : */ 74 : template <class S> 75 : class SerialMIDI_Interface : public StreamMIDI_Interface { 76 : public: 77 : /** 78 : * @brief Create a new MIDI Interface on the given Serial interface 79 : * with the given baud rate. 80 : * 81 : * @param serial 82 : * The Serial interface. 83 : * @param baud 84 : * The baud rate for the Serial interface. 85 : */ 86 : SerialMIDI_Interface(S &serial, unsigned long baud = MIDI_BAUD) 87 : : StreamMIDI_Interface(serial), baud(baud) {} 88 : 89 : /** 90 : * @brief Start the Serial interface at the predefined baud rate. 91 : */ 92 : void begin() override { static_cast<S &>(stream).begin(baud); } 93 : 94 : private: 95 : const unsigned long baud; 96 : }; 97 : 98 : // -------------------------------------------------------------------------- // 99 : 100 : /** 101 : * @brief A class for MIDI interfaces sending and receiving 102 : * MIDI messages over a Hardware Serial port. 103 : * 104 : * @ingroup MIDIInterfaces 105 : */ 106 : class HardwareSerialMIDI_Interface 107 : : public SerialMIDI_Interface<HardwareSerial> { 108 : public: 109 : /** 110 : * @brief Construct a new MIDI Interface on the given HardwareSerial 111 : * interface with the given baud rate. 112 : * 113 : * @param serial 114 : * The HardwareSerial interface. 115 : * @param baud 116 : * The baud rate for the serial interface. 117 : */ 118 : HardwareSerialMIDI_Interface(HardwareSerial &serial, 119 : unsigned long baud = MIDI_BAUD) 120 : : SerialMIDI_Interface(serial, baud) {} 121 : }; 122 : 123 : // -------------------------------------------------------------------------- // 124 : 125 : /** 126 : * @brief A class for MIDI interfaces sending and receiving 127 : * MIDI messages over the Serial port of the USB connection. 128 : * 129 : * @ingroup MIDIInterfaces 130 : */ 131 : class USBSerialMIDI_Interface : public SerialMIDI_Interface<decltype(Serial)> { 132 : public: 133 : /** 134 : * @brief Construct a USBSerialMIDI_Interface with the given baud rate. 135 : * 136 : * @param baud 137 : * The baud rate to start the USB Serial connection with. 138 : */ 139 : USBSerialMIDI_Interface(unsigned long baud) 140 : : SerialMIDI_Interface(Serial, baud) {} 141 : }; 142 : 143 : // -------------------------------------------------------------------------- // 144 : 145 : #if !defined(TEENSYDUINO) || \ 146 : (defined(TEENSYDUINO) && defined(TEENSY_SERIALUSB_ENABLED)) 147 : /** 148 : * @brief A class for MIDI Interfaces sending and receiving 149 : * data over the USB Serial CDC connection for the use 150 : * with the [Hairless MIDI<->Serial Bridge](http://projectgus.github.io/hairless-midiserial/). 151 : * 152 : * @ingroup MIDIInterfaces 153 : */ 154 : class HairlessMIDI_Interface : public USBSerialMIDI_Interface { 155 : public: 156 : /** 157 : * @brief Construct a HairlessMIDI_Interface. 158 : * 159 : * The default Hairless baud rate of 115200 baud is used. 160 : * This can be changed in the Settings.hpp file. 161 : */ 162 : HairlessMIDI_Interface(unsigned long baud = HAIRLESS_BAUD) 163 : : USBSerialMIDI_Interface(baud) {} 164 : }; 165 : #endif 166 : 167 : END_CS_NAMESPACE