Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
ESP32BLEBackend< Impl > Class Template Reference

#include <MIDI_Interfaces/BLEMIDI/ESP32Backend.hpp>

Detailed Description

template<class Impl>
class ESP32BLEBackend< Impl >

ESP32 backend intended to be plugged into GenericBLEMIDI_Interface.

Impl can be used to select different low-level BLE stacks.

Definition at line 23 of file ESP32Backend.hpp.

+ Inheritance diagram for ESP32BLEBackend< Impl >:
+ Collaboration diagram for ESP32BLEBackend< Impl >:

Classes

struct  AtomicSize
 

Public Types

using IncomingMIDIMessage = AnyMIDIMessage
 

Public Member Functions

bool popMessage (IncomingMIDIMessage &incomingMessage)
 
void begin (BLESettings ble_settings)
 
void end ()
 
bool isConnected () const
 
ProtectedBuilder acquirePacket ()
 Acquire exclusive access to the buffer to be sent by the timer.
 
void forceMinMTU (uint16_t mtu)
 Force the MTU to an artificially small value (used for testing).
 
uint16_t getMinMTU () const
 Get the minimum MTU of all connected clients.
 
void releasePacketAndNotify (ProtectedBuilder &lck)
 Release exclusive access to the buffer and notify the sender thread that data is available.
 
void sendNow (ProtectedBuilder &lck)
 Sends the data immediately without waiting for the timeout.
 
void setTimeout (std::chrono::milliseconds timeout)
 Set the timeout, the number of milliseconds to buffer the outgoing MIDI messages.
 

Protected Types

using Sender = ThreadedBLEMIDISender<ESP32BLEBackend>
 

Protected Member Functions

void sendData (BLEDataView data)
 
void handleConnect (BLEConnectionHandle conn_handle) override
 Called by the BLE stack when a connection is established.
 
void handleDisconnect (BLEConnectionHandle conn_handle) override
 Called by the BLE stack when a connection is terminated.
 
void handleMTU (BLEConnectionHandle conn_handle, uint16_t mtu) override
 Called by the BLE stack when the maximum transmission unit for the connection changes.
 
void handleSubscribe (BLEConnectionHandle conn_handle, BLECharacteristicHandle char_handle, bool notify) override
 Called by the BLE stack when the central subscribes to receive notifications for the MIDI GATT characteristic.
 
void handleData (BLEConnectionHandle conn_handle, BLEDataGenerator &&data, BLEDataLifetime) override
 Called by the BLE stack when the central writes data to the MIDI GATT characteristic.
 

Protected Attributes

Impl impl
 
friend Sender
 
std::atomic< BLEConnectionHandleconnection
 
std::atomic< BLECharacteristicHandlecharacteristic
 

Private Types

using lock_t
 Lock type used to lock the mutex.
 

Private Member Functions

void begin ()
 Start the background thread.
 
void updateMTU (uint16_t mtu)
 Set the maximum transmission unit of the Bluetooth link.
 
bool handleSendEvents ()
 Function that waits for BLE packets and sends them in the background.
 

Private Attributes

BufferedBLEMIDIParser< 4096, AtomicSizeparser
 Contains incoming BLE MIDI data to be parsed.
 
BLEMIDIPacketBuilder packet
 View of the data to send.
 
bool stop
 Flag to stop the background thread.
 
bool flush
 Flag to tell the sender thread to send the packet immediately.
 
std::chrono::milliseconds timeout
 Timeout before the sender thread sends a packet.
 
std::mutex mtx
 Lock to protect all shared data in this struct.
 
struct { 
 
   BLEMIDIPacketBuilder   packet 
 View of the data to send. More...
 
   bool   stop = false 
 Flag to stop the background thread. More...
 
   bool   flush = false 
 Flag to tell the sender thread to send the packet immediately. More...
 
   std::chrono::milliseconds   timeout {10} 
 Timeout before the sender thread sends a packet. More...
 
   std::mutex   mtx 
 Lock to protect all shared data in this struct. More...
 
shared 
 
std::condition_variable cv
 Condition variable used by the background sender thread to wait for data to send, and for the main thread to wait for the data to be flushed by the sender thread.
 
std::thread send_thread
 The background thread responsible for sending the data.
 
std::atomic_uint_fast16_t min_mtu
 The minimum MTU of all connected clients.
 
std::atomic_uint_fast16_t force_min_mtu
 Override the minimum MTU (0 means don't override, nonzero overrides if it's smaller than the minimum MTU of the clients).
 

Member Typedef Documentation

◆ Sender

template<class Impl >
using Sender = ThreadedBLEMIDISender<ESP32BLEBackend>
protected

Definition at line 27 of file ESP32Backend.hpp.

◆ IncomingMIDIMessage

template<class Impl >
using IncomingMIDIMessage = AnyMIDIMessage

Definition at line 114 of file ESP32Backend.hpp.

◆ lock_t

using lock_t
privateinherited

Lock type used to lock the mutex.

Definition at line 82 of file ThreadedBLEMIDISender.hpp.

Member Function Documentation

◆ sendData()

template<class Impl >
void sendData ( BLEDataView data)
inlineprotected

Definition at line 29 of file ESP32Backend.hpp.

◆ handleConnect()

template<class Impl >
void handleConnect ( BLEConnectionHandle conn_handle)
inlineoverrideprotectedvirtual

Called by the BLE stack when a connection is established.

Implements MIDIBLEInstance.

Definition at line 42 of file ESP32Backend.hpp.

◆ handleDisconnect()

template<class Impl >
void handleDisconnect ( BLEConnectionHandle conn_handle)
inlineoverrideprotectedvirtual

Called by the BLE stack when a connection is terminated.

Implements MIDIBLEInstance.

Definition at line 46 of file ESP32Backend.hpp.

◆ handleMTU()

template<class Impl >
void handleMTU ( BLEConnectionHandle conn_handle,
uint16_t mtu )
inlineoverrideprotectedvirtual

Called by the BLE stack when the maximum transmission unit for the connection changes.

Implements MIDIBLEInstance.

Definition at line 52 of file ESP32Backend.hpp.

◆ handleSubscribe()

template<class Impl >
void handleSubscribe ( BLEConnectionHandle conn_handle,
BLECharacteristicHandle char_handle,
bool notify )
inlineoverrideprotectedvirtual

Called by the BLE stack when the central subscribes to receive notifications for the MIDI GATT characteristic.

Implements MIDIBLEInstance.

Definition at line 57 of file ESP32Backend.hpp.

◆ handleData()

template<class Impl >
void handleData ( BLEConnectionHandle conn_handle,
BLEDataGenerator && data,
BLEDataLifetime lifetime )
inlineoverrideprotectedvirtual

Called by the BLE stack when the central writes data to the MIDI GATT characteristic.

Implements MIDIBLEInstance.

Definition at line 69 of file ESP32Backend.hpp.

◆ popMessage()

template<class Impl >
bool popMessage ( IncomingMIDIMessage & incomingMessage)
inline

Definition at line 115 of file ESP32Backend.hpp.

◆ begin() [1/2]

template<class Impl >
void begin ( BLESettings ble_settings)
inline

Definition at line 120 of file ESP32Backend.hpp.

◆ end()

template<class Impl >
void end ( )
inline

Definition at line 126 of file ESP32Backend.hpp.

◆ isConnected()

template<class Impl >
bool isConnected ( ) const
inline

Definition at line 129 of file ESP32Backend.hpp.

◆ acquirePacket()

template<class Impl >
ProtectedBuilder acquirePacket ( )

Acquire exclusive access to the buffer to be sent by the timer.

Returns
A RAII wrapper that automatically releases the buffer upon destruction. Just make sure you don't keep any pointers to the packet member.

◆ forceMinMTU()

template<class Impl >
void forceMinMTU ( uint16_t mtu)

Force the MTU to an artificially small value (used for testing).

◆ getMinMTU()

template<class Impl >
uint16_t getMinMTU ( ) const
inline

Get the minimum MTU of all connected clients.

Definition at line 45 of file ThreadedBLEMIDISender.hpp.

◆ releasePacketAndNotify()

template<class Impl >
void releasePacketAndNotify ( ProtectedBuilder & lck)

Release exclusive access to the buffer and notify the sender thread that data is available.

◆ sendNow()

template<class Impl >
void sendNow ( ProtectedBuilder & lck)

Sends the data immediately without waiting for the timeout.

◆ setTimeout()

template<class Impl >
void setTimeout ( std::chrono::milliseconds timeout)

Set the timeout, the number of milliseconds to buffer the outgoing MIDI messages.

◆ begin() [2/2]

void begin ( )
inherited

Start the background thread.

◆ updateMTU()

void updateMTU ( uint16_t mtu)
inherited

Set the maximum transmission unit of the Bluetooth link.

Used to compute the MIDI BLE packet size.

◆ handleSendEvents()

bool handleSendEvents ( )
privateinherited

Function that waits for BLE packets and sends them in the background.

It either sends them after a timeout (a given number of milliseconds after the first data was added to the packet), or immediately when it receives a flush signal from the main thread.

Member Data Documentation

◆ impl

template<class Impl >
Impl impl
protected

Definition at line 26 of file ESP32Backend.hpp.

◆ Sender

template<class Impl >
friend Sender
protected

Definition at line 28 of file ESP32Backend.hpp.

◆ connection

template<class Impl >
std::atomic<BLEConnectionHandle> connection
protected

Definition at line 38 of file ESP32Backend.hpp.

◆ characteristic

template<class Impl >
std::atomic<BLECharacteristicHandle> characteristic
protected

Definition at line 39 of file ESP32Backend.hpp.

◆ parser

template<class Impl >
BufferedBLEMIDIParser<4096, AtomicSize> parser
private

Contains incoming BLE MIDI data to be parsed.

Definition at line 111 of file ESP32Backend.hpp.

◆ packet

BLEMIDIPacketBuilder packet
inherited

View of the data to send.

Definition at line 66 of file ThreadedBLEMIDISender.hpp.

◆ stop

bool stop
inherited

Flag to stop the background thread.

Definition at line 68 of file ThreadedBLEMIDISender.hpp.

◆ flush

bool flush
inherited

Flag to tell the sender thread to send the packet immediately.

Definition at line 70 of file ThreadedBLEMIDISender.hpp.

◆ timeout

std::chrono::milliseconds timeout
inherited

Timeout before the sender thread sends a packet.

See also
setTimeout()

Definition at line 73 of file ThreadedBLEMIDISender.hpp.

◆ mtx

std::mutex mtx
inherited

Lock to protect all shared data in this struct.

Definition at line 75 of file ThreadedBLEMIDISender.hpp.

◆ [struct]

struct { ... } shared

◆ cv

std::condition_variable cv
privateinherited

Condition variable used by the background sender thread to wait for data to send, and for the main thread to wait for the data to be flushed by the sender thread.

Definition at line 80 of file ThreadedBLEMIDISender.hpp.

◆ send_thread

std::thread send_thread
privateinherited

The background thread responsible for sending the data.

Definition at line 84 of file ThreadedBLEMIDISender.hpp.

◆ min_mtu

std::atomic_uint_fast16_t min_mtu
privateinherited

The minimum MTU of all connected clients.

Definition at line 88 of file ThreadedBLEMIDISender.hpp.

◆ force_min_mtu

std::atomic_uint_fast16_t force_min_mtu
privateinherited

Override the minimum MTU (0 means don't override, nonzero overrides if it's smaller than the minimum MTU of the clients).

See also
forceMinMTU()

Definition at line 92 of file ThreadedBLEMIDISender.hpp.


The documentation for this class was generated from the following file: