mirror of
https://github.com/esphome/esphome.git
synced 2025-03-15 15:18:16 +00:00
uart: add drain() API
it would allow to empty RX buffer when necessary Signed-off-by: Igor Mammedov <imammedo@redhat.com>
This commit is contained in:
parent
a783637a7a
commit
6bdd0e7f6d
@ -98,6 +98,14 @@ void UARTComponent::flush() {
|
|||||||
ESP_LOGVV(TAG, " Flushing...");
|
ESP_LOGVV(TAG, " Flushing...");
|
||||||
this->hw_serial_->flush();
|
this->hw_serial_->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UARTComponent::drain() {
|
||||||
|
for (int i = 0; this->available(); i++) {
|
||||||
|
uint8_t junk;
|
||||||
|
this->read_byte(&junk);
|
||||||
|
ESP_LOGVV(TAG, "Draining RX[%d]: %0x", i, junk);
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif // ESP32
|
#endif // ESP32
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
@ -235,6 +243,19 @@ void UARTComponent::flush() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UARTComponent::drain() {
|
||||||
|
if (this->hw_serial_ != nullptr) {
|
||||||
|
for (int i = 0; this->available(); i++) {
|
||||||
|
uint8_t junk;
|
||||||
|
this->read_byte(&junk);
|
||||||
|
ESP_LOGVV(TAG, "Draining RX[%d]: %0x", i, junk);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this->sw_serial_->drain();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ESP8266SoftwareSerial::setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate) {
|
void ESP8266SoftwareSerial::setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate) {
|
||||||
this->bit_time_ = F_CPU / baud_rate;
|
this->bit_time_ = F_CPU / baud_rate;
|
||||||
if (tx_pin != -1) {
|
if (tx_pin != -1) {
|
||||||
@ -321,6 +342,14 @@ uint8_t ESP8266SoftwareSerial::peek_byte() {
|
|||||||
return this->rx_buffer_[this->rx_out_pos_];
|
return this->rx_buffer_[this->rx_out_pos_];
|
||||||
}
|
}
|
||||||
void ESP8266SoftwareSerial::flush() { this->rx_in_pos_ = this->rx_out_pos_ = 0; }
|
void ESP8266SoftwareSerial::flush() { this->rx_in_pos_ = this->rx_out_pos_ = 0; }
|
||||||
|
|
||||||
|
void ESP8266SoftwareSerial::drain() {
|
||||||
|
for (int i = 0; this->available(); i++) {
|
||||||
|
uint8_t junk = this->read_byte();
|
||||||
|
ESP_LOGVV(TAG, "Draining RX[%d]: %0x", i, junk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int ESP8266SoftwareSerial::available() {
|
int ESP8266SoftwareSerial::available() {
|
||||||
int avail = int(this->rx_in_pos_) - int(this->rx_out_pos_);
|
int avail = int(this->rx_in_pos_) - int(this->rx_out_pos_);
|
||||||
if (avail < 0)
|
if (avail < 0)
|
||||||
|
@ -16,6 +16,7 @@ class ESP8266SoftwareSerial {
|
|||||||
uint8_t peek_byte();
|
uint8_t peek_byte();
|
||||||
|
|
||||||
void flush();
|
void flush();
|
||||||
|
void drain();
|
||||||
|
|
||||||
void write_byte(uint8_t data);
|
void write_byte(uint8_t data);
|
||||||
|
|
||||||
@ -63,6 +64,8 @@ class UARTComponent : public Component, public Stream {
|
|||||||
|
|
||||||
void flush() override;
|
void flush() override;
|
||||||
|
|
||||||
|
void drain();
|
||||||
|
|
||||||
float get_setup_priority() const override { return setup_priority::BUS; }
|
float get_setup_priority() const override { return setup_priority::BUS; }
|
||||||
|
|
||||||
size_t write(uint8_t data) override;
|
size_t write(uint8_t data) override;
|
||||||
@ -111,6 +114,8 @@ class UARTDevice : public Stream {
|
|||||||
|
|
||||||
void flush() override { return this->parent_->flush(); }
|
void flush() override { return this->parent_->flush(); }
|
||||||
|
|
||||||
|
void drain() { return this->parent_->drain(); }
|
||||||
|
|
||||||
size_t write(uint8_t data) override { return this->parent_->write(data); }
|
size_t write(uint8_t data) override { return this->parent_->write(data); }
|
||||||
int read() override { return this->parent_->read(); }
|
int read() override { return this->parent_->read(); }
|
||||||
int peek() override { return this->parent_->peek(); }
|
int peek() override { return this->parent_->peek(); }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user