Control Surface stm32
MIDI Control Surface library for Arduino
Classes | Public Types | Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
PluggableUSBMIDI Class Reference

#include <MIDI_Interfaces/USBMIDI/mbed-m0/PluggableUSBMIDI.hpp>

Detailed Description

Definition at line 18 of file mbed-m0/PluggableUSBMIDI.hpp.

+ Inheritance diagram for PluggableUSBMIDI:
+ Collaboration diagram for PluggableUSBMIDI:

Classes

struct  Reading
 State for reading incoming USB-MIDI data. More...
 
struct  Writing
 State for writing outgoing USB-MIDI data. More...
 

Public Types

using setup_packet_t = USBDevice::setup_packet_t
 
using DeviceState = USBDevice::DeviceState
 
using microseconds = std::chrono::microseconds
 
using setup_packet_t = USBDevice::setup_packet_t
 
using DeviceState = USBDevice::DeviceState
 
using microseconds = std::chrono::microseconds
 

Public Member Functions

 PluggableUSBMIDI ()
 
 ~PluggableUSBMIDI ()
 
bool connected () const
 Check if this class is connected and ready. More...
 
void write (uint32_t msg)
 Send a MIDI USB message. More...
 
void write (const uint32_t *msgs, uint32_t num_msgs)
 Send multiple MIDI USB messages. More...
 
template<size_t N>
void write (const uint32_t(&msgs)[N])
 Send multiple MIDI USB messages. May block. More...
 
uint32_t write_nonblock (const uint32_t *msgs, uint32_t num_msgs)
 Send multiple MIDI USB messages without blocking. More...
 
uint32_t read ()
 Try reading a 4-byte MIDI USB message. More...
 
void send_now ()
 Try sending the buffered data now. More...
 
void setTimeout (microseconds timeout)
 Set the timeout, the number of microseconds to buffer the outgoing MIDI messages. More...
 
void setErrorTimeout (microseconds timeout)
 
uint32_t getWriteError () const
 Count how many USB packets were dropped. More...
 
void clearWriteError ()
 Clear the counter of how many USB packets were dropped. More...
 
 PluggableUSBMIDI ()
 
 ~PluggableUSBMIDI ()
 
bool connected () const
 Check if this class is connected and ready. More...
 
void write (uint32_t msg)
 Send a MIDI USB message. More...
 
void write (const uint32_t *msgs, uint32_t num_msgs)
 Send multiple MIDI USB messages. More...
 
template<size_t N>
void write (const uint32_t(&msgs)[N])
 Send multiple MIDI USB messages. May block. More...
 
uint32_t write_nonblock (const uint32_t *msgs, uint32_t num_msgs)
 Send multiple MIDI USB messages without blocking. More...
 
uint32_t read ()
 Try reading a 4-byte MIDI USB message. More...
 
void send_now ()
 Try sending the buffered data now. More...
 
void setTimeout (microseconds timeout)
 Set the timeout, the number of microseconds to buffer the outgoing MIDI messages. More...
 
void setErrorTimeout (microseconds timeout)
 
uint32_t getWriteError () const
 Count how many USB packets were dropped. More...
 
void clearWriteError ()
 Clear the counter of how many USB packets were dropped. More...
 

Static Public Attributes

static constexpr uint32_t PacketSize = 64
 USB packet size. More...
 

Protected Types

using rbuffer_t = Reading::Buffer
 
using wbuffer_t = Writing::Buffer
 
using rbuffer_t = std::remove_reference_t< decltype(reading.buffers[0])>
 
using wbuffer_t = std::remove_reference_t< decltype(writing.buffers[0])>
 

Protected Member Functions

void init (EndpointResolver &resolver) override
 
void callback_state_change (DeviceState new_state) override
 
uint32_t callback_request (const setup_packet_t *setup, USBDevice::RequestResult *result, uint8_t **data) override
 
bool callback_request_xfer_done (const setup_packet_t *setup, bool aborted) override
 
bool callback_set_configuration (uint8_t configuration) override
 
void callback_set_interface (uint16_t interface, uint8_t alternate) override
 
const uint8_t * string_iinterface_desc () override
 
const uint8_t * configuration_desc (uint8_t index) override
 
uint8_t getProductVersion () override
 
uint32_t write_impl (const uint32_t *msgs, uint32_t num_msgs, bool nonblocking)
 
std::tuple< uint32_t, wbuffer_t *, uint32_t > read_writebuf_size ()
 
void write_start_sync (uint8_t *buffer, uint32_t size)
 
void send_in_callback (uint32_t activebuf_idx)
 
bool send_now_impl_nonblock (uint32_t activebuf_idx)
 
void timeout_callback ()
 
void in_callback ()
 
void out_callback ()
 
void init (EndpointResolver &resolver) override
 
void callback_state_change (DeviceState new_state) override
 
uint32_t callback_request (const setup_packet_t *setup, USBDevice::RequestResult *result, uint8_t **data) override
 
bool callback_request_xfer_done (const setup_packet_t *setup, bool aborted) override
 
bool callback_set_configuration (uint8_t configuration) override
 
void callback_set_interface (uint16_t interface, uint8_t alternate) override
 
const uint8_t * string_iinterface_desc () override
 
const uint8_t * configuration_desc (uint8_t index) override
 
uint8_t getProductVersion () override
 
uint32_t write_impl (const uint32_t *msgs, uint32_t num_msgs, bool nonblocking)
 
std::tuple< uint32_t, wbuffer_t *, uint32_t > read_writebuf_size ()
 
void write_start_sync (uint8_t *buffer, uint32_t size)
 
void send_in_callback (uint32_t activebuf_idx)
 
bool send_now_impl_nonblock (uint32_t activebuf_idx)
 
void timeout_callback ()
 
void in_callback ()
 
void out_callback ()
 

Protected Attributes

std::atomic< bool > usb_connected {false}
 
struct PluggableUSBMIDI::Reading reading
 
struct PluggableUSBMIDI::Writing writing
 
usb_ep_t bulk_in_ep
 
usb_ep_t bulk_out_ep
 
uint8_t config_descriptor [0x65]
 

Static Protected Attributes

static constexpr uint32_t SizeReserved = PacketSize + 1
 
static constexpr uint32_t NumRxPackets = 2
 

Class Documentation

◆ PluggableUSBMIDI::Reading

struct PluggableUSBMIDI::Reading
+ Collaboration diagram for PluggableUSBMIDI::Reading:
Class Members
struct Buffer buffers[NumRxPackets]
uint32_t available
uint32_t read_idx
uint32_t write_idx
bool reading
atomic< uint32_t > available
atomic< uint32_t > read_idx
atomic< uint32_t > write_idx
atomic< bool > reading

◆ PluggableUSBMIDI::Writing

struct PluggableUSBMIDI::Writing
+ Collaboration diagram for PluggableUSBMIDI::Writing:
Class Members
struct Buffer buffers[2]
uint32_t active_writebuffer The index of the buffer that is currently being written to.
Buffer * sending Buffer that is being sent.
Buffer * send_timeout Buffer to be sent in the timeout callback.
microseconds timeout_duration
microseconds error_timeout_duration
Timeout timeout
uint32_t errors
atomic< uint32_t > active_writebuffer
atomic< Buffer * > sending
atomic< Buffer * > send_timeout

Member Typedef Documentation

◆ setup_packet_t [1/2]

using setup_packet_t = USBDevice::setup_packet_t

Definition at line 24 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ DeviceState [1/2]

using DeviceState = USBDevice::DeviceState

Definition at line 25 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ microseconds [1/2]

using microseconds = std::chrono::microseconds

Definition at line 26 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ rbuffer_t [1/2]

using rbuffer_t = Reading::Buffer
protected

Definition at line 126 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ wbuffer_t [1/2]

using wbuffer_t = Writing::Buffer
protected

Definition at line 154 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ setup_packet_t [2/2]

using setup_packet_t = USBDevice::setup_packet_t

Definition at line 24 of file mbed/PluggableUSBMIDI.hpp.

◆ DeviceState [2/2]

using DeviceState = USBDevice::DeviceState

Definition at line 25 of file mbed/PluggableUSBMIDI.hpp.

◆ microseconds [2/2]

using microseconds = std::chrono::microseconds

Definition at line 26 of file mbed/PluggableUSBMIDI.hpp.

◆ rbuffer_t [2/2]

using rbuffer_t = std::remove_reference_t<decltype(reading.buffers[0])>
protected

Definition at line 126 of file mbed/PluggableUSBMIDI.hpp.

◆ wbuffer_t [2/2]

using wbuffer_t = std::remove_reference_t<decltype(writing.buffers[0])>
protected

Definition at line 143 of file mbed/PluggableUSBMIDI.hpp.

Constructor & Destructor Documentation

◆ PluggableUSBMIDI() [1/2]

◆ ~PluggableUSBMIDI() [1/2]

◆ PluggableUSBMIDI() [2/2]

◆ ~PluggableUSBMIDI() [2/2]

Member Function Documentation

◆ connected() [1/2]

bool connected ( ) const

Check if this class is connected and ready.

◆ write() [1/6]

void write ( uint32_t  msg)

Send a MIDI USB message.

May block.

Parameters
msgThe 4-byte MIDI USB message to send.

◆ write() [2/6]

void write ( const uint32_t *  msgs,
uint32_t  num_msgs 
)

Send multiple MIDI USB messages.

May block.

Parameters
msgsAn array of 4-byte MIDI USB messages to send.
num_msgsThe number of messages in the array.

◆ write() [3/6]

void write ( const uint32_t(&)  msgs[N])
inline

Send multiple MIDI USB messages. May block.

Definition at line 48 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ write_nonblock() [1/2]

uint32_t write_nonblock ( const uint32_t *  msgs,
uint32_t  num_msgs 
)

Send multiple MIDI USB messages without blocking.

Parameters
msgsAn array of 4-byte MIDI USB messages to send.
num_msgsThe number of messages in the array.
Returns
The number of messages that were actually sent.

◆ read() [1/2]

uint32_t read ( )

Try reading a 4-byte MIDI USB message.

Returns
The message or 0x00000000 if no messages available.

◆ send_now() [1/2]

void send_now ( )

Try sending the buffered data now.

Start transmitting the latest packet if possible, even if it isn't full yet. If the latest packet is empty, this function has no effect.

◆ setTimeout() [1/2]

void setTimeout ( microseconds  timeout)
inline

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

A shorter timeout usually results in lower latency, but also causes more overhead, because more packets might be required.

Definition at line 74 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ setErrorTimeout() [1/2]

void setErrorTimeout ( microseconds  timeout)
inline

Definition at line 78 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ getWriteError() [1/2]

uint32_t getWriteError ( ) const
inline

Count how many USB packets were dropped.

Definition at line 83 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ clearWriteError() [1/2]

void clearWriteError ( )
inline

Clear the counter of how many USB packets were dropped.

Definition at line 85 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ init() [1/2]

void init ( EndpointResolver &  resolver)
overrideprotected

◆ callback_state_change() [1/2]

void callback_state_change ( DeviceState  new_state)
overrideprotected

◆ callback_request() [1/2]

uint32_t callback_request ( const setup_packet_t setup,
USBDevice::RequestResult *  result,
uint8_t **  data 
)
overrideprotected

◆ callback_request_xfer_done() [1/2]

bool callback_request_xfer_done ( const setup_packet_t setup,
bool  aborted 
)
overrideprotected

◆ callback_set_configuration() [1/2]

bool callback_set_configuration ( uint8_t  configuration)
overrideprotected

◆ callback_set_interface() [1/2]

void callback_set_interface ( uint16_t  interface,
uint8_t  alternate 
)
overrideprotected

◆ string_iinterface_desc() [1/2]

const uint8_t * string_iinterface_desc ( )
overrideprotected

◆ configuration_desc() [1/2]

const uint8_t * configuration_desc ( uint8_t  index)
overrideprotected

◆ getProductVersion() [1/2]

uint8_t getProductVersion ( )
inlineoverrideprotected

Definition at line 100 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ write_impl() [1/2]

uint32_t write_impl ( const uint32_t *  msgs,
uint32_t  num_msgs,
bool  nonblocking 
)
protected

◆ read_writebuf_size() [1/2]

std::tuple< uint32_t, wbuffer_t *, uint32_t > read_writebuf_size ( )
protected

◆ write_start_sync() [1/2]

void write_start_sync ( uint8_t *  buffer,
uint32_t  size 
)
protected

◆ send_in_callback() [1/2]

void send_in_callback ( uint32_t  activebuf_idx)
protected

◆ send_now_impl_nonblock() [1/2]

bool send_now_impl_nonblock ( uint32_t  activebuf_idx)
protected

◆ timeout_callback() [1/2]

void timeout_callback ( )
protected

◆ in_callback() [1/2]

void in_callback ( )
protected

◆ out_callback() [1/2]

void out_callback ( )
protected

◆ connected() [2/2]

bool connected ( ) const

Check if this class is connected and ready.

◆ write() [4/6]

void write ( uint32_t  msg)

Send a MIDI USB message.

May block.

Parameters
msgThe 4-byte MIDI USB message to send.

◆ write() [5/6]

void write ( const uint32_t *  msgs,
uint32_t  num_msgs 
)

Send multiple MIDI USB messages.

May block.

Parameters
msgsAn array of 4-byte MIDI USB messages to send.
num_msgsThe number of messages in the array.

◆ write() [6/6]

void write ( const uint32_t(&)  msgs[N])
inline

Send multiple MIDI USB messages. May block.

Definition at line 48 of file mbed/PluggableUSBMIDI.hpp.

◆ write_nonblock() [2/2]

uint32_t write_nonblock ( const uint32_t *  msgs,
uint32_t  num_msgs 
)

Send multiple MIDI USB messages without blocking.

Parameters
msgsAn array of 4-byte MIDI USB messages to send.
num_msgsThe number of messages in the array.
Returns
The number of messages that were actually sent.

◆ read() [2/2]

uint32_t read ( )

Try reading a 4-byte MIDI USB message.

Returns
The message or 0x00000000 if no messages available.

◆ send_now() [2/2]

void send_now ( )

Try sending the buffered data now.

Start transmitting the latest packet if possible, even if it isn't full yet. If the latest packet is empty, this function has no effect.

◆ setTimeout() [2/2]

void setTimeout ( microseconds  timeout)
inline

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

A shorter timeout usually results in lower latency, but also causes more overhead, because more packets might be required.

Definition at line 74 of file mbed/PluggableUSBMIDI.hpp.

◆ setErrorTimeout() [2/2]

void setErrorTimeout ( microseconds  timeout)
inline

Definition at line 78 of file mbed/PluggableUSBMIDI.hpp.

◆ getWriteError() [2/2]

uint32_t getWriteError ( ) const
inline

Count how many USB packets were dropped.

Definition at line 83 of file mbed/PluggableUSBMIDI.hpp.

◆ clearWriteError() [2/2]

void clearWriteError ( )
inline

Clear the counter of how many USB packets were dropped.

Definition at line 85 of file mbed/PluggableUSBMIDI.hpp.

◆ init() [2/2]

void init ( EndpointResolver &  resolver)
overrideprotected

◆ callback_state_change() [2/2]

void callback_state_change ( DeviceState  new_state)
overrideprotected

◆ callback_request() [2/2]

uint32_t callback_request ( const setup_packet_t setup,
USBDevice::RequestResult *  result,
uint8_t **  data 
)
overrideprotected

◆ callback_request_xfer_done() [2/2]

bool callback_request_xfer_done ( const setup_packet_t setup,
bool  aborted 
)
overrideprotected

◆ callback_set_configuration() [2/2]

bool callback_set_configuration ( uint8_t  configuration)
overrideprotected

◆ callback_set_interface() [2/2]

void callback_set_interface ( uint16_t  interface,
uint8_t  alternate 
)
overrideprotected

◆ string_iinterface_desc() [2/2]

const uint8_t * string_iinterface_desc ( )
overrideprotected

◆ configuration_desc() [2/2]

const uint8_t * configuration_desc ( uint8_t  index)
overrideprotected

◆ getProductVersion() [2/2]

uint8_t getProductVersion ( )
inlineoverrideprotected

Definition at line 100 of file mbed/PluggableUSBMIDI.hpp.

◆ write_impl() [2/2]

uint32_t write_impl ( const uint32_t *  msgs,
uint32_t  num_msgs,
bool  nonblocking 
)
protected

◆ read_writebuf_size() [2/2]

std::tuple< uint32_t, wbuffer_t *, uint32_t > read_writebuf_size ( )
protected

◆ write_start_sync() [2/2]

void write_start_sync ( uint8_t *  buffer,
uint32_t  size 
)
protected

◆ send_in_callback() [2/2]

void send_in_callback ( uint32_t  activebuf_idx)
protected

◆ send_now_impl_nonblock() [2/2]

bool send_now_impl_nonblock ( uint32_t  activebuf_idx)
protected

◆ timeout_callback() [2/2]

void timeout_callback ( )
protected

◆ in_callback() [2/2]

void in_callback ( )
protected

◆ out_callback() [2/2]

void out_callback ( )
protected

Member Data Documentation

◆ usb_connected

std::atomic< bool > usb_connected {false}
protected

Definition at line 103 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ PacketSize

static constexpr uint32_t PacketSize = 64
staticconstexpr

USB packet size.

Must be a power of two.

Must be a power of two.

Definition at line 108 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ SizeReserved

static constexpr uint32_t SizeReserved = PacketSize + 1
staticconstexprprotected

Definition at line 111 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ NumRxPackets

static constexpr uint32_t NumRxPackets = 2
staticconstexprprotected

Definition at line 112 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ reading

struct PluggableUSBMIDI::Reading reading
protected

◆ writing

struct PluggableUSBMIDI::Writing writing
protected

◆ bulk_in_ep

usb_ep_t bulk_in_ep
protected

Definition at line 156 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ bulk_out_ep

usb_ep_t bulk_out_ep
protected

Definition at line 157 of file mbed-m0/PluggableUSBMIDI.hpp.

◆ config_descriptor

uint8_t config_descriptor
protected

Definition at line 158 of file mbed-m0/PluggableUSBMIDI.hpp.


The documentation for this class was generated from the following files: