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 : #endif 53 : 54 : protected: 55 : Stream &stream; 56 : SerialMIDI_Parser parser; 57 : }; 58 : 59 : // -------------------------------------------------------------------------- // 60 : 61 : /** 62 : * @brief A wrapper class for MIDI interfaces sending and receiving 63 : * MIDI messages over a Serial port of generic class S. 64 : * 65 : * @note This is a template class because the type of the Serial object 66 : * is completely different on different architectures, and they 67 : * do not share a common super-class that has a `begin` method. 68 : * 69 : * @ingroup MIDIInterfaces 70 : */ 71 : template <class S> 72 : class SerialMIDI_Interface : public StreamMIDI_Interface { 73 : public: 74 : /** 75 : * @brief Create a new MIDI Interface on the given Serial interface 76 : * with the given baud rate. 77 : * 78 : * @param serial 79 : * The Serial interface. 80 : * @param baud 81 : * The baud rate for the Serial interface. 82 : */ 83 : SerialMIDI_Interface(S &serial, unsigned long baud = MIDI_BAUD) 84 : : StreamMIDI_Interface(serial), baud(baud) {} 85 : 86 : /** 87 : * @brief Start the Serial interface at the predefined baud rate. 88 : */ 89 : void begin() override { static_cast<S &>(stream).begin(baud); } 90 : 91 : private: 92 : const unsigned long baud; 93 : }; 94 : 95 : // -------------------------------------------------------------------------- // 96 : 97 : /** 98 : * @brief A class for MIDI interfaces sending and receiving 99 : * MIDI messages over a Hardware Serial port. 100 : * 101 : * @ingroup MIDIInterfaces 102 : */ 103 : class HardwareSerialMIDI_Interface 104 : : public SerialMIDI_Interface<HardwareSerial> { 105 : public: 106 : /** 107 : * @brief Construct a new MIDI Interface on the given HardwareSerial 108 : * interface with the given baud rate. 109 : * 110 : * @param serial 111 : * The HardwareSerial interface. 112 : * @param baud 113 : * The baud rate for the serial interface. 114 : */ 115 : HardwareSerialMIDI_Interface(HardwareSerial &serial, 116 : unsigned long baud = MIDI_BAUD) 117 : : SerialMIDI_Interface(serial, baud) {} 118 : }; 119 : 120 : // -------------------------------------------------------------------------- // 121 : 122 : /** 123 : * @brief A class for MIDI interfaces sending and receiving 124 : * MIDI messages over the Serial port of the USB connection. 125 : * 126 : * @ingroup MIDIInterfaces 127 : */ 128 : class USBSerialMIDI_Interface : public SerialMIDI_Interface<decltype(Serial)> { 129 : public: 130 : /** 131 : * @brief Construct a USBSerialMIDI_Interface with the given baud rate. 132 : * 133 : * @param baud 134 : * The baud rate to start the USB Serial connection with. 135 : */ 136 : USBSerialMIDI_Interface(unsigned long baud) 137 : : SerialMIDI_Interface(Serial, baud) {} 138 : }; 139 : 140 : // -------------------------------------------------------------------------- // 141 : 142 : #if !defined(TEENSYDUINO) || \ 143 : (defined(TEENSYDUINO) && defined(TEENSY_SERIALUSB_ENABLED)) 144 : /** 145 : * @brief A class for MIDI Interfaces sending and receiving 146 : * data over the USB Serial CDC connection for the use 147 : * with the [Hairless MIDI<->Serial Bridge] 148 : * (http://projectgus.github.io/hairless-midiserial/). 149 : * 150 : * @ingroup MIDIInterfaces 151 : */ 152 : class HairlessMIDI_Interface : public USBSerialMIDI_Interface { 153 : public: 154 : /** 155 : * @brief Construct a HairlessMIDI_Interface. 156 : * 157 : * The default Hairless baud rate of 115200 baud is used. 158 : * This can be changed in the Settings.hpp file. 159 : */ 160 : HairlessMIDI_Interface(unsigned long baud = HAIRLESS_BAUD) 161 : : USBSerialMIDI_Interface(baud){}; 162 : }; 163 : #endif 164 : 165 : END_CS_NAMESPACE