ESPHome  2024.7.1
uart_component.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <cstring>
5 #include "esphome/core/defines.h"
7 #include "esphome/core/hal.h"
8 #include "esphome/core/log.h"
9 #ifdef USE_UART_DEBUGGER
11 #endif
12 
13 namespace esphome {
14 namespace uart {
15 
20 };
21 
22 #ifdef USE_UART_DEBUGGER
27 };
28 #endif
29 
30 const LogString *parity_to_str(UARTParityOptions parity);
31 
33  public:
34  // Writes an array of bytes to the UART bus.
35  // @param data A vector of bytes to be written.
36  void write_array(const std::vector<uint8_t> &data) { this->write_array(&data[0], data.size()); }
37 
38  // Writes a single byte to the UART bus.
39  // @param data The byte to be written.
40  void write_byte(uint8_t data) { this->write_array(&data, 1); };
41 
42  // Writes a null-terminated string to the UART bus.
43  // @param str Pointer to the null-terminated string.
44  void write_str(const char *str) {
45  const auto *data = reinterpret_cast<const uint8_t *>(str);
46  this->write_array(data, strlen(str));
47  };
48 
49  // Pure virtual method to write an array of bytes to the UART bus.
50  // @param data Pointer to the array of bytes.
51  // @param len Length of the array.
52  virtual void write_array(const uint8_t *data, size_t len) = 0;
53 
54  // Reads a single byte from the UART bus.
55  // @param data Pointer to the byte where the read data will be stored.
56  // @return True if a byte was successfully read, false otherwise.
57  bool read_byte(uint8_t *data) { return this->read_array(data, 1); };
58 
59  // Pure virtual method to peek the next byte in the UART buffer without removing it.
60  // @param data Pointer to the byte where the peeked data will be stored.
61  // @return True if a byte is available to peek, false otherwise.
62  virtual bool peek_byte(uint8_t *data) = 0;
63 
64  // Pure virtual method to read an array of bytes from the UART bus.
65  // @param data Pointer to the array where the read data will be stored.
66  // @param len Number of bytes to read.
67  // @return True if the specified number of bytes were successfully read, false otherwise.
68  virtual bool read_array(uint8_t *data, size_t len) = 0;
69 
70  // Pure virtual method to return the number of bytes available for reading.
71  // @return Number of available bytes.
72  virtual int available() = 0;
73 
74  // Pure virtual method to block until all bytes have been written to the UART bus.
75  virtual void flush() = 0;
76 
77  // Sets the TX (transmit) pin for the UART bus.
78  // @param tx_pin Pointer to the internal GPIO pin used for transmission.
79  void set_tx_pin(InternalGPIOPin *tx_pin) { this->tx_pin_ = tx_pin; }
80 
81  // Sets the RX (receive) pin for the UART bus.
82  // @param rx_pin Pointer to the internal GPIO pin used for reception.
83  void set_rx_pin(InternalGPIOPin *rx_pin) { this->rx_pin_ = rx_pin; }
84 
85  // Sets the size of the RX buffer.
86  // @param rx_buffer_size Size of the RX buffer in bytes.
87  void set_rx_buffer_size(size_t rx_buffer_size) { this->rx_buffer_size_ = rx_buffer_size; }
88 
89  // Gets the size of the RX buffer.
90  // @return Size of the RX buffer in bytes.
91  size_t get_rx_buffer_size() { return this->rx_buffer_size_; }
92 
93  // Sets the number of stop bits used in UART communication.
94  // @param stop_bits Number of stop bits.
95  void set_stop_bits(uint8_t stop_bits) { this->stop_bits_ = stop_bits; }
96 
97  // Gets the number of stop bits used in UART communication.
98  // @return Number of stop bits.
99  uint8_t get_stop_bits() const { return this->stop_bits_; }
100 
101  // Set the number of data bits used in UART communication.
102  // @param data_bits Number of data bits.
103  void set_data_bits(uint8_t data_bits) { this->data_bits_ = data_bits; }
104 
105  // Get the number of data bits used in UART communication.
106  // @return Number of data bits.
107  uint8_t get_data_bits() const { return this->data_bits_; }
108 
109  // Set the parity used in UART communication.
110  // @param parity Parity option.
111  void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
112 
113  // Get the parity used in UART communication.
114  // @return Parity option.
115  UARTParityOptions get_parity() const { return this->parity_; }
116 
117  // Set the baud rate for UART communication.
118  // @param baud_rate Baud rate in bits per second.
119  void set_baud_rate(uint32_t baud_rate) { baud_rate_ = baud_rate; }
120 
121  // Get the baud rate for UART communication.
122  // @return Baud rate in bits per second.
123  uint32_t get_baud_rate() const { return baud_rate_; }
124 
125 #if defined(USE_ESP8266) || defined(USE_ESP32)
126 
137  virtual void load_settings(bool dump_config){};
138 
149  virtual void load_settings(){};
150 #endif // USE_ESP8266 || USE_ESP32
151 
152 #ifdef USE_UART_DEBUGGER
153  void add_debug_callback(std::function<void(UARTDirection, uint8_t)> &&callback) {
154  this->debug_callback_.add(std::move(callback));
155  }
156 #endif
157 
158  protected:
159  virtual void check_logger_conflict() = 0;
160  bool check_read_timeout_(size_t len = 1);
161 
165  uint32_t baud_rate_;
166  uint8_t stop_bits_;
167  uint8_t data_bits_;
169 #ifdef USE_UART_DEBUGGER
171 #endif
172 };
173 
174 } // namespace uart
175 } // namespace esphome
virtual bool read_array(uint8_t *data, size_t len)=0
uint32_t get_baud_rate() const
void set_tx_pin(InternalGPIOPin *tx_pin)
bool read_byte(uint8_t *data)
void set_stop_bits(uint8_t stop_bits)
void set_data_bits(uint8_t data_bits)
virtual void load_settings()
Load the UART settings.
void set_parity(UARTParityOptions parity)
virtual void load_settings(bool dump_config)
Load the UART settings.
UARTParityOptions get_parity() const
void set_baud_rate(uint32_t baud_rate)
virtual bool peek_byte(uint8_t *data)=0
std::string size_t len
Definition: helpers.h:292
void set_rx_buffer_size(size_t rx_buffer_size)
void write_str(const char *str)
void write_byte(uint8_t data)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool check_read_timeout_(size_t len=1)
void add_debug_callback(std::function< void(UARTDirection, uint8_t)> &&callback)
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
virtual void check_logger_conflict()=0
void set_rx_pin(InternalGPIOPin *rx_pin)
const LogString * parity_to_str(UARTParityOptions parity)
Definition: uart.cpp:33
void write_array(const std::vector< uint8_t > &data)