Control Surface  1.1.0
MIDI Control Surface library for Arduino
MIDIInputElementSysEx.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "MIDIInputElement.hpp"
5 
6 #if defined(ESP32)
7 #include <mutex>
8 #define GUARD_LIST_LOCK std::lock_guard<std::mutex> _guard(mutex)
9 #else
10 #define GUARD_LIST_LOCK
11 #endif
12 
14 
15 /**
16  * @brief Class for objects that listen for incoming MIDI SysEx events.
17  *
18  * @ingroup MIDIInputElements
19  */
20 class MIDIInputElementSysEx : public DoublyLinkable<MIDIInputElementSysEx> {
21  protected:
22  /**
23  * @brief Constructor.
24  * @todo Documentation.
25  */
27  : CN{CN} {
29  elements.append(this);
30  }
31 
32  public:
33  /**
34  * @brief Destructor.
35  * @todo Documentation.
36  */
39  elements.remove(this);
40  }
41 
42  /// Initialize the input element.
43  virtual void begin() {}
44 
45  /// Reset the input element to its initial state.
46  virtual void reset() {}
47 
48  /// Update the value of the input element. Used for decaying VU meters etc.
49  virtual void update() {}
50 
51  /**
52  * @brief Initialize all MIDIInputElementSysEx elements.
53  *
54  * @see MIDIInputElementSysEx#begin
55  */
56  static void beginAll() {
59  e.begin();
60  }
61 
62  /**
63  * @brief Update all MIDIInputElementSysEx elements.
64  *
65  * @see MIDIInputElementSysEx#update
66  */
67  static void updateAll() {
70  e.update();
71  }
72 
73  /**
74  * @brief Reset all MIDIInputElementSysEx elements to their initial state.
75  *
76  * @see MIDIInputElementSysEx#reset
77  */
78  static void resetAll() {
81  e.reset();
82  }
83 
84  /**
85  * @brief Update all MIDIInputElementSysEx elements with a new MIDI
86  * message.
87  *
88  * @see MIDIInputElementSysEx#updateWith
89  */
90  static void updateAllWith(SysExMessage midimsg) {
92  if (e.updateWith(midimsg)) {
93  e.moveDown();
94  return;
95  }
96  // No mutex required:
97  // e.moveDown may alter the list, but if it does, it always returns,
98  // and we stop iterating, so it doesn't matter.
99  }
100 
101  private:
102  /// @todo Documentation.
103  bool updateWith(SysExMessage midimsg) {
104  return midimsg.CN == this->CN && updateImpl(midimsg);
105  }
106 
107  /// @todo Documentation.
108  virtual bool updateImpl(SysExMessage midimsg) = 0;
109 
110  /**
111  * @brief Move down this element in the linked list of elements.
112  *
113  * This means that the element will be checked earlier on the next
114  * iteration.
115  */
116  void moveDown() {
118  elements.moveDown(this);
119  }
120 
121  uint8_t CN;
122 
124 #ifdef ESP32
125  static std::mutex mutex;
126 #endif
127 };
128 
129 #undef GUARD_LIST_LOCK
130 
MIDIInputElementSysEx::updateWith
bool updateWith(SysExMessage midimsg)
Definition: MIDIInputElementSysEx.hpp:103
MIDIInputElementSysEx::updateImpl
virtual bool updateImpl(SysExMessage midimsg)=0
SysExMessage
Definition: MIDI_Parser.hpp:64
DoublyLinkedList::append
void append(Node *node)
Append a node to a linked list.
Definition: LinkedList.hpp:117
MIDIInputElement.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
MIDIInputElementSysEx::beginAll
static void beginAll()
Initialize all MIDIInputElementSysEx elements.
Definition: MIDIInputElementSysEx.hpp:56
MIDIInputElementSysEx::elements
static DoublyLinkedList< MIDIInputElementSysEx > elements
Definition: MIDIInputElementSysEx.hpp:123
MIDIInputElementSysEx::moveDown
void moveDown()
Move down this element in the linked list of elements.
Definition: MIDIInputElementSysEx.hpp:116
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
LinkedList.hpp
DoublyLinkedList::remove
void remove(Node *node)
Remove a node from the linked list.
Definition: LinkedList.hpp:200
MIDIInputElementSysEx::MIDIInputElementSysEx
MIDIInputElementSysEx(uint8_t CN=0)
Constructor.
Definition: MIDIInputElementSysEx.hpp:26
MIDIInputElementSysEx::begin
virtual void begin()
Initialize the input element.
Definition: MIDIInputElementSysEx.hpp:43
MIDIInputElementSysEx::resetAll
static void resetAll()
Reset all MIDIInputElementSysEx elements to their initial state.
Definition: MIDIInputElementSysEx.hpp:78
MIDIInputElementSysEx::CN
uint8_t CN
Definition: MIDIInputElementSysEx.hpp:121
MIDIInputElementSysEx::update
virtual void update()
Update the value of the input element. Used for decaying VU meters etc.
Definition: MIDIInputElementSysEx.hpp:49
GUARD_LIST_LOCK
#define GUARD_LIST_LOCK
Definition: MIDIInputElementSysEx.hpp:10
MIDIInputElementSysEx
Class for objects that listen for incoming MIDI SysEx events.
Definition: MIDIInputElementSysEx.hpp:20
DoublyLinkedList< MIDIInputElementSysEx >
DoublyLinkable
A class that can be inherited from to allow inserting into a DoublyLinkedList.
Definition: LinkedList.hpp:302
MIDIInputElementSysEx::~MIDIInputElementSysEx
virtual ~MIDIInputElementSysEx()
Destructor.
Definition: MIDIInputElementSysEx.hpp:37
MIDIInputElementSysEx::updateAll
static void updateAll()
Update all MIDIInputElementSysEx elements.
Definition: MIDIInputElementSysEx.hpp:67
SysExMessage::CN
uint8_t CN
Definition: MIDI_Parser.hpp:74
DoublyLinkedList::moveDown
void moveDown(Node *node)
Move down the given node in the linked list.
Definition: LinkedList.hpp:232
MIDIInputElementSysEx::reset
virtual void reset()
Reset the input element to its initial state.
Definition: MIDIInputElementSysEx.hpp:46
MIDIInputElementSysEx::updateAllWith
static void updateAllWith(SysExMessage midimsg)
Update all MIDIInputElementSysEx elements with a new MIDI message.
Definition: MIDIInputElementSysEx.hpp:90