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
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
33 ProtectedBuilder acquirePacket();
36 void releasePacketAndNotify(ProtectedBuilder &lck);
37
39 void sendNow(ProtectedBuilder &lck);
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.
void sendData(BLEDataView)=delete
Actually perform the BLE notification with the given data.
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.
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
ThreadedBLEMIDISender(const ThreadedBLEMIDISender &)=delete
ProtectedBuilder acquirePacket()
Acquire exclusive access to the buffer to be sent by the timer.
struct ThreadedBLEMIDISender::@361307330222120137105303366211377223340036235154 shared
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
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.
Non-owning, std::span-style read-only view of BLE data.
Definition BLEAPI.hpp:42