ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
uart_component.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <cstring>
7#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
10#ifdef USE_UART_DEBUGGER
12#endif
13
14namespace esphome {
15namespace uart {
16
22
23#ifdef USE_UART_DEBUGGER
29#endif
30
31const LogString *parity_to_str(UARTParityOptions parity);
32
34 public:
35 // Writes an array of bytes to the UART bus.
36 // @param data A vector of bytes to be written.
37 void write_array(const std::vector<uint8_t> &data) { this->write_array(&data[0], data.size()); }
38
39 // Writes a single byte to the UART bus.
40 // @param data The byte to be written.
41 void write_byte(uint8_t data) { this->write_array(&data, 1); };
42
43 // Writes a null-terminated string to the UART bus.
44 // @param str Pointer to the null-terminated string.
45 void write_str(const char *str) {
46 const auto *data = reinterpret_cast<const uint8_t *>(str);
47 this->write_array(data, strlen(str));
48 };
49
50 // Pure virtual method to write an array of bytes to the UART bus.
51 // @param data Pointer to the array of bytes.
52 // @param len Length of the array.
53 virtual void write_array(const uint8_t *data, size_t len) = 0;
54
55 // Reads a single byte from the UART bus.
56 // @param data Pointer to the byte where the read data will be stored.
57 // @return True if a byte was successfully read, false otherwise.
58 bool read_byte(uint8_t *data) { return this->read_array(data, 1); };
59
60 // Pure virtual method to peek the next byte in the UART buffer without removing it.
61 // @param data Pointer to the byte where the peeked data will be stored.
62 // @return True if a byte is available to peek, false otherwise.
63 virtual bool peek_byte(uint8_t *data) = 0;
64
65 // Pure virtual method to read an array of bytes from the UART bus.
66 // @param data Pointer to the array where the read data will be stored.
67 // @param len Number of bytes to read.
68 // @return True if the specified number of bytes were successfully read, false otherwise.
69 virtual bool read_array(uint8_t *data, size_t len) = 0;
70
71 // Pure virtual method to return the number of bytes available for reading.
72 // @return Number of available bytes.
73 virtual int available() = 0;
74
75 // Pure virtual method to block until all bytes have been written to the UART bus.
76 virtual void flush() = 0;
77
78 // Sets the TX (transmit) pin for the UART bus.
79 // @param tx_pin Pointer to the internal GPIO pin used for transmission.
80 void set_tx_pin(InternalGPIOPin *tx_pin) { this->tx_pin_ = tx_pin; }
81
82 // Sets the RX (receive) pin for the UART bus.
83 // @param rx_pin Pointer to the internal GPIO pin used for reception.
84 void set_rx_pin(InternalGPIOPin *rx_pin) { this->rx_pin_ = rx_pin; }
85
86 // Sets the flow control pin for the UART bus.
87 // @param flow_control_pin Pointer to the internal GPIO pin used for flow control.
88 void set_flow_control_pin(InternalGPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; }
89
90 // Sets the size of the RX buffer.
91 // @param rx_buffer_size Size of the RX buffer in bytes.
92 void set_rx_buffer_size(size_t rx_buffer_size) { this->rx_buffer_size_ = rx_buffer_size; }
93
94 // Gets the size of the RX buffer.
95 // @return Size of the RX buffer in bytes.
96 size_t get_rx_buffer_size() { return this->rx_buffer_size_; }
97
98 // Sets the RX FIFO full interrupt threshold.
99 // @param rx_full_threshold RX full interrupt threshold in bytes.
100 virtual void set_rx_full_threshold(size_t rx_full_threshold) {}
101
102 // Sets the RX FIFO full interrupt threshold.
103 // @param time RX full interrupt threshold in ms.
104 void set_rx_full_threshold_ms(uint8_t time);
105
106 // Gets the RX FIFO full interrupt threshold.
107 // @return RX full interrupt threshold in bytes.
108 size_t get_rx_full_threshold() { return this->rx_full_threshold_; }
109
110 // Sets the RX timeout interrupt threshold.
111 // @param rx_timeout RX timeout interrupt threshold (unit: time of sending one byte).
112 virtual void set_rx_timeout(size_t rx_timeout) {}
113
114 // Gets the RX timeout interrupt threshold.
115 // @return RX timeout interrupt threshold (unit: time of sending one byte).
116 size_t get_rx_timeout() { return this->rx_timeout_; }
117
118 // Sets the number of stop bits used in UART communication.
119 // @param stop_bits Number of stop bits.
120 void set_stop_bits(uint8_t stop_bits) { this->stop_bits_ = stop_bits; }
121
122 // Gets the number of stop bits used in UART communication.
123 // @return Number of stop bits.
124 uint8_t get_stop_bits() const { return this->stop_bits_; }
125
126 // Set the number of data bits used in UART communication.
127 // @param data_bits Number of data bits.
128 void set_data_bits(uint8_t data_bits) { this->data_bits_ = data_bits; }
129
130 // Get the number of data bits used in UART communication.
131 // @return Number of data bits.
132 uint8_t get_data_bits() const { return this->data_bits_; }
133
134 // Set the parity used in UART communication.
135 // @param parity Parity option.
136 void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
137
138 // Get the parity used in UART communication.
139 // @return Parity option.
140 UARTParityOptions get_parity() const { return this->parity_; }
141
142 // Set the baud rate for UART communication.
143 // @param baud_rate Baud rate in bits per second.
144 void set_baud_rate(uint32_t baud_rate) { baud_rate_ = baud_rate; }
145
146 // Get the baud rate for UART communication.
147 // @return Baud rate in bits per second.
148 uint32_t get_baud_rate() const { return baud_rate_; }
149
150#if defined(USE_ESP8266) || defined(USE_ESP32)
162 virtual void load_settings(bool dump_config){};
163
174 virtual void load_settings(){};
175#endif // USE_ESP8266 || USE_ESP32
176
177#ifdef USE_UART_DEBUGGER
178 void add_debug_callback(std::function<void(UARTDirection, uint8_t)> &&callback) {
179 this->debug_callback_.add(std::move(callback));
180 }
181#endif
182
183 protected:
184 virtual void check_logger_conflict() = 0;
185 bool check_read_timeout_(size_t len = 1);
186
192 size_t rx_timeout_{0};
193 uint32_t baud_rate_;
194 uint8_t stop_bits_;
195 uint8_t data_bits_;
197#ifdef USE_UART_DEBUGGER
199#endif
200};
201
202} // namespace uart
203} // namespace esphome
UARTParityOptions get_parity() const
void write_byte(uint8_t data)
virtual void load_settings(bool dump_config)
Load the UART settings.
bool check_read_timeout_(size_t len=1)
virtual void load_settings()
Load the UART settings.
void write_array(const std::vector< uint8_t > &data)
void set_baud_rate(uint32_t baud_rate)
void write_str(const char *str)
void set_rx_full_threshold_ms(uint8_t time)
virtual void set_rx_full_threshold(size_t rx_full_threshold)
bool read_byte(uint8_t *data)
void add_debug_callback(std::function< void(UARTDirection, uint8_t)> &&callback)
void set_tx_pin(InternalGPIOPin *tx_pin)
void set_parity(UARTParityOptions parity)
void set_flow_control_pin(InternalGPIOPin *flow_control_pin)
virtual void check_logger_conflict()=0
virtual void write_array(const uint8_t *data, size_t len)=0
void set_rx_buffer_size(size_t rx_buffer_size)
void set_stop_bits(uint8_t stop_bits)
virtual bool peek_byte(uint8_t *data)=0
void set_rx_pin(InternalGPIOPin *rx_pin)
InternalGPIOPin * flow_control_pin_
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
virtual void set_rx_timeout(size_t rx_timeout)
virtual bool read_array(uint8_t *data, size_t len)=0
void set_data_bits(uint8_t data_bits)
const LogString * parity_to_str(UARTParityOptions parity)
Definition uart.cpp:33
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:291