Control Surface disable-pipes
MIDI Control Surface library for Arduino
USBMIDI_Interface.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "MIDI_Interface.hpp"
4#include "USBMIDI/USBMIDI.hpp"
5#include "USBMIDI_Sender.hpp"
6#include <AH/Error/Error.hpp>
9
11
13
18template <class Backend>
20 public:
24 template <class... Args>
26 : backend(std::forward<Args>(args)...),
27 alwaysSendImmediately_(backend.preferImmediateSend()) {}
28
29 private:
30 // MIDI send implementations
35 void sendNowImpl() override { backend.sendNow(); }
36
37 private:
38#if !DISABLE_PIPES
39 void handleStall() override { MIDI_Interface::handleStall(this); }
40#endif
41
42 public:
43 void begin() override;
44 void update() override;
45
46 public:
49
54
63
65
66 public:
69
71 Backend backend;
72
73 private:
75 struct Sender {
77 void operator()(Cable cn, MIDICodeIndexNumber cin, uint8_t d0,
78 uint8_t d1, uint8_t d2) {
79 uint8_t cn_cin = (cn.getRaw() << 4) | uint8_t(cin);
80 iface->backend.write({cn_cin, d0, d1, d2});
81 }
82 };
84
85 private:
91 bool alwaysSendImmediately_ = true;
92
93 public:
96
101 bool alwaysSendsImmediately() const { return alwaysSendImmediately_; }
108 void neverSendImmediately() { alwaysSendImmediately_ = false; }
111 void alwaysSendImmediately() { alwaysSendImmediately_ = true; }
112
114};
115
117
118#include "USBMIDI_Interface.ipp"
119
120#if defined(TEENSYDUINO) && !defined(TEENSY_MIDIUSB_ENABLED)
121#warning \
122 "Teensy: USB MIDI not enabled. Enable it from the Tools > USB Type menu."
123#define CS_USB_MIDI_DISABLED
124#endif
125
126// If MIDI over USB is supported
127#if (!defined(CS_USB_MIDI_NOT_SUPPORTED) && !defined(CS_USB_MIDI_DISABLED)) || \
128 !defined(ARDUINO)
129
131
149 : public GenericUSBMIDI_Interface<USBDeviceMIDIBackend> {
150 public:
151 USBMIDI_Interface() = default;
153};
154
156
157// If the main MCU doesn't have a USB connection:
158// Fall back on Serial connection at the hardware MIDI baud rate.
159// (Can be used with HIDUINO or USBMidiKliK.)
160#elif !defined(CS_USB_MIDI_DISABLED)
161
163
165
175 public:
180};
181
183
184#endif
185
MIDIReadEvent
Values returned by the MIDI reading functions.
constexpr auto MIDI_BAUD
MIDICodeIndexNumber
MIDI USB Code Index Numbers.
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:53
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:52
A type-safe class for MIDI USB Cable numbers.
Definition: Cable.hpp:13
constexpr uint8_t getRaw() const
Get the cable as an integer.
Definition: Cable.hpp:29
A class for MIDI interfaces sending MIDI messages over a USB MIDI connection.
ChannelMessage getChannelMessage() const
Return the received channel voice message.
RealTimeMessage getRealTimeMessage() const
Return the received real-time message.
void sendChannelMessageImpl(ChannelMessage) override
Low-level function for sending a MIDI channel voice message.
void alwaysSendImmediately()
Send the USB packets immediately after sending a MIDI message.
void handleStall() override
Call back that should finish any MIDI messages that are in progress, and un-stall the pipe or MIDI so...
void update() override
Read the MIDI interface and call the callback if a message was received.
void neverSendImmediately()
Don't send the USB packets immediately after sending a MIDI message.
USBMIDI_Sender sender
Sends USB MIDI messages.
void sendNowImpl() override
Low-level function for sending any buffered outgoing MIDI messages.
SysCommonMessage getSysCommonMessage() const
Return the received system common message.
void sendRealTimeImpl(RealTimeMessage) override
Low-level function for sending a MIDI real-time message.
USBMIDI_Parser parser
Parses USB packets into MIDI messages.
Backend backend
The (platform-specific) backend used for MIDI over USB communication.
void begin() override
Initialize the MIDI Interface.
void sendSysExImpl(SysExMessage) override
Low-level function for sending a system exclusive MIDI message.
void sendSysCommonImpl(SysCommonMessage) override
Low-level function for sending a MIDI system common message.
bool alwaysSendsImmediately() const
Check if this USB interface always sends its USB packets immediately after sending a MIDI message.
GenericUSBMIDI_Interface(Args &&...args)
Construct a new GenericUSBMIDI_Interface.
MIDIReadEvent read()
Try reading and parsing a single incoming MIDI message.
SysExMessage getSysExMessage() const
Return the received system exclusive message.
An abstract class for MIDI interfaces.
A class for MIDI interfaces sending MIDI messages over a USB MIDI connection.
USBMIDI_Interface()=default
Parser for MIDI over USB packets.
A class for sending MIDI USB messages.
A class for MIDI interfaces sending and receiving MIDI messages over the Serial port of the USB conne...
Functor to send USB MIDI packets.
void operator()(Cable cn, MIDICodeIndexNumber cin, uint8_t d0, uint8_t d1, uint8_t d2)
GenericUSBMIDI_Interface * iface
virtual void handleStall()=0
Call back that should finish any MIDI messages that are in progress, and un-stall the pipe or MIDI so...
AH::Array< uint8_t, 4 > MIDIUSBPacket_t
Definition: USBMIDI_Mock.hpp:9