Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
drain.hpp
Go to the documentation of this file.
1/**
2 * @file drain.hpp
3 * @brief Feed every byte a MIDI source has waiting into a parser
4 *
5 * The source is any type with static byte access in one of two shapes,
6 * detected at compile time:
7 *
8 * - `available() -> bool` and `read_byte() -> uint8_t`: a UART driver.
9 * - `read(uint8_t* buf, uint32_t max) -> uint32_t`, returning 0 when empty:
10 * `sbl::usb::MidiPort`, or any port that hands over bytes in blocks.
11 *
12 * Usage:
13 * @code
14 * sbl::midi::drain<sbl::driver::Uart<1>>(parser); // DIN/TRS MIDI
15 * sbl::midi::drain<sbl::usb::MidiPort>(parser); // USB MIDI
16 * @endcode
17 *
18 * The transport is the caller's: this header includes no hardware or USB
19 * code. Non-blocking: it returns once the source is empty.
20 */
21
22#ifndef SBL_PROTOCOL_MIDI_DRAIN_HPP_
23#define SBL_PROTOCOL_MIDI_DRAIN_HPP_
24
25#include <cstdint>
26#include <type_traits>
27#include <utility>
28
30
31namespace sbl::midi {
32
33/** Bytes read per block from a block source. */
34inline constexpr uint32_t DRAIN_CHUNK = 64;
35
36namespace detail {
37
38template<typename T, typename = void>
39struct reads_bytes : std::false_type {};
40template<typename T>
41struct reads_bytes<T, std::void_t<decltype(T::available()), decltype(T::read_byte())>> : std::true_type {};
42
43template<typename T, typename = void>
44struct reads_blocks : std::false_type {};
45template<typename T>
46struct reads_blocks<T, std::void_t<decltype(T::read(std::declval<uint8_t*>(), std::declval<uint32_t>()))>>
47 : std::true_type {};
48
49} // namespace detail
50
51/**
52 * @brief Push everything `Source` has waiting into `parser`, copying it to a thru
53 *
54 * `thru(const uint8_t* bytes, uint32_t count)` is given every run of bytes
55 * as it is read, before the parser has them, to copy to another port.
56 *
57 * @tparam Source A byte source in either shape above; block reads win if both exist
58 * @tparam Thru Callable taking (const uint8_t*, uint32_t)
59 * @return Bytes pushed
60 * @context ISR-safe only if the source is: a UART ring buffer is, a USB stack may not be.
61 */
62template<typename Source, typename Thru>
63inline uint32_t drain(Parser& parser, Thru&& thru) {
64 static_assert(detail::reads_blocks<Source>::value || detail::reads_bytes<Source>::value,
65 "SBL ERROR: MIDI source needs read(uint8_t*, uint32_t) or available() and read_byte()");
66 uint32_t total = 0;
67 if constexpr (detail::reads_blocks<Source>::value) {
68 uint8_t buf[DRAIN_CHUNK];
69 uint32_t n = 0;
70 while ((n = Source::read(buf, DRAIN_CHUNK)) > 0) {
71 thru(static_cast<const uint8_t*>(buf), n);
72 parser.push(buf, static_cast<uint16_t>(n));
73 total += n;
74 }
75 } else {
76 while (Source::available()) {
77 const uint8_t byte = Source::read_byte();
78 thru(&byte, uint32_t{1});
79 parser.push(byte);
80 ++total;
81 }
82 }
83 return total;
84}
85
86/**
87 * @brief Push everything `Source` has waiting into `parser`
88 *
89 * @tparam Source A byte source in either shape above; block reads win if both exist
90 * @return Bytes pushed
91 * @context ISR-safe only if the source is: a UART ring buffer is, a USB stack may not be.
92 */
93template<typename Source>
94inline uint32_t drain(Parser& parser) {
95 return drain<Source>(parser, [](const uint8_t*, uint32_t) {});
96}
97
98} // namespace sbl::midi
99
100#endif // SBL_PROTOCOL_MIDI_DRAIN_HPP_
MIDI byte stream parser - state machine with running status.
Definition parser.hpp:41
void push(uint8_t byte)
Feed one byte from UART RX.
Definition parser.hpp:65
MIDI parser, encoder, SMF and sequencer.
Definition drain.hpp:31
uint32_t drain(Parser &parser, Thru &&thru)
Push everything Source has waiting into parser, copying it to a thru.
Definition drain.hpp:63
constexpr uint32_t DRAIN_CHUNK
Definition drain.hpp:34
MIDI byte stream parser.