Control Surface master
MIDI Control Surface library for Arduino
BLEMIDIPacketBuilder.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <AH/STL/vector>
6
8
11 private:
12 uint8_t runningHeader = 0;
13 uint8_t runningTimestamp = 0;
14 std::vector<uint8_t> buffer = std::vector<uint8_t>(0);
15
16 constexpr static const uint8_t SysExStart =
17 static_cast<uint8_t>(MIDIMessageType::SYSEX_START);
18 constexpr static const uint8_t SysExEnd =
19 static_cast<uint8_t>(MIDIMessageType::SYSEX_END);
20
22 bool hasSpaceFor(size_t bytes) const {
23 return bytes <= size_t(buffer.capacity() - buffer.size());
24 }
25
27 constexpr static uint8_t getTimestampMSB(uint16_t timestamp) {
28 return ((timestamp >> 7) & 0x3F) | 0x80;
29 }
31 constexpr static uint8_t getTimestampLSB(uint16_t timestamp) {
32 return timestamp | 0x80;
33 }
34
37 void initBuffer(uint16_t timestamp) {
38 if (buffer.empty())
39 buffer.push_back(getTimestampMSB(timestamp));
40 }
41
64 template <bool ThreeBytes>
65 bool addImpl(uint8_t header, uint8_t data1, uint8_t data2,
66 uint16_t timestamp);
67
68 public:
69 BLEMIDIPacketBuilder(size_t capacity = 20) { buffer.reserve(capacity); }
70
72 void reset();
73
76 void setCapacity(uint16_t capacity);
77
79 uint16_t getSize() const { return buffer.size(); }
81 const uint8_t *getBuffer() const { return buffer.data(); }
83 bool empty() const { return buffer.empty(); }
84
86 const std::vector<uint8_t> &getPacket() const { return buffer; }
87
106 bool add3B(uint8_t header, uint8_t data1, uint8_t data2,
107 uint16_t timestamp);
108
125 bool add2B(uint8_t header, uint8_t data1, uint16_t timestamp);
126
141 bool addRealTime(uint8_t rt, uint16_t timestamp);
142
163 bool addSysCommon(uint8_t num_data, uint8_t header, uint8_t data1,
164 uint8_t data2, uint16_t timestamp);
165
210 bool addSysEx(const uint8_t *&data, size_t &length, uint16_t timestamp);
211
233 void continueSysEx(const uint8_t *&data, size_t &length,
234 uint16_t timestamp);
235};
236
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Class for building MIDI over Bluetooth Low Energy packets.
const uint8_t * getBuffer() const
Get a pointer to the packet data buffer.
constexpr static uint8_t getTimestampMSB(uint16_t timestamp)
Timestamp[0]: 0b10hh hhhh.
std::vector< uint8_t > buffer
uint16_t getSize() const
Get the size of the current packet.
void setCapacity(uint16_t capacity)
Set the maximum capacity of the buffer.
void continueSysEx(const uint8_t *&data, size_t &length, uint16_t timestamp)
Add a SysEx continuation to the packet.
constexpr static const uint8_t SysExEnd
bool addSysEx(const uint8_t *&data, size_t &length, uint16_t timestamp)
Try adding (part of) a SysEx message to the packet.
bool hasSpaceFor(size_t bytes) const
Check if the buffer has space left.
BLEMIDIPacketBuilder(size_t capacity=20)
bool empty() const
Check if the packet buffer is empty.
constexpr static uint8_t getTimestampLSB(uint16_t timestamp)
Timestamp[1]: 0b1lll llll.
bool add2B(uint8_t header, uint8_t data1, uint16_t timestamp)
Try adding a 2-byte MIDI channel voice message to the packet.
bool addSysCommon(uint8_t num_data, uint8_t header, uint8_t data1, uint8_t data2, uint16_t timestamp)
Try adding a MIDI system common message to the packet.
bool addRealTime(uint8_t rt, uint16_t timestamp)
Try adding a MIDI real-time message to the packet.
constexpr static const uint8_t SysExStart
const std::vector< uint8_t > & getPacket() const
Return the packet as a vector of bytes.
void reset()
Reset the builder to start a new packet.
bool addImpl(uint8_t header, uint8_t data1, uint8_t data2, uint16_t timestamp)
Try adding a 2-byte or 3-byte MIDI channel voice message to the packet.
bool add3B(uint8_t header, uint8_t data1, uint8_t data2, uint16_t timestamp)
Try adding a 3-byte MIDI channel voice message to the packet.
void initBuffer(uint16_t timestamp)
If this is the first byte/message in the packet, add the header containing the 6 most significant bit...