Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
ArduinoBLEBackend.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <AH/Error/Error.hpp>
4
5#include "ArduinoBLE/midi.hpp"
6#include "BLEAPI.hpp"
9
11
14class ArduinoBLEBackend : private PollingBLEMIDISender<ArduinoBLEBackend>,
15 private MIDIBLEInstance {
16 private:
17 // Callbacks from the ArduinoBLE stack.
18 void handleConnect(BLEConnectionHandle) override { connected = true; }
20 connected = subscribed = false;
21 }
22 void handleMTU(BLEConnectionHandle, uint16_t mtu) override {
24 }
26 bool notify) override {
27 subscribed = notify;
28 }
30 BLEDataLifetime) override {
31 while (true) {
32 BLEDataView packet = data();
33 if (packet.length == 0) {
34 break;
35 } else if (!parser.pushPacket(packet)) {
36 DEBUGREF(F("BLE packet dropped, size: ") << packet.length);
37 break;
38 }
39 }
40 }
41
42 private:
44 bool connected = false;
46 bool subscribed = false;
49
50 public:
52
54 bool popMessage(IncomingMIDIMessage &incomingMessage) {
55 // This function is assumed to be polled regularly by the higher-level
56 // MIDI_Interface, so we check the sender's timer here, and we poll
57 // the ArduinoBLE library.
58 auto lck = Sender::acquirePacket();
61 // Actually get a MIDI message from the buffer
62 return parser.popMessage(incomingMessage);
63 }
64
65 public:
67 void begin(BLESettings ble_settings) {
68 arduino_ble_midi::init(*this, ble_settings);
70 }
73 void end() {}
75 bool isConnected() const { return connected; }
76
77 private:
78 // Implement the interface for the BLE sender.
80 friend Sender;
82 void sendData(BLEDataView data) {
83 if (connected && subscribed)
85 }
86
87 public:
88 // Expose the necessary BLE sender functions.
93 using Sender::sendNow;
95};
96
Type definitions and callback interfaces for communication between the low-level BLE stacks and highe...
BLEDataLifetime
Should a buffer of BLEData be consumed immediately inside of the callback, or can we hold on to it an...
Definition BLEAPI.hpp:113
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
ArduinoBLE backend intended to be plugged into GenericBLEMIDI_Interface.
void handleSubscribe(BLEConnectionHandle, BLECharacteristicHandle, bool notify) override
Called by the BLE stack when the central subscribes to receive notifications for the MIDI GATT charac...
void handleMTU(BLEConnectionHandle, uint16_t mtu) override
Called by the BLE stack when the maximum transmission unit for the connection changes.
void handleConnect(BLEConnectionHandle) override
Called by the BLE stack when a connection is established.
void sendData(BLEDataView data)
Send the given MIDI BLE packet.
void handleData(BLEConnectionHandle, BLEDataGenerator &&data, BLEDataLifetime) override
Called by the BLE stack when the central writes data to the MIDI GATT characteristic.
void begin(BLESettings ble_settings)
Initialize the BLE stack etc.
bool isConnected() const
Returns true if we are connected to a BLE Central device.
bool subscribed
Did the BLE Central subscribe to be notified for the MIDI characteristic?
void end()
Deinitialize the BLE stack.
bool connected
Are we connected to a BLE Central?
bool popMessage(IncomingMIDIMessage &incomingMessage)
Retrieve and remove a single incoming MIDI message from the buffer.
void handleDisconnect(BLEConnectionHandle) override
Called by the BLE stack when a connection is terminated.
BufferedBLEMIDIParser< 1024 > parser
Contains incoming BLE MIDI data to be parsed.
Callable that returns the next chunk of data from a BLE packet when called.
Definition BLEAPI.hpp:66
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.
bool popMessage(IncomingMIDIMessage &incomingMessage)
Retrieve and remove a single incoming MIDI message from the buffer.
Defines the interface for callback functions registered by the low-level BLE code.
Definition BLEAPI.hpp:127
Class that buffers MIDI BLE packets.
BLEMIDIPacketBuilder packet
View of the data to send.
uint16_t getMinMTU() const
Get the minimum MTU of all connected clients.
void setTimeout(std::chrono::milliseconds timeout)
Set the timeout, the number of milliseconds to buffer the outgoing MIDI messages.
void forceMinMTU(uint16_t mtu)
Force the MTU to an artificially small value (used for testing).
ProtectedBuilder acquirePacket()
Acquire exclusive access to the buffer.
void releasePacketAndNotify(ProtectedBuilder &lck)
Release exclusive access to the buffer and notify the sender thread that data is available.
void updateMTU(uint16_t mtu)
Set the maximum transmission unit of the Bluetooth link.
void sendNow(ProtectedBuilder &lck)
Sends the data immediately without waiting for the timeout.
#define DEBUGREF(x)
Print an expression and its location (file and line number) to the debug output if debugging is enabl...
Definition Debug.hpp:105
void notify(BLEDataView data)
bool init(MIDIBLEInstance &instance, BLESettings ble_settings)
MIDI message variant type (with timestamp).
Represents a handle to a local GATT characteristic.
Definition BLEAPI.hpp:30
Represents a handle to the connection to another device.
Definition BLEAPI.hpp:19
Non-owning, std::span-style read-only view of BLE data.
Definition BLEAPI.hpp:42
Configuration options for the low-level BLE code.
Definition BLEAPI.hpp:150