Control Surface new-input
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 
18 template <class Backend>
20  public:
24  template <class... Args>
25  GenericUSBMIDI_Interface(Args &&...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 update() override;
42 
43  public:
46 
50  MIDIReadEvent read();
51 
53  ChannelMessage getChannelMessage() const;
55  SysCommonMessage getSysCommonMessage() const;
57  RealTimeMessage getRealTimeMessage() const;
59  SysExMessage getSysExMessage() const;
60 
62 
63  public:
66 
68  Backend backend;
69 
70  private:
72  struct Sender {
74  void operator()(Cable cn, MIDICodeIndexNumber cin, uint8_t d0,
75  uint8_t d1, uint8_t d2) {
76  uint8_t cn_cin = (cn.getRaw() << 4) | uint8_t(cin);
77  iface->backend.write(cn_cin, d0, d1, d2);
78  }
79  };
81 
82  private:
88  bool alwaysSendImmediately_ = true;
89 
90  public:
93 
98  bool alwaysSendsImmediately() const { return alwaysSendImmediately_; }
105  void neverSendImmediately() { alwaysSendImmediately_ = false; }
108  void alwaysSendImmediately() { alwaysSendImmediately_ = true; }
109 
111 };
112 
114 
115 #include "USBMIDI_Interface.ipp"
116 
117 #if defined(TEENSYDUINO) && !defined(TEENSY_MIDIUSB_ENABLED)
118 #warning \
119  "Teensy: USB MIDI not enabled. Enable it from the Tools > USB Type menu."
120 #define CS_USB_MIDI_DISABLED
121 #endif
122 
123 // If MIDI over USB is supported
124 #if (!defined(CS_USB_MIDI_NOT_SUPPORTED) && !defined(CS_USB_MIDI_DISABLED)) || \
125  !defined(ARDUINO)
126 
128 
146  : public GenericUSBMIDI_Interface<USBDeviceMIDIBackend> {
147  public:
148  USBMIDI_Interface() = default;
150 };
151 
153 
154 // If the main MCU doesn't have a USB connection:
155 // Fall back on Serial connection at the hardware MIDI baud rate.
156 // (Can be used with HIDUINO or USBMidiKliK.)
157 #elif !defined(CS_USB_MIDI_DISABLED)
158 
159 #include "SerialMIDI_Interface.hpp"
160 
162 
172  public:
177 };
178 
180 
181 #endif
182 
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:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
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