Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
BufferedBLEMIDIParser.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Settings/NamespaceSettings.hpp>
4
5#include "BLERingBuf.hpp"
9
11
15template <uint16_t Capacity, class SizeT = NonatomicBLERingBufSize<uint16_t>>
17 private:
24
25 public:
27
31 return ble_buffer.push(packet, type);
32 }
33
35 bool popMessage(IncomingMIDIMessage &incomingMessage) {
36 // Try reading a MIDI message from the parser
37 auto try_read = [&] {
39 switch (event) {
41 incomingMessage = {parser.getChannelMessage(),
43 return true;
44 case MIDIReadEvent::SYSEX_CHUNK: // fallthrough
46 incomingMessage = {parser.getSysExMessage(),
48 return true;
50 incomingMessage = {parser.getRealTimeMessage(),
52 return true;
54 incomingMessage = {parser.getSysCommonMessage(),
56 return true;
57 case MIDIReadEvent::NO_MESSAGE: return false;
58 default: break; // LCOV_EXCL_LINE
59 }
60 return false;
61 };
62 while (true) {
63 // Try reading a MIDI message from the current buffer
64 if (try_read())
65 return true; // success, incomingMessage updated
66 // Get the next chunk of the BLE packet (if available)
67 BLEDataView chunk;
68 auto popped = ble_buffer.pop(chunk);
69 if (popped == BLEDataType::None)
70 return false; // no more BLE data available
71 else if (popped == BLEDataType::Continuation)
72 ble_parser.extend(chunk.data, chunk.length); // same BLE packet
73 else if (popped == BLEDataType::Packet)
74 ble_parser = {chunk.data, chunk.length}; // new BLE packet
75 }
76 }
77};
78
BLEDataType
Describes a byte buffer containing (part of) a BLE packet.
Definition BLEAPI.hpp:58
@ None
No buffers available.
@ Continuation
Buffer contains a chunk of a BLE packet.
@ Packet
Buffer contains the start of a BLE packet.
MIDIReadEvent
Values returned by the MIDI reading functions.
@ CHANNEL_MESSAGE
A MIDI Channel message was received.
@ SYSEX_CHUNK
An incomplete System Exclusive message.
@ SYSCOMMON_MESSAGE
A MIDI System Common message was received.
@ NO_MESSAGE
No new messages were received.
@ SYSEX_MESSAGE
A MIDI System Exclusive message was received.
@ REALTIME_MESSAGE
A MIDI Real-Time message was received.
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Class for parsing BLE-MIDI packets.
void extend(const uint8_t *data, size_t length)
Extend the BLE packet with the given buffer.
uint16_t getTimestamp() const
Circular FIFO buffer for buffering BLE packet data.
FIFO buffer that you can push BLE packets into, and pop MIDI messages out of.
bool pushPacket(BLEDataView packet, BLEDataType type=BLEDataType::Packet)
Add a new BLE packet or chunk to the buffer.
BLEMIDIParser ble_parser
Parses the (chunked) BLE packet obtained from ble_buffer.
bool popMessage(IncomingMIDIMessage &incomingMessage)
Retrieve and remove a single incoming MIDI message from the buffer.
BLERingBuf< Capacity, SizeT > ble_buffer
Contains incoming data to be parsed.
SerialMIDI_Parser parser
Parser for MIDI data extracted from the BLE packet by ble_parser.
ChannelMessage getChannelMessage() const
Get the latest MIDI channel voice message.
RealTimeMessage getRealTimeMessage() const
Get the latest MIDI real-time message.
SysCommonMessage getSysCommonMessage() const
Get the latest MIDI system common message.
Parser for Serial MIDI streams (and BLE-MIDI).
MIDIReadEvent pull(BytePuller &&puller)
Parse one incoming MIDI message.
SysExMessage getSysExMessage() const
Get the latest SysEx message.
MIDI message variant type (with timestamp).
Non-owning, std::span-style read-only view of BLE data.
Definition BLEAPI.hpp:42
uint16_t length
Definition BLEAPI.hpp:44
const uint8_t * data
Definition BLEAPI.hpp:43