Line data Source code
1 : /* ✔ */ 2 : 3 : #pragma once 4 : 5 : #include <AH/Containers/Updatable.hpp> 6 : #include <AH/Hardware/FilteredAnalog.hpp> 7 : #include <AH/Timing/MillisMicrosTimer.hpp> 8 : #include <Display/DisplayElement.hpp> 9 : #include <Display/DisplayInterface.hpp> 10 : #include <MIDI_Interfaces/MIDI_Interface.hpp> 11 : #include <Settings/SettingsWrapper.hpp> 12 : 13 : BEGIN_CS_NAMESPACE 14 : 15 : using AH::FilteredAnalog; 16 : using AH::NormalUpdatable; 17 : using AH::Timer; 18 : using AH::Updatable; 19 : 20 : /** 21 : * @brief This class ensures initialization, updating, and interaction between 22 : * all other classes, it's the glue that holds everything together. 23 : * 24 : * @ingroup ControlSurfaceModule 25 : */ 26 2 : class Control_Surface_ : public MIDI_Callbacks { 27 : public: 28 : // Copying is not allowed 29 : Control_Surface_(Control_Surface_ const &) = delete; 30 : void operator=(Control_Surface_ const &) = delete; 31 : 32 : /** 33 : * @brief Return the static Control_Surface_ instance. 34 : * (Control_Surface_ is a singleton.) 35 : */ 36 : static Control_Surface_ &getInstance(); 37 : 38 : /** 39 : * @brief Initialize the Control_Surface. 40 : */ 41 : void begin(); 42 : 43 : /** 44 : * @brief Update all MIDI elements, send MIDI events and read MIDI input. 45 : */ 46 : void loop(); 47 : 48 : /** 49 : * @brief Get the MIDI interface of the Control Surface. 50 : * 51 : * @return A reference to the Control Surface's MIDI interface. 52 : * 53 : * @todo This violate's the Law of Demeter. 54 : */ 55 : MIDI_Interface &MIDI(); 56 : 57 : /** 58 : * @brief Update all MIDI interfaces to receive new MIDI events. 59 : */ 60 : void updateMidiInput(); 61 : 62 : /** 63 : * @brief Update all MIDIInputElement%s. 64 : */ 65 : void updateInputs(); 66 : 67 : /** 68 : * @brief Clear, draw and display all displays. 69 : */ 70 : void updateDisplays(); 71 : 72 : private: 73 : /** 74 : * @brief Control_Surface_ is a singleton, so the constructor is private. 75 : */ 76 4 : Control_Surface_() = default; 77 : 78 : /** 79 : * @brief The callback to be called when a MIDI channel message is 80 : * received. 81 : */ 82 : void onChannelMessage(Parsing_MIDI_Interface &midi) override; 83 : 84 : /** 85 : * @brief The callback to be called when a MIDI System Exclusive message 86 : * is received. 87 : */ 88 : void onSysExMessage(Parsing_MIDI_Interface &midi) override; 89 : 90 : /** 91 : * @brief The callback to be called when a MIDI Real-Time message is 92 : * received. 93 : */ 94 : void onRealtimeMessage(Parsing_MIDI_Interface &midi, 95 : uint8_t message) override; 96 : 97 : /// A timer to know when to update the analog inputs. 98 2 : Timer<micros> potentiometerTimer = {AH::FILTERED_INPUT_UPDATE_INTERVAL}; 99 : /// A timer to know when to refresh the displays. 100 2 : Timer<micros> displayTimer = {1000000UL / MAX_FPS}; 101 : 102 : public: 103 : /// Callback function type for channel messages. Return true if handling is 104 : /// done in the user-provided callback, false if `Control_Surface` 105 : /// should handle the message. 106 : using ChannelMessageCallback = bool (*)(ChannelMessage); 107 : /// Callback function type for SysEx messages. Return true if handling is 108 : /// done in the user-provided callback, false if `Control_Surface` 109 : /// should handle the message. 110 : using SysExMessageCallback = bool (*)(SysExMessage); 111 : /// Callback function type for Real-Time messages. Return true if handling 112 : /// is done in the user-provided callback, false if `Control_Surface` 113 : /// should handle the message. 114 : using RealTimeMessageCallback = bool (*)(RealTimeMessage); 115 : 116 : /// Set the MIDI input callbacks. 117 : void 118 : setMIDIInputCallbacks(ChannelMessageCallback channelMessageCallback, 119 : SysExMessageCallback sysExMessageCallback, 120 : RealTimeMessageCallback realTimeMessageCallback) { 121 : this->channelMessageCallback = channelMessageCallback; 122 : this->sysExMessageCallback = sysExMessageCallback; 123 : this->realTimeMessageCallback = realTimeMessageCallback; 124 : } 125 : 126 : private: 127 2 : ChannelMessageCallback channelMessageCallback = nullptr; 128 2 : SysExMessageCallback sysExMessageCallback = nullptr; 129 2 : RealTimeMessageCallback realTimeMessageCallback = nullptr; 130 : }; 131 : 132 : /// A predefined instance of the Control Surface to use in the Arduino sketches. 133 : extern Control_Surface_ &Control_Surface; 134 : 135 : END_CS_NAMESPACE