Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
MIDIAddress.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
5#include <Def/Def.hpp>
6
8
13struct __attribute__((packed)) RawMIDIAddress {
14 bool valid : 1;
15 uint8_t address : 7;
16 uint8_t channel : 4;
17 uint8_t cableNumber : 4;
18};
19
25 friend class MIDIAddress;
26
27 public:
30
31 constexpr MIDIChannelCable() : addresses {0, 0, 0, 0} {}
32 constexpr MIDIChannelCable(Channel channel, Cable cableNumber = Cable_1)
33 : addresses {1, 0, channel.getRaw(), cableNumber.getRaw()} {}
34
35 constexpr static MIDIChannelCable invalid() { return {}; }
36
38
39 public:
42
44 constexpr Channel getChannel() const { return Channel {addresses.channel}; }
46 constexpr uint8_t getRawChannel() const { return addresses.channel; }
47
49 constexpr Cable getCableNumber() const {
50 return Cable(addresses.cableNumber);
51 }
53 constexpr uint8_t getRawCableNumber() const {
54 return addresses.cableNumber;
55 }
56
58
59 public:
62
67 constexpr bool operator==(MIDIChannelCable rhs) const {
68 return this->addresses.valid && rhs.addresses.valid &&
69 this->addresses.channel == rhs.addresses.channel &&
70 this->addresses.cableNumber == rhs.addresses.cableNumber;
71 }
72
77 constexpr bool operator!=(MIDIChannelCable rhs) const {
78 return this->addresses.valid && rhs.addresses.valid &&
79 !(this->addresses.channel == rhs.addresses.channel &&
80 this->addresses.cableNumber == rhs.addresses.cableNumber);
81 }
82
84 constexpr bool isValid() const { return addresses.valid; }
85
88 constexpr explicit operator bool() const { return isValid(); }
89
91
92 public:
95
97 static bool matchSingle(MIDIChannelCable toMatch, MIDIChannelCable base) {
98 return base == toMatch;
99 }
100
102
103 protected:
104 constexpr MIDIChannelCable(RawMIDIAddress addresses)
105 : addresses(addresses) {}
106
107 private:
108 RawMIDIAddress addresses;
109};
110
114 friend class MIDIAddress;
115
116 public:
117 constexpr RelativeMIDIAddress() : addresses {0, 0, 0, 0} {}
118 constexpr RelativeMIDIAddress(int deltaAddress, int deltaChannel = 0,
119 int deltaCableNumber = 0)
120 : addresses {
121 1,
122 static_cast<uint8_t>(deltaAddress),
123 static_cast<uint8_t>(deltaChannel),
124 static_cast<uint8_t>(deltaCableNumber),
125 } {}
126 constexpr bool isValid() const { return addresses.valid; }
127
131
132 private:
133 RawMIDIAddress addresses;
134 static_assert(((-1) & 0x7F) == 0x7F,
135 "Negative numbers must be two's complement");
136};
137
146 public:
149
151 constexpr MIDIAddress()
152 : addresses {
153 0,
154 0,
155 0,
156 0,
157 } {}
158
170 constexpr MIDIAddress(int address, MIDIChannelCable channelCN)
171 : addresses {
172 1,
173 static_cast<uint8_t>(address),
174 channelCN.getRawChannel(),
175 channelCN.getRawCableNumber(),
176 } {} // Deliberate overflow for negative numbers
177
193 constexpr MIDIAddress(int address, Channel channel = Channel_1,
194 Cable cableNumber = Cable_1)
195 : addresses {
196 1,
197 static_cast<uint8_t>(address),
198 channel.getRaw(),
199 cableNumber.getRaw(),
200 } {} // Deliberate overflow for negative numbers
201
214 constexpr MIDIAddress(int address, Cable cableNumber)
215 : addresses {
216 1,
217 static_cast<uint8_t>(address),
218 0,
219 cableNumber.getRaw(),
220 } {} // Deliberate overflow for negative numbers
221
232 constexpr MIDIAddress(Channel channel, Cable cableNumber = Cable_1)
233 : addresses {
234 1,
235 0,
236 channel.getRaw(),
237 cableNumber.getRaw(),
238 } {}
239
246 constexpr MIDIAddress(MIDIChannelCable address)
247 : addresses(address.addresses) {}
248
250 constexpr static MIDIAddress invalid() { return {}; }
251
253
254 public:
257
260
263
266
269
271
272 public:
275
277 constexpr uint8_t getAddress() const { return addresses.address; }
278
280 constexpr Channel getChannel() const { return Channel {addresses.channel}; }
282 constexpr uint8_t getRawChannel() const { return addresses.channel; }
283
285 constexpr Cable getCableNumber() const {
286 return Cable(addresses.cableNumber);
287 }
289 constexpr uint8_t getRawCableNumber() const {
290 return addresses.cableNumber;
291 }
293 constexpr MIDIChannelCable getChannelCable() const { return {addresses}; }
294
296
297 public:
300
305 constexpr bool operator==(MIDIAddress rhs) const {
306 return this->addresses.valid && rhs.addresses.valid &&
307 this->addresses.address == rhs.addresses.address &&
308 this->addresses.channel == rhs.addresses.channel &&
309 this->addresses.cableNumber == rhs.addresses.cableNumber;
310 }
311
316 constexpr bool operator!=(MIDIAddress rhs) const {
317 return this->addresses.valid && rhs.addresses.valid &&
318 !(this->addresses.address == rhs.addresses.address &&
319 this->addresses.channel == rhs.addresses.channel &&
320 this->addresses.cableNumber == rhs.addresses.cableNumber);
321 }
322
324 constexpr bool isValid() const { return addresses.valid; }
325
328 constexpr explicit operator bool() const { return isValid(); }
329
331
332 public:
335
337 static bool matchSingle(MIDIAddress toMatch, MIDIAddress base) {
338 return base == toMatch;
339 }
340
349 static bool matchAddressInRange(MIDIAddress toMatch, MIDIAddress base,
350 uint8_t length);
351
353
354 private:
355 RawMIDIAddress addresses;
356};
357
constexpr Cable Cable_1
Definition Cable.hpp:118
constexpr Channel Channel_1
Definition Channel.hpp:118
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
A type-safe class for MIDI USB Cable numbers.
Definition Cable.hpp:13
A type-safe class for MIDI channels.
Definition Channel.hpp:13
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
constexpr bool isValid() const
Check if the MIDI address is valid.
MIDIAddress operator-(RelativeMIDIAddress rhs) const
Subtract a relative offset.
constexpr MIDIAddress(int address, Channel channel=Channel_1, Cable cableNumber=Cable_1)
Constructor.
constexpr MIDIAddress(int address, Cable cableNumber)
Constructor.
RawMIDIAddress addresses
constexpr uint8_t getRawChannel() const
Get the channel as an integer [0, 15].
static constexpr MIDIAddress invalid()
Return an invalid address.
constexpr MIDIChannelCable getChannelCable() const
Get the channel and cable number.
static bool matchSingle(MIDIAddress toMatch, MIDIAddress base)
Check if two addresses match (are equal).
constexpr uint8_t getAddress() const
Get the address [0, 127].
MIDIAddress & operator-=(RelativeMIDIAddress rhs)
Subtract a relative offset from this address.
MIDIAddress operator+(RelativeMIDIAddress rhs) const
Add a relative offset.
constexpr uint8_t getRawCableNumber() const
Get the cable number as an integer [0, 15].
constexpr MIDIAddress(int address, MIDIChannelCable channelCN)
Constructor.
constexpr Channel getChannel() const
Get the channel [Channel_1, Channel_16].
constexpr MIDIAddress(Channel channel, Cable cableNumber=Cable_1)
Constructor.
static bool matchAddressInRange(MIDIAddress toMatch, MIDIAddress base, uint8_t length)
Check if an address falls within a range of MIDI addresses, starting with address base,...
constexpr MIDIAddress(MIDIChannelCable address)
Constructor.
MIDIAddress & operator+=(RelativeMIDIAddress rhs)
Add a relative offset to this address.
constexpr bool operator!=(MIDIAddress rhs) const
Check for inequality: two addresses are not equal if and only if they are both valid addresses and ha...
constexpr Cable getCableNumber() const
Get the cable number [Cable_1, Cable_16].
constexpr MIDIAddress()
Default constructor, creates an invalid address.
constexpr bool operator==(MIDIAddress rhs) const
Check for equality: two addresses are equal if and only if they are both valid addresses and the MIDI...
A class for saving a MIDI channel and cable number.
constexpr bool isValid() const
Check if the MIDI address is valid.
constexpr MIDIChannelCable(RawMIDIAddress addresses)
RawMIDIAddress addresses
static constexpr MIDIChannelCable invalid()
constexpr uint8_t getRawChannel() const
Get the channel as an integer [0, 15].
constexpr uint8_t getRawCableNumber() const
Get the cable number as an integer [0, 15].
constexpr bool operator==(MIDIChannelCable rhs) const
Check for equality: two addresses are equal if and only if they are both valid addresses and the MIDI...
constexpr Channel getChannel() const
Get the channel [1, 16].
constexpr MIDIChannelCable()
constexpr bool operator!=(MIDIChannelCable rhs) const
Check for inequality: two addresses are not equal if and only if they are both valid addresses and ha...
constexpr MIDIChannelCable(Channel channel, Cable cableNumber=Cable_1)
constexpr Cable getCableNumber() const
Get the cable number [Cable_1, Cable_16].
static bool matchSingle(MIDIChannelCable toMatch, MIDIChannelCable base)
Check if two addresses match (are equal).
A class for saving an offset to a MIDI address.
constexpr bool isValid() const
constexpr RelativeMIDIAddress(int deltaAddress, int deltaChannel=0, int deltaCableNumber=0)
RawMIDIAddress addresses
constexpr RelativeMIDIAddress()
RelativeMIDIAddress & operator+=(RelativeMIDIAddress that)
Compound addition.