#include <MIDI_Interfaces/BLEMIDI/BLEMIDIPacketBuilder.hpp>
Class for building MIDI over Bluetooth Low Energy packets.
Definition at line 10 of file BLEMIDIPacketBuilder.hpp.
Public Member Functions | |
BLEMIDIPacketBuilder (size_t capacity=20) | |
void | reset () |
Reset the builder to start a new packet. | |
void | setCapacity (uint16_t capacity) |
Set the maximum capacity of the buffer. | |
uint16_t | getSize () const |
Get the size of the current packet. | |
const uint8_t * | getBuffer () const |
Get a pointer to the packet data buffer. | |
bool | empty () const |
Check if the packet buffer is empty. | |
const std::vector< uint8_t > & | getPacket () const |
Return the packet as a vector of bytes. | |
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. | |
bool | add2B (uint8_t header, uint8_t data1, uint16_t timestamp) |
Try adding a 2-byte MIDI channel voice message to the packet. | |
bool | addRealTime (uint8_t rt, uint16_t timestamp) |
Try adding a MIDI real-time 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 | addSysEx (const uint8_t *&data, size_t &length, uint16_t timestamp) |
Try adding (part of) a SysEx message to the packet. | |
void | continueSysEx (const uint8_t *&data, size_t &length, uint16_t timestamp) |
Add a SysEx continuation to the packet. | |
Private Member Functions | |
bool | hasSpaceFor (size_t bytes) const |
Check if the buffer has space left. | |
void | initBuffer (uint16_t timestamp) |
If this is the first byte/message in the packet, add the header containing the 6 most significant bits of the timestamp. | |
template<bool ThreeBytes> | |
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. | |
Static Private Member Functions | |
static constexpr uint8_t | getTimestampMSB (uint16_t timestamp) |
Timestamp[0]: 0b10hh hhhh. | |
static constexpr uint8_t | getTimestampLSB (uint16_t timestamp) |
Timestamp[1]: 0b1lll llll. | |
Private Attributes | |
uint8_t | runningHeader = 0 |
uint8_t | runningTimestamp = 0 |
std::vector< uint8_t > | buffer = std::vector<uint8_t>(0) |
Static Private Attributes | |
static constexpr const uint8_t | SysExStart |
static constexpr const uint8_t | SysExEnd |
|
inline |
Definition at line 69 of file BLEMIDIPacketBuilder.hpp.
Check if the buffer has space left.
Definition at line 22 of file BLEMIDIPacketBuilder.hpp.
Timestamp[0]: 0b10hh hhhh.
Definition at line 27 of file BLEMIDIPacketBuilder.hpp.
Timestamp[1]: 0b1lll llll.
Definition at line 31 of file BLEMIDIPacketBuilder.hpp.
If this is the first byte/message in the packet, add the header containing the 6 most significant bits of the timestamp.
Definition at line 37 of file BLEMIDIPacketBuilder.hpp.
|
private |
Try adding a 2-byte or 3-byte MIDI channel voice message to the packet.
ThreeBytes | Set to true for a 3-byte message, false for a 2-byte message. |
header | MIDI status byte. |
data1 | MIDI data byte 1. |
data2 | MIDI data byte 2 (if ThreeBytes == true ). |
timestamp | 13-bit BLE-MIDI timestamp. |
true | Successfully added message to the packet. |
false | Buffer is too full, send the current packet, reset the packet builder, and try again. |
Definition at line 6 of file BLEMIDIPacketBuilder.cpp.
void reset | ( | ) |
Reset the builder to start a new packet.
Definition at line 48 of file BLEMIDIPacketBuilder.cpp.
Set the maximum capacity of the buffer.
Set this to the MTU of the BLE link minus three bytes (for notify overhead).
Definition at line 53 of file BLEMIDIPacketBuilder.cpp.
|
inline |
Get the size of the current packet.
Definition at line 79 of file BLEMIDIPacketBuilder.hpp.
Get a pointer to the packet data buffer.
Definition at line 81 of file BLEMIDIPacketBuilder.hpp.
|
inline |
Check if the packet buffer is empty.
Definition at line 83 of file BLEMIDIPacketBuilder.hpp.
Return the packet as a vector of bytes.
Definition at line 86 of file BLEMIDIPacketBuilder.hpp.
Try adding a 3-byte MIDI channel voice message to the packet.
header | MIDI status byte. |
data1 | MIDI data byte 1. |
data2 | MIDI data byte 2. |
timestamp | 13-bit BLE-MIDI timestamp. |
true | Successfully added message to the packet. |
false | Buffer is too full, send the current packet, reset the packet builder, and try again. |
Definition at line 60 of file BLEMIDIPacketBuilder.cpp.
Try adding a 2-byte MIDI channel voice message to the packet.
header | MIDI status byte. |
data1 | MIDI data byte 1. |
timestamp | 13-bit BLE-MIDI timestamp. |
true | Successfully added message to the packet. |
false | Buffer is too full, send the current packet, reset the packet builder, and try again. |
Definition at line 66 of file BLEMIDIPacketBuilder.cpp.
Try adding a MIDI real-time message to the packet.
rt | MIDI real-time byte. |
timestamp | 13-bit BLE-MIDI timestamp. |
true | Successfully added message to the packet. |
false | Buffer is too full, send the current packet, reset the packet builder, and try again. |
Definition at line 72 of file BLEMIDIPacketBuilder.cpp.
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.
num_data | The number of data bytes (0, 1 or 2). |
header | System common status byte. |
data1 | MIDI data byte 1. |
data2 | MIDI data byte 2. |
timestamp | 13-bit BLE-MIDI timestamp. |
true | Successfully added message to the packet. |
false | Buffer is too full, send the current packet, reset the packet builder, and try again. |
Definition at line 85 of file BLEMIDIPacketBuilder.cpp.
Try adding (part of) a SysEx message to the packet.
[in,out] | data | Pointer to the first byte of the SysEx message. At the end, this will point to the first byte to send in the next packet, or nullptr if the message was finished. |
[in,out] | length | The number of bytes in the SysEx message. At the end, this will be set to remaining number of bytes to send in the next packet. |
[in] | timestamp | 13-bit BLE-MIDI timestamp. |
true | Successfully added (part of) the message to the packet. |
false | Buffer is too full, send the current packet, reset the packet builder, and try again. |
If the message fits in a single packet, length
is set to 0
(no remaining data bytes) and data
is set to nullptr
.
For example:
Definition at line 105 of file BLEMIDIPacketBuilder.cpp.
Add a SysEx continuation to the packet.
[in,out] | data | Pointer to the first byte of the SysEx message to send in this continuation packet. At the end, this will point to the first byte to send in the next packet, or nullptr if the message was finished. |
[in,out] | length | The number of remaining bytes in the SysEx message. At the end, this will be set to remaining number of bytes to send in the next packet. |
[in] | timestamp | 13-bit BLE-MIDI timestamp. |
If the message can be completed in a single packet, length
is set to 0
(no remaining data bytes) and data
is set to nullptr
.
Definition at line 137 of file BLEMIDIPacketBuilder.cpp.
|
private |
Definition at line 12 of file BLEMIDIPacketBuilder.hpp.
|
private |
Definition at line 13 of file BLEMIDIPacketBuilder.hpp.
Definition at line 14 of file BLEMIDIPacketBuilder.hpp.
Definition at line 16 of file BLEMIDIPacketBuilder.hpp.
Definition at line 18 of file BLEMIDIPacketBuilder.hpp.