Control Surface  1.1.0
MIDI Control Surface library for Arduino
MIDIInputElementCC.hpp
Go to the documentation of this file.
1 #pragma once
2 
5 
6 
7 #if defined(ESP32)
8 #include <mutex>
9 #define GUARD_LIST_LOCK std::lock_guard<std::mutex> _guard(mutex)
10 #else
11 #define GUARD_LIST_LOCK
12 #endif
13 
15 
16 /**
17  * @brief Class for objects that listen for incoming MIDI Controller Change
18  * events.
19  *
20  * All instances are added to a linked list that can be traversed to update
21  * all of them when a new MIDI CC event is received.
22  */
24  public DoublyLinkable<MIDIInputElementCC> {
25  public:
26  /**
27  * @brief Create a new MIDIInputElementCC that listens on the given
28  * address.
29  *
30  * Add the element to the linked list.
31  *
32  * @param address
33  * The MIDI address to listen to. (Controller number [0, 119],
34  * Channel [1, 16], Cable Number [0, 15].)
35  */
39  elements.append(this);
40  }
41 
42  /// Destructor: delete from the linked list.
43  virtual ~MIDIInputElementCC() {
45  elements.remove(this);
46  }
47 
48  /// Initialize all MIDIInputElementCC elements.
49  /// @see MIDIInputElementCC#begin
50  static void beginAll() {
52  for (MIDIInputElementCC &e : elements)
53  e.begin();
54  }
55 
56  /// Update all MIDIInputElementCC elements.
57  /// @see MIDIInputElementCC#update
58  static void updateAll() {
60  for (MIDIInputElementCC &e : elements)
61  e.update();
62  }
63 
64  /// Reset all MIDIInputElementCC elements to their initial state.
65  /// @see MIDIInputElementCC::reset
66  static void resetAll() {
68  for (MIDIInputElementCC &e : elements)
69  e.reset();
70  }
71 
72  /// Update all MIDIInputElementCC elements with a new MIDI message.
73  /// @see MIDIInputElementCC#updateWith
74  static void updateAllWith(const ChannelMessageMatcher &midimsg) {
75  for (MIDIInputElementCC &e : elements)
76  if (e.updateWith(midimsg)) {
77  e.moveDown();
78  return;
79  }
80  // No mutex required:
81  // e.moveDown may alter the list, but if it does, it always returns,
82  // and we stop iterating, so it doesn't matter.
83  }
84 
85  private:
86  /**
87  * @brief Move down this element in the linked list of elements.
88  *
89  * This means that the element will be checked earlier on the next
90  * iteration.
91  */
92  void moveDown() {
94  elements.moveDown(this);
95  }
96 
98 #ifdef ESP32
99  static std::mutex mutex;
100 #endif
101 };
102 
103 #undef GUARD_LIST_LOCK
104 
MIDIInputElementCC::MIDIInputElementCC
MIDIInputElementCC(const MIDICNChannelAddress &address)
Create a new MIDIInputElementCC that listens on the given address.
Definition: MIDIInputElementCC.hpp:36
MIDIInputElementCC::beginAll
static void beginAll()
Initialize all MIDIInputElementCC elements.
Definition: MIDIInputElementCC.hpp:50
DoublyLinkedList::append
void append(Node *node)
Append a node to a linked list.
Definition: LinkedList.hpp:117
MIDIInputElementCC::~MIDIInputElementCC
virtual ~MIDIInputElementCC()
Destructor: delete from the linked list.
Definition: MIDIInputElementCC.hpp:43
MIDIInputElementCC::updateAllWith
static void updateAllWith(const ChannelMessageMatcher &midimsg)
Update all MIDIInputElementCC elements with a new MIDI message.
Definition: MIDIInputElementCC.hpp:74
MIDIInputElement.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
MIDIInputElementCC
Class for objects that listen for incoming MIDI Controller Change events.
Definition: MIDIInputElementCC.hpp:23
ChannelMessageMatcher
Struct for easily matching MIDI messages.
Definition: ChannelMessageMatcher.hpp:10
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
MIDICNChannelAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDICNChannelAddress.hpp:82
MIDIInputElementCC::resetAll
static void resetAll()
Reset all MIDIInputElementCC elements to their initial state.
Definition: MIDIInputElementCC.hpp:66
MIDIInputElementCC::updateAll
static void updateAll()
Update all MIDIInputElementCC elements.
Definition: MIDIInputElementCC.hpp:58
GUARD_LIST_LOCK
#define GUARD_LIST_LOCK
Definition: MIDIInputElementCC.hpp:11
MIDIInputElementCC::elements
static DoublyLinkedList< MIDIInputElementCC > elements
Definition: MIDIInputElementCC.hpp:97
DoublyLinkedList< MIDIInputElementCC >
DoublyLinkable
A class that can be inherited from to allow inserting into a DoublyLinkedList.
Definition: LinkedList.hpp:302
MIDIInputElement::address
const MIDICNChannelAddress address
Definition: MIDIInputElement.hpp:80
DoublyLinkedList::moveDown
void moveDown(Node *node)
Move down the given node in the linked list.
Definition: LinkedList.hpp:232
MIDIInputElement
A class for objects that listen for incoming MIDI events.
Definition: MIDIInputElement.hpp:15
MIDIInputElementCC::moveDown
void moveDown()
Move down this element in the linked list of elements.
Definition: MIDIInputElementCC.hpp:92