ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
uart_virtual.h
Go to the documentation of this file.
1#pragma once
2
3#include "uart_component.h"
5
6namespace esphome::uart {
7
10template<size_t RX_BUFFER_SIZE> class VirtualUARTComponent : public UARTComponent {
11 public:
12 VirtualUARTComponent() { this->rx_buffer_size_ = RX_BUFFER_SIZE; }
13
15 bool inject_rx(const uint8_t *data, size_t len) {
16 if (len > this->rx_.free_space())
17 return false;
18 this->rx_.push(data, len);
19 return true;
20 }
21
22 bool peek_byte(uint8_t *data) override {
23 if (this->rx_.empty())
24 return false;
25 *data = this->rx_.front();
26 return true;
27 }
28 bool read_array(uint8_t *data, size_t len) override {
29 if (this->rx_.size() < len)
30 return false;
31 this->rx_.pop(data, len);
32#ifdef USE_UART_DEBUGGER
33 for (size_t i = 0; i < len; i++)
34 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
35#endif
36 return true;
37 }
38 size_t available() override { return this->rx_.size(); }
39#if defined(USE_ESP8266) || defined(USE_ESP32) || defined(USE_ZEPHYR)
41 // Nothing is clocked, so there is nothing to apply.
42 void load_settings(bool dump_config) override {}
43#endif
44
45 protected:
46 void check_logger_conflict() override {}
48 void debug_tx_(const uint8_t *data, size_t len) {
49#ifdef USE_UART_DEBUGGER
50 for (size_t i = 0; i < len; i++)
51 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
52#endif
53 }
54
56};
57
58} // namespace esphome::uart
Fixed-size circular buffer with FIFO semantics and iteration support.
Definition helpers.h:468
index_type free_space() const
Definition helpers.h:563
bool push(const T &value)
Definition helpers.h:502
index_type size() const
Definition helpers.h:561
void load_settings()
Load the UART settings.
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
UART without a wire: the derived class writes and feeds reads with inject_rx().
void load_settings(bool dump_config) override
bool read_array(uint8_t *data, size_t len) override
StaticRingBuffer< uint8_t, RX_BUFFER_SIZE > rx_
bool peek_byte(uint8_t *data) override
void debug_tx_(const uint8_t *data, size_t len)
For write_array() of the derived class: hands the written bytes to a configured UART debugger.
bool inject_rx(const uint8_t *data, size_t len)
Adds one whole block to the RX ring; false, and nothing kept, when it does not fit.
const void size_t len
Definition hal.h:68