Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
input.hpp
Go to the documentation of this file.
1// sbl/midi/input.hpp — HAL convenience for MIDI input
2//
3// Drains available bytes from UART RX or USB MIDI into a MIDI parser.
4//
5// Usage:
6// sbl::midi::poll<sbl::driver::Uart<1>>(parser); // UART MIDI
7// sbl::midi::poll_usb(parser); // USB MIDI
8
9#pragma once
10
11#include <sbl/midi/parser.hpp>
12
13#if defined(SBL_HAS_USB) && defined(SBL_HAS_USB_MIDI)
14#include <sbl/usb/midi.hpp>
15#endif
16
17namespace sbl::midi {
18
19/**
20 * @brief Poll UART for MIDI bytes and feed them to the parser
21 *
22 * Drains all available bytes from the UART RX buffer.
23 * Non-blocking — returns immediately if no data available.
24 *
25 * @tparam UartDriver UART driver class with available() and read_byte()
26 * @note ISR-safe — drains available bytes from ring buffer, no blocking.
27 */
28template<typename UartDriver>
29inline void poll(Parser& parser) {
30 while (UartDriver::available()) {
31 parser.push(UartDriver::read_byte());
32 }
33}
34
35#if defined(SBL_HAS_USB) && defined(SBL_HAS_USB_MIDI)
36/**
37 * @brief Poll USB MIDI for bytes and feed them to the parser
38 *
39 * TinyUSB's stream_read unpacks 4-byte USB MIDI packets into standard
40 * MIDI bytes, which we feed directly to the existing parser.
41 * Non-blocking — returns immediately if no data available.
42 *
43 * @note NOT ISR-safe — calls TinyUSB device stack. Main loop only.
44 */
45inline void poll_usb(Parser& parser) {
46 uint8_t buf[64];
47 uint32_t n = sbl::usb::MidiPort::read(buf, sizeof(buf));
48 if (n > 0) {
49 parser.push(buf, static_cast<uint16_t>(n));
50 }
51}
52#endif // SBL_HAS_USB && SBL_HAS_USB_MIDI
53
54} // namespace sbl::midi
MIDI byte stream parser — state machine with running status.
Definition parser.hpp:35
void push(uint8_t byte)
Feed one byte from UART RX.
Definition parser.hpp:59
static uint32_t read(uint8_t *buf, uint32_t max_len)
Read decoded MIDI bytes from USB (RX)
Definition midi.cpp:25
MIDI protocol support.
Definition input.hpp:17
void poll(Parser &parser)
Poll UART for MIDI bytes and feed them to the parser.
Definition input.hpp:29
USB MIDI device interface.