Control Surface  1.1.0
MIDI Control Surface library for Arduino
MIDIInputElementPC.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 
16  public DoublyLinkable<MIDIInputElementPC> {
17  public:
18  /**
19  * @brief Constructor.
20  * @todo Documentation.
21  */
25  elements.append(this);
26  }
27 
28  /**
29  * @brief Destructor.
30  * @todo Documentation.
31  */
32  virtual ~MIDIInputElementPC() {
34  elements.remove(this);
35  }
36 
37  static void beginAll() {
39  for (MIDIInputElementPC &el : elements)
40  el.begin();
41  }
42 
43  /**
44  * @brief Reset all MIDIInputElementPC elements to their
45  * initial state.
46  *
47  * @see MIDIInputElementPC#reset
48  */
49  static void resetAll() {
51  for (MIDIInputElementPC &el : elements)
52  el.reset();
53  }
54 
55  /**
56  * @brief Update all MIDIInputElementPC elements.
57  */
58  static void updateAll() {
60  for (MIDIInputElementPC &el : elements)
61  el.update();
62  }
63 
64  /**
65  * @brief Update all MIDIInputElementPC elements with a new MIDI
66  * message.
67  *
68  * @see MIDIInputElementPC#updateWith
69  */
70  static void updateAllWith(const ChannelMessageMatcher &midimsg) {
71  for (MIDIInputElementPC &e : elements)
72  if (e.updateWith(midimsg)) {
73  e.moveDown();
74  return;
75  }
76  // No mutex required:
77  // e.moveDown may alter the list, but if it does, it always returns,
78  // and we stop iterating, so it doesn't matter.
79  }
80 
81  private:
82  /// Program Change doesn't have an address, so the target consists of just
83  /// the channel and the cable number.
85  getTarget(const ChannelMessageMatcher &midimsg) const override {
86  return {0, Channel(midimsg.channel), midimsg.CN};
87  }
88 
89  /**
90  * @brief Move down this element in the linked list of elements.
91  *
92  * This means that the element will be checked earlier on the next
93  * iteration.
94  */
95  void moveDown() {
97  elements.moveDown(this);
98  }
99 
101 #ifdef ESP32
102  static std::mutex mutex;
103 #endif
104 };
105 
106 #undef GUARD_LIST_LOCK
107 
ChannelMessageMatcher::CN
uint8_t CN
Definition: ChannelMessageMatcher.hpp:22
Channel
A type-safe class for MIDI channels.
Definition: Channel.hpp:13
MIDIInputElementPC::moveDown
void moveDown()
Move down this element in the linked list of elements.
Definition: MIDIInputElementPC.hpp:95
MIDIInputElementPC::getTarget
MIDICNChannelAddress getTarget(const ChannelMessageMatcher &midimsg) const override
Program Change doesn't have an address, so the target consists of just the channel and the cable numb...
Definition: MIDIInputElementPC.hpp:85
DoublyLinkedList::append
void append(Node *node)
Append a node to a linked list.
Definition: LinkedList.hpp:117
MIDIInputElementPC::MIDIInputElementPC
MIDIInputElementPC(const MIDICNChannelAddress &address)
Constructor.
Definition: MIDIInputElementPC.hpp:22
MIDIInputElement.hpp
MIDIInputElementPC::updateAll
static void updateAll()
Update all MIDIInputElementPC elements.
Definition: MIDIInputElementPC.hpp:58
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
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
MIDIInputElementPC::elements
static DoublyLinkedList< MIDIInputElementPC > elements
Definition: MIDIInputElementPC.hpp:100
GUARD_LIST_LOCK
#define GUARD_LIST_LOCK
Definition: MIDIInputElementPC.hpp:10
MIDIInputElementPC
Definition: MIDIInputElementPC.hpp:15
ChannelMessageMatcher::channel
uint8_t channel
Definition: ChannelMessageMatcher.hpp:19
MIDIInputElementPC::resetAll
static void resetAll()
Reset all MIDIInputElementPC elements to their initial state.
Definition: MIDIInputElementPC.hpp:49
DoublyLinkedList< MIDIInputElementPC >
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
MIDIInputElementPC::~MIDIInputElementPC
virtual ~MIDIInputElementPC()
Destructor.
Definition: MIDIInputElementPC.hpp:32
MIDIInputElementPC::beginAll
static void beginAll()
Definition: MIDIInputElementPC.hpp:37
MIDIInputElementPC::updateAllWith
static void updateAllWith(const ChannelMessageMatcher &midimsg)
Update all MIDIInputElementPC elements with a new MIDI message.
Definition: MIDIInputElementPC.hpp:70
MIDIInputElement
A class for objects that listen for incoming MIDI events.
Definition: MIDIInputElement.hpp:15