Sound Byte Libs 0.5.1-121-g3358a44
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/hw/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#ifndef SBL_HW_MIDI_INPUT_HPP_
10#define SBL_HW_MIDI_INPUT_HPP_
11
13
14#if defined(SBL_HAS_USB) && defined(SBL_HAS_USB_MIDI)
15#include <sbl/usb/midi.hpp>
16#endif
17
18namespace sbl::midi {
19
20/**
21 * @brief Poll UART for MIDI bytes and feed them to the parser
22 *
23 * Drains all available bytes from the UART RX buffer.
24 * Non-blocking — returns immediately if no data available.
25 *
26 * @tparam UartDriver UART driver class with available() and read_byte()
27 * @note ISR-safe — drains available bytes from ring buffer, no blocking.
28 */
29template<typename UartDriver>
30inline void poll(Parser& parser) {
31 while (UartDriver::available()) {
32 parser.push(UartDriver::read_byte());
33 }
34}
35
36#if defined(SBL_HAS_USB) && defined(SBL_HAS_USB_MIDI)
37/**
38 * @brief Poll USB MIDI for bytes and feed them to the parser
39 *
40 * TinyUSB's stream_read unpacks 4-byte USB MIDI packets into standard
41 * MIDI bytes, which we feed directly to the existing parser.
42 * Non-blocking — returns immediately if no data available.
43 *
44 * @note NOT ISR-safe — calls TinyUSB device stack. Main loop only.
45 */
46inline void poll_usb(Parser& parser) {
47 uint8_t buf[64];
48 uint32_t n = sbl::usb::MidiPort::read(buf, sizeof(buf));
49 if (n > 0) {
50 parser.push(buf, static_cast<uint16_t>(n));
51 }
52}
53#endif // SBL_HAS_USB && SBL_HAS_USB_MIDI
54
55} // namespace sbl::midi
56
57#endif // SBL_HW_MIDI_INPUT_HPP_
MIDI byte stream parser — state machine with running status.
Definition parser.hpp:36
void push(uint8_t byte)
Feed one byte from UART RX.
Definition parser.hpp:60
static uint32_t read(uint8_t *buf, uint32_t max_len)
Read decoded MIDI bytes from USB (RX)
Definition midi.cpp:28
USB MIDI device interface.
MIDI parser, encoder, SMF and sequencer.
Definition input.hpp:18
void poll(Parser &parser)
Poll UART for MIDI bytes and feed them to the parser.
Definition input.hpp:30
MIDI byte stream parser.