Control Surface pin-t-adl
MIDI Control Surface library for Arduino
USBMIDI_Teensy2.hpp
Go to the documentation of this file.
2
4#include <Settings/NamespaceSettings.hpp>
5
7
12 void sendNow();
14};
15
17
18#ifdef TEENSY_MIDIUSB_ENABLED
19
20#include <usb_private.h>
21
23
25 // https://github.com/PaulStoffregen/cores/blob/73ea157600a7082686d9cc48786a73caa7567da9/usb_midi/usb_api.cpp#L195
26 uint8_t c, intr_state;
27
28 intr_state = SREG;
29 cli();
30 if (!usb_configuration) {
31 SREG = intr_state;
32 return MIDIUSBPacket_t{0};
33 }
34 UENUM = MIDI_RX_ENDPOINT;
35retry:
36 c = UEINTX;
37 if (!(c & (1 << RWAL))) {
38 if (c & (1 << RXOUTI)) {
39 UEINTX = 0x6B;
40 goto retry;
41 }
42 SREG = intr_state;
43 return MIDIUSBPacket_t{0};
44 }
45 MIDIUSBPacket_t packet;
46 packet.data[0] = UEDATX;
47 packet.data[1] = UEDATX;
48 packet.data[2] = UEDATX;
49 packet.data[3] = UEDATX;
50 if (!(UEINTX & (1 << RWAL)))
51 UEINTX = 0x6B;
52 SREG = intr_state;
53
54 return packet;
55}
56
57inline void USBDeviceMIDIBackend::write(MIDIUSBPacket_t data) {
58 uint8_t intr_state, timeout;
59
60 if (!usb_configuration)
61 return;
62 intr_state = SREG;
63 cli();
64 UENUM = MIDI_TX_ENDPOINT;
65 timeout = UDFNUML + 2;
66 while (1) {
67 // are we ready to transmit?
68 if (UEINTX & (1 << RWAL))
69 break;
70 SREG = intr_state;
71 if (UDFNUML == timeout)
72 return;
73 if (!usb_configuration)
74 return;
75 intr_state = SREG;
76 cli();
77 UENUM = MIDI_TX_ENDPOINT;
78 }
79 UEDATX = data.data[0];
80 UEDATX = data.data[1];
81 UEDATX = data.data[2];
82 UEDATX = data.data[3];
83 if (!(UEINTX & (1 << RWAL)))
84 UEINTX = 0x3A;
85 SREG = intr_state;
86}
87
89 // TODO: I think the UEINTX = 0x3A actually sends/flushes the data, but I'm
90 // not sure, and I don't have the right hardware to test it.
91}
92
93inline bool USBDeviceMIDIBackend::preferImmediateSend() { return false; }
94
96
97#endif
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
T data[N]
Definition: Array.hpp:37
MIDIUSBPacket_t read()
void write(MIDIUSBPacket_t data)
void write(MIDIUSBPacket_t packet)
AH::Array< uint8_t, 4 > MIDIUSBPacket_t