Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
ThreadedBLEMIDISender.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Settings/NamespaceSettings.hpp>
4
5#include <chrono>
6#include <condition_variable>
7#include <mutex>
8#include <thread>
9
10#include "BLEAPI.hpp"
12
14
16template <class Derived>
18 public:
23
25 void begin();
26
27 struct ProtectedBuilder;
28
37
40
43 void updateMTU(uint16_t mtu);
45 uint16_t getMinMTU() const { return min_mtu; }
47 void forceMinMTU(uint16_t mtu);
48
51 void setTimeout(std::chrono::milliseconds timeout);
52
53 private:
55 void sendData(BLEDataView) = delete; // should be implemented by subclass
56
62
63 private:
64 struct {
68 bool stop = false;
70 bool flush = false;
73 std::chrono::milliseconds timeout {10};
75 std::mutex mtx;
76 } shared {};
80 std::condition_variable cv;
82 using lock_t = std::unique_lock<std::mutex>;
84 std::thread send_thread;
85
86 private:
88 std::atomic_uint_fast16_t min_mtu {23};
92 std::atomic_uint_fast16_t force_min_mtu {515};
93
94 public:
99};
100
102
103#include "ThreadedBLEMIDISender.ipp"
Type definitions and callback interfaces for communication between the low-level BLE stacks and highe...
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Class for building MIDI over Bluetooth Low Energy packets.
Class that manages a background thread that sends BLE packets asynchronously.
BLEMIDIPacketBuilder packet
View of the data to send.
void sendData(BLEDataView)=delete
Actually perform the BLE notification with the given data.
std::condition_variable cv
Condition variable used by the background sender thread to wait for data to send, and for the main th...
uint16_t getMinMTU() const
Get the minimum MTU of all connected clients.
std::unique_lock< std::mutex > lock_t
Lock type used to lock the mutex.
struct ThreadedBLEMIDISender::@5 shared
std::chrono::milliseconds timeout
Timeout before the sender thread sends a packet.
void setTimeout(std::chrono::milliseconds timeout)
Set the timeout, the number of milliseconds to buffer the outgoing MIDI messages.
bool handleSendEvents()
Function that waits for BLE packets and sends them in the background.
void forceMinMTU(uint16_t mtu)
Force the MTU to an artificially small value (used for testing).
ThreadedBLEMIDISender & operator=(const ThreadedBLEMIDISender &)=delete
bool stop
Flag to stop the background thread.
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 ...
ThreadedBLEMIDISender(const ThreadedBLEMIDISender &)=delete
ProtectedBuilder acquirePacket()
Acquire exclusive access to the buffer to be sent by the timer.
void begin()
Start the background thread.
void releasePacketAndNotify(ProtectedBuilder &lck)
Release exclusive access to the buffer and notify the sender thread that data is available.
ThreadedBLEMIDISender()=default
std::thread send_thread
The background thread responsible for sending the data.
std::mutex mtx
Lock to protect all shared data in this struct.
void updateMTU(uint16_t mtu)
Set the maximum transmission unit of the Bluetooth link.
std::atomic_uint_fast16_t min_mtu
The minimum MTU of all connected clients.
void sendNow(ProtectedBuilder &lck)
Sends the data immediately without waiting for the timeout.
Non-owning, std::span-style read-only view of BLE data.
Definition BLEAPI.hpp:42