Control Surface main
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
31 void sendChannelMessageImpl(ChannelMessage) override;
32 void sendSysCommonImpl(SysCommonMessage) override;
33 void sendSysExImpl(SysExMessage) override;
34 void sendRealTimeImpl(RealTimeMessage) override;
35 void sendNowImpl() override { backend.sendNow(); }
36
37 private:
38 void handleStall() override;
39
40 public:
41 void begin() override;
42 void update() override;
43
44 public:
47
51 MIDIReadEvent read();
52
54 ChannelMessage getChannelMessage() const;
56 SysCommonMessage getSysCommonMessage() const;
58 RealTimeMessage getRealTimeMessage() const;
60 SysExMessage getSysExMessage() const;
61
63
64 public:
67
69 Backend backend;
70
71 private:
73 struct Sender {
75 void operator()(Cable cn, MIDICodeIndexNumber cin, uint8_t d0,
76 uint8_t d1, uint8_t d2) {
77 uint8_t cn_cin = (cn.getRaw() << 4) | uint8_t(cin);
78 iface->backend.write({cn_cin, d0, d1, d2});
79 }
80 };
82
83 private:
89 bool alwaysSendImmediately_ = true;
90
91 public:
94
99 bool alwaysSendsImmediately() const { return alwaysSendImmediately_; }
106 void neverSendImmediately() { alwaysSendImmediately_ = false; }
109 void alwaysSendImmediately() { alwaysSendImmediately_ = true; }
110
112};
113
115
116#include "USBMIDI_Interface.ipp"
117
118#if defined(TEENSYDUINO) && !defined(TEENSY_MIDIUSB_ENABLED)
119#warning \
120 "Teensy: USB MIDI not enabled. Enable it from the Tools > USB Type menu."
121#define CS_USB_MIDI_DISABLED
122#endif
123
124// If MIDI over USB is supported
125#if (!defined(CS_USB_MIDI_NOT_SUPPORTED) && !defined(CS_USB_MIDI_DISABLED)) || \
126 !defined(ARDUINO)
127
129
147 : public GenericUSBMIDI_Interface<USBDeviceMIDIBackend> {
148 public:
149 USBMIDI_Interface() = default;
151};
152
154
155// If the main MCU doesn't have a USB connection:
156// Fall back on Serial connection at the hardware MIDI baud rate.
157// (Can be used with HIDUINO or USBMidiKliK.)
158#elif !defined(CS_USB_MIDI_DISABLED)
159
161
163
173 public:
178};
179
181
182#endif
183
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:37
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:36
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.
void alwaysSendImmediately()
Send the USB packets immediately after sending a MIDI message.
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.
USBMIDI_Parser parser
Parses USB packets into MIDI messages.
Backend backend
The (platform-specific) backend used for MIDI over USB communication.
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.
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
AH::Array< uint8_t, 4 > MIDIUSBPacket_t