Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
BulkTX.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4
6#include <Settings/NamespaceSettings.hpp>
7
9
11template <class Derived, class MessageTypeT, uint16_t MaxPacketSizeV>
12struct BulkTX {
13 public:
14 using MessageType = MessageTypeT;
15
20 void write(MessageType msg);
21
28 void write(const MessageType *msgs, uint32_t num_msgs);
29
31 template <size_t N>
32 void write(const MessageType (&msgs)[N]) {
33 write(msgs, N);
34 }
35
43 uint32_t write_nonblock(const MessageType *msgs, uint32_t num_msgs);
44
48 void send_now();
49
51 bool is_done() const;
52
54 uint32_t getWriteError() const { return writing.error.load(mo_rlx); }
56 uint32_t clearWriteError() { return writing.error.exchange(0, mo_rlx); }
57
58 protected:
59 void reset(uint16_t packet_size = MaxPacketSize);
61
62 private:
63 static constexpr uint16_t MaxPacketSize = MaxPacketSizeV;
64 static constexpr uint16_t SizeReserved = MaxPacketSize + 1;
65
66 protected:
67 // Derived should implement the following methods:
68
71 void start_timeout() = delete;
73 void cancel_timeout() = delete;
75 void tx_start(const void *data, uint32_t size) = delete;
77 void tx_start_timeout(const void *data, uint32_t size) = delete;
79 void tx_start_isr(const void *data, uint32_t size) = delete;
80
81 private:
82 constexpr static std::memory_order mo_seq = std::memory_order_seq_cst;
83 constexpr static std::memory_order mo_rel = std::memory_order_release;
84 constexpr static std::memory_order mo_acq = std::memory_order_acquire;
85 constexpr static std::memory_order mo_rlx = std::memory_order_relaxed;
86 constexpr static std::memory_order mo_acq_rel = std::memory_order_acq_rel;
87
101 using wbuffer_t = typename Writing::Buffer;
102 bool disconnected = false;
103
104 uint32_t index_of(wbuffer_t *p) const { return p - writing.buffers; }
106 return &writing.buffers[!index_of(p)];
107 }
108 uint32_t write_impl(const MessageType *msgs, uint32_t num_msgs);
109
110 protected:
113};
114
116
117#include "BulkTX.ipp"
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Wrapper that provides atomic access to variables shared between the main program and interrupt handle...
Definition Atomic.hpp:81
T load(std::memory_order o) const
Definition Atomic.hpp:124
T exchange(T arg, std::memory_order o)
Definition Atomic.hpp:140
uint8_t buffer[MaxPacketSize]
Definition BulkTX.hpp:92
State for writing outgoing USB-MIDI data.
Definition BulkTX.hpp:89
interrupt_atomic< Buffer * > send_later
Definition BulkTX.hpp:96
interrupt_atomic< Buffer * > active_writebuffer
Definition BulkTX.hpp:94
interrupt_atomic< uint32_t > error
Definition BulkTX.hpp:98
interrupt_atomic< Buffer * > send_now
Definition BulkTX.hpp:97
uint16_t packet_size
Definition BulkTX.hpp:99
interrupt_atomic< Buffer * > sending
Definition BulkTX.hpp:95
struct BulkTX::Writing::Buffer buffers[2]
Sends Bulk packets (IN for device mode, OUT for host mode)
Definition BulkTX.hpp:12
void cancel_timeout()=delete
Cancel the timeout started by timeout_callback().
static constexpr std::memory_order mo_seq
Definition BulkTX.hpp:82
void tx_callback()
uint32_t index_of(wbuffer_t *p) const
Definition BulkTX.hpp:104
static constexpr std::memory_order mo_acq
Definition BulkTX.hpp:84
static constexpr std::memory_order mo_rlx
Definition BulkTX.hpp:85
void start_timeout()=delete
Start a timeout (e.g.
uint32_t write_nonblock(const MessageType *msgs, uint32_t num_msgs)
Send multiple MIDI USB messages without blocking.
MessageTypeT MessageType
Definition BulkTX.hpp:14
void timeout_callback()
void tx_start_isr(const void *data, uint32_t size)=delete
Start a USB transfer (from the USB interrupt handler).
void send_now()
Try sending the buffered data now.
static constexpr std::memory_order mo_acq_rel
Definition BulkTX.hpp:86
wbuffer_t * other_buf(wbuffer_t *p)
Definition BulkTX.hpp:105
bool wait_connect()
uint32_t getWriteError() const
Get the number messages that failed to send.
Definition BulkTX.hpp:54
bool is_done() const
Check if all transfers have completed.
void write(const MessageType(&msgs)[N])
Send multiple MIDI USB messages. May block.
Definition BulkTX.hpp:32
struct BulkTX::Writing writing
static constexpr uint16_t MaxPacketSize
Definition BulkTX.hpp:63
void reset(uint16_t packet_size=MaxPacketSize)
typename Writing::Buffer wbuffer_t
Definition BulkTX.hpp:101
void tx_start(const void *data, uint32_t size)=delete
Start a USB transfer (from the main program).
static constexpr uint16_t SizeReserved
Definition BulkTX.hpp:64
void write(const MessageType *msgs, uint32_t num_msgs)
Send multiple MIDI USB messages.
void write(MessageType msg)
Send a MIDI USB message.
static constexpr std::memory_order mo_rel
Definition BulkTX.hpp:83
void tx_start_timeout(const void *data, uint32_t size)=delete
Start a USB transfer (from the timeout callback).
bool disconnected
Definition BulkTX.hpp:102
uint32_t write_impl(const MessageType *msgs, uint32_t num_msgs)
uint32_t clearWriteError()
Get and clear the number messages that failed to send.
Definition BulkTX.hpp:56