roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
uart_input_stream.h
Go to the documentation of this file.
1#pragma once
2
3#if (defined ESP_PLATFORM || defined ROO_TESTING)
4
5#include "hal/uart_types.h"
7
8namespace roo_io {
9
10/// `InputStream` adapter backed by ESP32 UART (esp-idf APIs).
11///
12/// Initialize UART before use. Otherwise reads fail immediately.
13class Esp32UartInputStream : public InputStream {
14 public:
15 /// Creates adapter over ESP32 `uart_port_t`.
16 ///
17 /// @param uart_num UART port number.
19
20 /// Non-blocking read.
21 ///
22 /// Updates status.
23 ///
24 /// @return Number of bytes read.
25 size_t tryRead(roo::byte* buf, size_t count) override;
26
27 /// Potentially blocking read.
28 ///
29 /// Updates status.
30 ///
31 /// @return Number of bytes read.
32 size_t read(roo::byte* buf, size_t count) override;
33
34 /// Reads exactly `count` bytes unless the UART API call fails.
35 ///
36 /// Updates status.
37 ///
38 /// @return Number of bytes read.
39 size_t readFully(roo::byte* buf, size_t count) override;
40
41 /// Returns whether stream is considered open.
42 ///
43 /// @return `true` when `status()` is `kOk`.
44 bool isOpen() const override { return status_ == roo_io::kOk; }
45
46 /// Closes adapter by setting status to `kClosed`.
47 ///
48 /// Updates status.
49 void close() override { status_ = roo_io::kClosed; }
50
51 /// Returns current status.
52 ///
53 /// @return Current status value.
54 roo_io::Status status() const override { return status_; }
55
56 private:
58 mutable roo_io::Status status_;
59};
60
61} // namespace roo_io
62
63#endif // (defined ESP_PLATFORM || defined ROO_TESTING)
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
size_t count
Definition compare.h:45
Status
Definition status.h:7
@ kOk
Definition status.h:8
@ kClosed
Definition status.h:10