Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
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
16template <class Backend>
18 public:
22 template <class... Args>
24 : backend {std::forward<Args>(args)...},
25 alwaysSendImmediately_(backend.preferImmediateSend()) {}
26
32
33 private:
34 // MIDI send implementations
39 void sendNowImpl() override { backend.sendNow(); }
40
41 private:
42#if !DISABLE_PIPES
43 void handleStall() override { MIDI_Interface::handleStall(this); }
44#ifdef DEBUG_OUT
45 const char *getName() const override { return "usb"; }
46#endif
47#endif
48
49 public:
52
54 void begin() override;
58 void update() override;
59
61
62 public:
65
70
79
81
82 public:
85
87 Backend backend;
88
89 private:
91 struct Sender {
93 void operator()(Cable cn, MIDICodeIndexNumber cin, uint8_t d0,
94 uint8_t d1, uint8_t d2) {
95 uint8_t cn_cin = (cn.getRaw() << 4) | uint8_t(cin);
96 iface->backend.write({cn_cin, d0, d1, d2});
97 }
98 };
100
101 private:
108
109 public:
112
128
130};
131
133
134#include "USBMIDI_Interface.ipp"
135
136#if defined(TEENSYDUINO) && !defined(TEENSY_MIDIUSB_ENABLED)
137#pragma message( \
138 "Teensy: USB MIDI not enabled. Enable it from the Tools > USB Type menu.")
139#define CS_USB_MIDI_DISABLED
140#endif
141
142// If MIDI over USB is supported
143#if (!defined(CS_USB_MIDI_NOT_SUPPORTED) && !defined(CS_USB_MIDI_DISABLED)) || \
144 !defined(ARDUINO)
145
147
171
173
174// If the main MCU doesn't have a USB connection:
175// Fall back on Serial connection at the hardware MIDI baud rate.
176// (Can be used with HIDUINO or USBMidiKliK.)
177#elif !defined(CS_USB_MIDI_DISABLED)
178
180
182
192 public:
197};
198
200
201#endif
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
MIDIUSB_USBDeviceMIDIBackend USBDeviceMIDIBackend
Definition USBMIDI.hpp:78
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.
GenericUSBMIDI_Interface(const GenericUSBMIDI_Interface &)=delete
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
Poll the backend (if necessary) and invoke the callbacks for any received MIDI messages,...
void neverSendImmediately()
Don't send the USB packets immediately after sending a MIDI message.
GenericUSBMIDI_Interface & operator=(GenericUSBMIDI_Interface &&)=delete
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.
GenericUSBMIDI_Interface(GenericUSBMIDI_Interface &&)=delete
USBMIDI_Parser parser
Parses USB packets into MIDI messages.
const char * getName() const override
Get the staller's name for debugging purposes.
Backend backend
The (platform-specific) backend used for MIDI over USB communication.
void begin() override
Initialize.
GenericUSBMIDI_Interface & operator=(const GenericUSBMIDI_Interface &)=delete
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.
virtual void handleStall()=0
Call back that should finish any MIDI messages that are in progress, and un-stall the pipe or MIDI so...
A class for MIDI interfaces sending MIDI messages over a USB MIDI connection.
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