SensESP 3.0.0
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
stream_producer.h
Go to the documentation of this file.
1#ifndef SENSESP_SYSTEM_STREAM_PRODUCER_H
2#define SENSESP_SYSTEM_STREAM_PRODUCER_H
3
4#include "sensesp.h"
5
6#include "ReactESP.h"
8#include "sensesp_base_app.h"
9
10namespace sensesp {
11
15class StreamCharProducer : public ValueProducer<char> {
16 public:
17 StreamCharProducer(Stream* stream) : stream_{stream} {
18 read_event_ = event_loop()->onAvailable(*stream_, [this]() {
19 while (stream_->available()) {
20 char c = stream_->read();
21 this->emit(c);
22 }
23 });
24 }
25
26 protected:
27 Stream* stream_;
28 reactesp::StreamEvent* read_event_;
29};
30
34class StreamLineProducer : public ValueProducer<String> {
35 public:
36 StreamLineProducer(Stream* stream,
37 reactesp::EventLoop* event_loop = event_loop(),
38 int max_line_length = 256)
39 : stream_{stream}, max_line_length_{max_line_length} {
40 buf_ = new char[max_line_length_ + 1];
42 event_loop->onAvailable(*stream_, [this]() { this->receive_line(); });
43 }
44
45 protected:
47 int buf_pos = 0;
48 char* buf_;
49 Stream* stream_;
50 reactesp::StreamEvent* read_event_;
51
52 void receive_line() {
53 while (stream_->available()) {
54 char c = stream_->read();
55 if (c == '\n') {
56 // Include the newline character in the output
57 buf_[buf_pos++] = c;
58 buf_[buf_pos] = '\0';
59 this->emit(buf_);
60 buf_pos = 0;
61 } else {
62 buf_[buf_pos++] = c;
63 if (buf_pos >= max_line_length_ - 1) {
64 buf_pos = 0;
65 }
66 }
67 }
68 }
69};
70
71} // namespace sensesp
72
73#endif // SENSESP_SRC_SENSESP_SYSTEM_STREAM_PRODUCER_H_
ValueProducer that reads from a Stream and produces each character.
reactesp::StreamEvent * read_event_
StreamCharProducer(Stream *stream)
ValueProducer that reads from a Stream and produces a full line.
reactesp::StreamEvent * read_event_
StreamLineProducer(Stream *stream, reactesp::EventLoop *event_loop=event_loop(), int max_line_length=256)
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void emit(const char &new_value)
reactesp::EventLoop * event_loop()
Definition sensesp.cpp:7