Skip to content

Commit 3c3e46b

Browse files
author
Samuel Sieb
committed
handle lines ending with only returns
1 parent 836298f commit 3c3e46b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

components/serial/text_sensor/serial_text_sensor.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,27 @@ void SerialTextSensor::loop() {
1515
}
1616

1717
void SerialTextSensor::handle_char_(uint8_t c) {
18-
if (c == '\r')
18+
if (c == '\r') {
19+
this->have_return_ = true;
20+
this->publish_();
1921
return;
22+
}
2023
if (c == '\n') {
21-
std::string s(this->rx_message_.begin(), this->rx_message_.end());
22-
this->publish_state(s);
23-
this->rx_message_.clear();
24+
if (!have_return_)
25+
this->publish_();
26+
this->have_return_ = false;
2427
return;
2528
}
29+
this->have_return_ = false;
2630
this->rx_message_.push_back(c);
2731
}
2832

33+
void SerialTextSensor::publish_() {
34+
std::string s(this->rx_message_.begin(), this->rx_message_.end());
35+
this->publish_state(s);
36+
this->rx_message_.clear();
37+
}
38+
2939
void SerialTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Serial Sensor", this); }
3040

3141
} // namespace serial

components/serial/text_sensor/serial_text_sensor.h

+2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class SerialTextSensor : public Component, public text_sensor::TextSensor, publ
1515

1616
protected:
1717
void handle_char_(uint8_t c);
18+
void publish_();
1819
std::vector<uint8_t> rx_message_;
20+
bool have_return_{false};
1921
};
2022

2123
} // namespace serial

0 commit comments

Comments
 (0)