roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
stream_input_stream.cpp
Go to the documentation of this file.
2
3#if (defined ARDUINO)
4
5namespace roo_io {
6
7ArduinoStreamInputStream::ArduinoStreamInputStream(Stream& input)
8 : input_(input), status_(kOk) {}
9
10bool ArduinoStreamInputStream::isOpen() const { return status() == kOk; }
11
12size_t ArduinoStreamInputStream::tryRead(byte* buf, size_t count) {
13 if (!isOpen() || count == 0) return 0;
14 size_t available = input_.available();
15 if (count > available) count = available;
16 if (count == 0) return 0;
17 return input_.readBytes((char*)buf, count);
18}
19
20size_t ArduinoStreamInputStream::read(byte* buf, size_t count) {
21 if (!isOpen() || count == 0) return 0;
22 while (true) {
23 size_t available = input_.available();
24 if (count > available) count = available;
25 // Must read at least one byte.
26 if (count == 0) ++count;
27 size_t result = input_.readBytes((char*)buf, count);
28 if (result > 0) return result;
29 yield();
30 }
31}
32
33size_t ArduinoStreamInputStream::readFully(byte* buf, size_t count) {
34 if (!isOpen() || count == 0) return 0;
35 size_t total = 0;
36 while (true) {
37 size_t result = input_.readBytes((char*)buf + total, count - total);
38 total += result;
39 if (total >= count) return total;
40 yield();
41 }
42}
43
44} // namespace roo_io
45
46#endif // (defined ARDUINO)
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
@ kOk
Definition status.h:8