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