Control Surface  1.1.0
MIDI Control Surface library for Arduino
MIDIInputElement.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
7 
9 
10 /**
11  * @brief A class for objects that listen for incoming MIDI events.
12  *
13  * They can either update some kind of display, or they can just save the state.
14  */
16  protected:
17  MIDIInputElement() {} // not used, only for virtual inheritance
18  /**
19  * @brief Create a new MIDIInputElement that listens on the given address.
20  *
21  * @param address
22  * The MIDI address to listen to.
23  */
25 
26  public:
27  virtual ~MIDIInputElement() = default;
28 
29  /// Initialize the input element.
30  virtual void begin() {}
31 
32  /// Reset the input element to its initial state.
33  virtual void reset() {}
34 
35  /// Update the value of the input element. Used for decaying VU meters etc.
36  virtual void update() {}
37 
38  /// Receive a new MIDI message and update the internal state.
39  bool updateWith(const ChannelMessageMatcher &midimsg) {
40  MIDICNChannelAddress target = getTarget(midimsg);
41  if (!this->match(target))
42  return false;
43  DEBUGFN(F("MIDI message matches"));
44  if (!updateImpl(midimsg, target))
45  return false;
46  DEBUGFN(F("Updated"));
47  return true;
48  }
49 
50  private:
51  /// Update the internal state with the new MIDI message.
52  virtual bool updateImpl(const ChannelMessageMatcher &midimsg,
53  const MIDICNChannelAddress &target) = 0;
54 
55  /**
56  * @brief Extract the target address from a MIDI message.
57  * @note This base version of the function is only valid for messages
58  * that use data1 as an address (i.e. Note On, Note Off, Polyphonic
59  * Key Pressure and Control Change), because it assumes that the
60  * target address consists of the address (data 1), the MIDI
61  * channel and the cable number.
62  */
63  virtual MIDICNChannelAddress
64  getTarget(const ChannelMessageMatcher &midimsg) const {
65  return {int8_t(midimsg.data1), Channel(midimsg.channel), midimsg.CN};
66  }
67 
68  /**
69  * @brief Check if the address of the incoming MIDI message matches an
70  * address of this element.
71  * @note This base version of the function is only valid for non-Bankable
72  * MIDI input elements, it only matches if the address is equal to
73  * the address of this element.
74  */
75  virtual bool match(const MIDICNChannelAddress &target) const {
76  return MIDICNChannelAddress::matchSingle(this->address, target);
77  }
78 
79  protected:
81 };
82 
ChannelMessageMatcher::CN
uint8_t CN
Definition: ChannelMessageMatcher.hpp:22
MIDIInputElement::~MIDIInputElement
virtual ~MIDIInputElement()=default
Channel
A type-safe class for MIDI channels.
Definition: Channel.hpp:13
MIDIInputElement::reset
virtual void reset()
Reset the input element to its initial state.
Definition: MIDIInputElement.hpp:33
MIDIInputElement::begin
virtual void begin()
Initialize the input element.
Definition: MIDIInputElement.hpp:30
MIDIInputElement::MIDIInputElement
MIDIInputElement()
Definition: MIDIInputElement.hpp:17
MIDIInputElement::match
virtual bool match(const MIDICNChannelAddress &target) const
Check if the address of the incoming MIDI message matches an address of this element.
Definition: MIDIInputElement.hpp:75
MIDICNChannelAddress.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
MIDIInputElement::updateWith
bool updateWith(const ChannelMessageMatcher &midimsg)
Receive a new MIDI message and update the internal state.
Definition: MIDIInputElement.hpp:39
ChannelMessageMatcher
Struct for easily matching MIDI messages.
Definition: ChannelMessageMatcher.hpp:10
ChannelMessageMatcher.hpp
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
MIDIInputElement::update
virtual void update()
Update the value of the input element. Used for decaying VU meters etc.
Definition: MIDIInputElement.hpp:36
MIDICNChannelAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDICNChannelAddress.hpp:82
MIDIInputElement::updateImpl
virtual bool updateImpl(const ChannelMessageMatcher &midimsg, const MIDICNChannelAddress &target)=0
Update the internal state with the new MIDI message.
ChannelMessageMatcher::channel
uint8_t channel
Definition: ChannelMessageMatcher.hpp:19
ChannelMessageMatcher::data1
uint8_t data1
Definition: ChannelMessageMatcher.hpp:20
MIDIInputElement::getTarget
virtual MIDICNChannelAddress getTarget(const ChannelMessageMatcher &midimsg) const
Extract the target address from a MIDI message.
Definition: MIDIInputElement.hpp:64
DEBUGFN
#define DEBUGFN(x)
Print an expression and its function (function name and line number) to the debug output if debugging...
Definition: Debug.hpp:93
MIDICNChannelAddress::matchSingle
static bool matchSingle(const MIDICNChannelAddress &toMatch, const MIDICNChannelAddress &base)
Check if two addresses match.
Definition: MIDICNChannelAddress.cpp:43
MIDIInputElement::address
const MIDICNChannelAddress address
Definition: MIDIInputElement.hpp:80
MIDIInputElement
A class for objects that listen for incoming MIDI events.
Definition: MIDIInputElement.hpp:15
MIDIInputElement::MIDIInputElement
MIDIInputElement(const MIDICNChannelAddress &address)
Create a new MIDIInputElement that listens on the given address.
Definition: MIDIInputElement.hpp:24
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23