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

#include <MIDI_Interfaces/BLEMIDI/ThreadedBLEMIDISender.hpp>

Detailed Description

template<class Derived>
class ThreadedBLEMIDISender< Derived >

Class that manages a background thread that sends BLE packets asynchronously.

Definition at line 17 of file ThreadedBLEMIDISender.hpp.

+ Collaboration diagram for ThreadedBLEMIDISender< Derived >:

Classes

struct  ProtectedBuilder
 

Public Member Functions

 ThreadedBLEMIDISender ()=default
 
 ThreadedBLEMIDISender (const ThreadedBLEMIDISender &)=delete
 
ThreadedBLEMIDISenderoperator= (const ThreadedBLEMIDISender &)=delete
 
 ~ThreadedBLEMIDISender ()
 
void begin ()
 Start the background thread.
 
ProtectedBuilder acquirePacket ()
 Acquire exclusive access to the buffer to be sent by the timer.
 
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 updateMTU (uint16_t mtu)
 Set the maximum transmission unit of the Bluetooth link.
 
uint16_t getMinMTU () const
 Get the minimum MTU of all connected clients.
 
void forceMinMTU (uint16_t mtu)
 Force the MTU to an artificially small value (used for testing).
 
void setTimeout (std::chrono::milliseconds timeout)
 Set the timeout, the number of milliseconds to buffer the outgoing MIDI messages.
 

Private Types

using lock_t = std::unique_lock<std::mutex>
 Lock type used to lock the mutex.
 

Private Member Functions

void sendData (BLEDataView)=delete
 Actually perform the BLE notification with the given data.
 
bool handleSendEvents ()
 Function that waits for BLE packets and sends them in the background.
 

Private Attributes

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 {23}
 The minimum MTU of all connected clients.
 
std::atomic_uint_fast16_t force_min_mtu {515}
 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

◆ lock_t

template<class Derived >
using lock_t = std::unique_lock<std::mutex>
private

Lock type used to lock the mutex.

Definition at line 82 of file ThreadedBLEMIDISender.hpp.

Constructor & Destructor Documentation

◆ ThreadedBLEMIDISender() [1/2]

template<class Derived >
ThreadedBLEMIDISender ( )
default

◆ ThreadedBLEMIDISender() [2/2]

template<class Derived >
ThreadedBLEMIDISender ( const ThreadedBLEMIDISender< Derived > & )
delete

◆ ~ThreadedBLEMIDISender()

template<class Derived >
~ThreadedBLEMIDISender ( )

Member Function Documentation

◆ operator=()

template<class Derived >
ThreadedBLEMIDISender & operator= ( const ThreadedBLEMIDISender< Derived > & )
delete

◆ begin()

template<class Derived >
void begin ( )

Start the background thread.

◆ acquirePacket()

template<class Derived >
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.

◆ releasePacketAndNotify()

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

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

◆ sendNow()

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

Sends the data immediately without waiting for the timeout.

◆ updateMTU()

template<class Derived >
void updateMTU ( uint16_t mtu)

Set the maximum transmission unit of the Bluetooth link.

Used to compute the MIDI BLE packet size.

◆ getMinMTU()

template<class Derived >
uint16_t getMinMTU ( ) const
inline

Get the minimum MTU of all connected clients.

Definition at line 45 of file ThreadedBLEMIDISender.hpp.

◆ forceMinMTU()

template<class Derived >
void forceMinMTU ( uint16_t mtu)

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

◆ setTimeout()

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

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

◆ sendData()

template<class Derived >
void sendData ( BLEDataView )
privatedelete

Actually perform the BLE notification with the given data.

◆ handleSendEvents()

template<class Derived >
bool handleSendEvents ( )
private

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

◆ packet

template<class Derived >
BLEMIDIPacketBuilder packet

View of the data to send.

Definition at line 66 of file ThreadedBLEMIDISender.hpp.

◆ stop

template<class Derived >
bool stop = false

Flag to stop the background thread.

Definition at line 68 of file ThreadedBLEMIDISender.hpp.

◆ flush

template<class Derived >
bool flush = false

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

Definition at line 70 of file ThreadedBLEMIDISender.hpp.

◆ timeout

template<class Derived >
std::chrono::milliseconds timeout {10}

Timeout before the sender thread sends a packet.

See also
setTimeout()

Definition at line 73 of file ThreadedBLEMIDISender.hpp.

◆ mtx

template<class Derived >
std::mutex mtx

Lock to protect all shared data in this struct.

Definition at line 75 of file ThreadedBLEMIDISender.hpp.

◆ [struct]

struct { ... } shared

◆ cv

template<class Derived >
std::condition_variable cv
private

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

template<class Derived >
std::thread send_thread
private

The background thread responsible for sending the data.

Definition at line 84 of file ThreadedBLEMIDISender.hpp.

◆ min_mtu

template<class Derived >
std::atomic_uint_fast16_t min_mtu {23}
private

The minimum MTU of all connected clients.

Definition at line 88 of file ThreadedBLEMIDISender.hpp.

◆ force_min_mtu

template<class Derived >
std::atomic_uint_fast16_t force_min_mtu {515}
private

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: