Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
TeensyHostMIDI.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <USBHost_t36.h>
4#include <atomic>
5#include <chrono>
6#include <tuple>
7
10#include <Settings/NamespaceSettings.hpp>
11
13
15template <uint16_t MaxPacketSize = 512>
17 : public USBDriver,
18 protected BulkRX<TeensyHostMIDI<MaxPacketSize>, uint32_t, MaxPacketSize>,
19 protected BulkTX<TeensyHostMIDI<MaxPacketSize>, uint32_t, MaxPacketSize> {
20 public:
21 TeensyHostMIDI(USBHost &) { init(); }
22
23 using microseconds = std::chrono::microseconds;
24
28 uint32_t read() {
29 uint32_t packet = 0;
30 read(packet);
31 return packet;
32 }
33
40
44 void setTimeout(microseconds timeout) { write_timeout_duration = timeout; }
48 }
49
50 bool connectedForWrite() const { return txpipe; }
51
52 protected:
53 bool claim(Device_t *device, int type, const uint8_t *descriptors,
54 uint32_t len) override;
55 void disconnect() override;
56
57 protected:
58 void timer_event(USBDriverTimer *whichtimer) override;
59
60 protected:
61 static void rx_callback(const Transfer_t *transfer);
62 static void tx_callback(const Transfer_t *transfer);
63
64 protected:
65 void write_start(uint8_t *buffer, uint32_t size);
66 void write_start_isr(uint8_t *buffer, uint32_t size);
67 uint32_t write_finish(const Transfer_t *transfer);
68
69 void read_start(uint8_t *buffer, uint32_t size);
70 void read_start_isr(uint8_t *buffer, uint32_t size);
71 uint32_t read_finish(const Transfer_t *transfer);
72
73 protected:
74 bool claim_if_midi(Device_t *device, int type, const uint8_t *descriptors,
75 uint32_t len);
76 void init();
77
78 private:
79 Pipe_t *rxpipe, *txpipe;
80 uint16_t rx_size, tx_size;
81 uint8_t rx_ep, tx_ep;
83 Pipe_t mypipes[3] __attribute__((aligned(32)));
84 Transfer_t mytransfers[7] __attribute__((aligned(32)));
85 strbuf_t mystring_bufs[1];
86
87 protected:
88 friend class BulkTX<TeensyHostMIDI, uint32_t, MaxPacketSize>;
91 void tx_start(const void *data, uint32_t size);
92 void tx_start_timeout(const void *data, uint32_t size);
93 void tx_start_isr(const void *data, uint32_t size);
94
95 USBDriverTimer write_timeout {this};
98
99 protected:
100 friend class BulkRX<TeensyHostMIDI, uint32_t, MaxPacketSize>;
101 void rx_start(void *data, uint32_t size);
102 void rx_start_isr(void *data, uint32_t size);
103
104 private:
106 void in_callback(const Transfer_t *transfer);
107 void out_callback(const Transfer_t *transfer);
108
109 private:
110 template <class T, size_t N>
111 static constexpr size_t len(T (&)[N]) {
112 return N;
113 }
114};
115
117
118#include "TeensyHostMIDI.ipp"
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Teensy USB Host MIDI driver.
void disconnect() override
uint32_t read_finish(const Transfer_t *transfer)
microseconds write_error_timeout_duration
void read_start(uint8_t *buffer, uint32_t size)
void setErrorTimeout(microseconds timeout)
uint32_t write_finish(const Transfer_t *transfer)
void cancel_timeout()
bool claim_if_midi(Device_t *device, int type, const uint8_t *descriptors, uint32_t len)
void timeout_callback()
void rx_start(void *data, uint32_t size)
void timer_event(USBDriverTimer *whichtimer) override
void rx_start_isr(void *data, uint32_t size)
uint32_t read()
Try reading a 4-byte MIDI USB message.
bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len) override
USBDriverTimer write_timeout
void in_callback(const Transfer_t *transfer)
TeensyHostMIDI(USBHost &)
void read_start_isr(uint8_t *buffer, uint32_t size)
void start_timeout()
void setTimeout(microseconds timeout)
Set the timeout, the number of microseconds to buffer the outgoing MIDI messages.
static constexpr size_t len(T(&)[N])
microseconds write_timeout_duration
void write_start(uint8_t *buffer, uint32_t size)
static void tx_callback(const Transfer_t *transfer)
bool connectedForWrite() const
void out_callback(const Transfer_t *transfer)
void tx_start_isr(const void *data, uint32_t size)
static void rx_callback(const Transfer_t *transfer)
void tx_start_timeout(const void *data, uint32_t size)
std::chrono::microseconds microseconds
strbuf_t mystring_bufs[1]
void tx_start(const void *data, uint32_t size)
void write_start_isr(uint8_t *buffer, uint32_t size)
Receives Bulk packets (OUT for device mode, IN for host mode)
Definition BulkRX.hpp:12
static constexpr uint16_t MaxPacketSize
Definition BulkRX.hpp:24
Sends Bulk packets (IN for device mode, OUT for host mode)
Definition BulkTX.hpp:12
uint32_t write_nonblock(const MessageType *msgs, uint32_t num_msgs)
Send multiple MIDI USB messages without blocking.
void send_now()
Try sending the buffered data now.
uint32_t getWriteError() const
Get the number messages that failed to send.
Definition BulkTX.hpp:54
void write(MessageType msg)
Send a MIDI USB message.
uint32_t clearWriteError()
Get and clear the number messages that failed to send.
Definition BulkTX.hpp:56