Control Surface
main
MIDI Control Surface library for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
MIDI_Interfaces
BLEMIDI
BufferedBLEMIDIParser.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <
Settings/NamespaceSettings.hpp
>
4
5
#include "
BLERingBuf.hpp
"
6
#include <
MIDI_Parsers/AnyMIDI_Message.hpp
>
7
#include <
MIDI_Parsers/BLEMIDIParser.hpp
>
8
#include <
MIDI_Parsers/SerialMIDI_Parser.hpp
>
9
10
BEGIN_CS_NAMESPACE
11
15
template
<u
int
16_t Capacity,
class
SizeT = NonatomicBLERingBufSize<u
int
16_t>>
16
class
BufferedBLEMIDIParser
{
17
private
:
19
BLERingBuf<Capacity, SizeT>
ble_buffer
{};
21
BLEMIDIParser
ble_parser
{
nullptr
, 0};
23
SerialMIDI_Parser
parser
{
false
};
24
25
public
:
26
using
IncomingMIDIMessage
=
AnyMIDIMessage
;
27
29
bool
pushPacket
(
BLEDataView
packet,
30
BLEDataType
type =
BLEDataType::Packet
) {
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 = [&] {
38
MIDIReadEvent
event
=
parser
.pull(
ble_parser
);
39
switch
(event) {
40
case
MIDIReadEvent::CHANNEL_MESSAGE
:
41
incomingMessage = {
parser
.getChannelMessage(),
42
ble_parser
.getTimestamp()};
43
return
true
;
44
case
MIDIReadEvent::SYSEX_CHUNK
:
// fallthrough
45
case
MIDIReadEvent::SYSEX_MESSAGE
:
46
incomingMessage = {
parser
.getSysExMessage(),
47
ble_parser
.getTimestamp()};
48
return
true
;
49
case
MIDIReadEvent::REALTIME_MESSAGE
:
50
incomingMessage = {
parser
.getRealTimeMessage(),
51
ble_parser
.getTimestamp()};
52
return
true
;
53
case
MIDIReadEvent::SYSCOMMON_MESSAGE
:
54
incomingMessage = {
parser
.getSysCommonMessage(),
55
ble_parser
.getTimestamp()};
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
79
END_CS_NAMESPACE
AnyMIDI_Message.hpp
BLEDataType
BLEDataType
Describes a byte buffer containing (part of) a BLE packet.
Definition
BLEAPI.hpp:58
BLEDataType::None
@ None
No buffers available.
Definition
BLEAPI.hpp:59
BLEDataType::Continuation
@ Continuation
Buffer contains a chunk of a BLE packet.
Definition
BLEAPI.hpp:61
BLEDataType::Packet
@ Packet
Buffer contains the start of a BLE packet.
Definition
BLEAPI.hpp:60
BLEMIDIParser.hpp
BLERingBuf.hpp
MIDIReadEvent
MIDIReadEvent
Values returned by the MIDI reading functions.
Definition
MIDIReadEvent.hpp:11
MIDIReadEvent::CHANNEL_MESSAGE
@ CHANNEL_MESSAGE
A MIDI Channel message was received.
Definition
MIDIReadEvent.hpp:13
MIDIReadEvent::SYSEX_CHUNK
@ SYSEX_CHUNK
An incomplete System Exclusive message.
Definition
MIDIReadEvent.hpp:16
MIDIReadEvent::SYSCOMMON_MESSAGE
@ SYSCOMMON_MESSAGE
A MIDI System Common message was received.
Definition
MIDIReadEvent.hpp:17
MIDIReadEvent::NO_MESSAGE
@ NO_MESSAGE
No new messages were received.
Definition
MIDIReadEvent.hpp:12
MIDIReadEvent::SYSEX_MESSAGE
@ SYSEX_MESSAGE
A MIDI System Exclusive message was received.
Definition
MIDIReadEvent.hpp:14
MIDIReadEvent::REALTIME_MESSAGE
@ REALTIME_MESSAGE
A MIDI Real-Time message was received.
Definition
MIDIReadEvent.hpp:15
SerialMIDI_Parser.hpp
NamespaceSettings.hpp
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:14
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:11
BLEMIDIParser
Class for parsing BLE-MIDI packets.
Definition
BLEMIDIParser.hpp:16
BLERingBuf
Circular FIFO buffer for buffering BLE packet data.
Definition
BLERingBuf.hpp:34
BufferedBLEMIDIParser
FIFO buffer that you can push BLE packets into, and pop MIDI messages out of.
Definition
BufferedBLEMIDIParser.hpp:16
BufferedBLEMIDIParser::pushPacket
bool pushPacket(BLEDataView packet, BLEDataType type=BLEDataType::Packet)
Add a new BLE packet or chunk to the buffer.
Definition
BufferedBLEMIDIParser.hpp:29
BufferedBLEMIDIParser::IncomingMIDIMessage
AnyMIDIMessage IncomingMIDIMessage
Definition
BufferedBLEMIDIParser.hpp:26
BufferedBLEMIDIParser::ble_parser
BLEMIDIParser ble_parser
Parses the (chunked) BLE packet obtained from ble_buffer.
Definition
BufferedBLEMIDIParser.hpp:21
BufferedBLEMIDIParser::popMessage
bool popMessage(IncomingMIDIMessage &incomingMessage)
Retrieve and remove a single incoming MIDI message from the buffer.
Definition
BufferedBLEMIDIParser.hpp:35
BufferedBLEMIDIParser::ble_buffer
BLERingBuf< Capacity, SizeT > ble_buffer
Contains incoming data to be parsed.
Definition
BufferedBLEMIDIParser.hpp:19
BufferedBLEMIDIParser::parser
SerialMIDI_Parser parser
Parser for MIDI data extracted from the BLE packet by ble_parser.
Definition
BufferedBLEMIDIParser.hpp:23
SerialMIDI_Parser
Parser for Serial MIDI streams (and BLE-MIDI).
Definition
SerialMIDI_Parser.hpp:13
AnyMIDIMessage
MIDI message variant type (with timestamp).
Definition
AnyMIDI_Message.hpp:10
BLEDataView
Non-owning, std::span-style read-only view of BLE data.
Definition
BLEAPI.hpp:42
BLEDataView::length
uint16_t length
Definition
BLEAPI.hpp:44
BLEDataView::data
const uint8_t * data
Definition
BLEAPI.hpp:43
Generated by
1.17.0