ESPHome  2024.12.2
uart.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
5 #include "esphome/core/hal.h"
6 #include "esphome/core/log.h"
7 #include "uart_component.h"
8 
9 namespace esphome {
10 namespace uart {
11 
12 class UARTDevice {
13  public:
14  UARTDevice() = default;
15  UARTDevice(UARTComponent *parent) : parent_(parent) {}
16 
17  void set_uart_parent(UARTComponent *parent) { this->parent_ = parent; }
18 
19  void write_byte(uint8_t data) { this->parent_->write_byte(data); }
20 
21  void write_array(const uint8_t *data, size_t len) { this->parent_->write_array(data, len); }
22  void write_array(const std::vector<uint8_t> &data) { this->parent_->write_array(data); }
23  template<size_t N> void write_array(const std::array<uint8_t, N> &data) {
24  this->parent_->write_array(data.data(), data.size());
25  }
26 
27  void write_str(const char *str) { this->parent_->write_str(str); }
28 
29  bool read_byte(uint8_t *data) { return this->parent_->read_byte(data); }
30  bool peek_byte(uint8_t *data) { return this->parent_->peek_byte(data); }
31 
32  bool read_array(uint8_t *data, size_t len) { return this->parent_->read_array(data, len); }
33  template<size_t N> optional<std::array<uint8_t, N>> read_array() { // NOLINT
34  std::array<uint8_t, N> res;
35  if (!this->read_array(res.data(), N)) {
36  return {};
37  }
38  return res;
39  }
40 
41  int available() { return this->parent_->available(); }
42 
43  void flush() { this->parent_->flush(); }
44 
45  // Compat APIs
46  int read() {
47  uint8_t data;
48  if (!this->read_byte(&data))
49  return -1;
50  return data;
51  }
52  size_t write(uint8_t data) {
53  this->write_byte(data);
54  return 1;
55  }
56  int peek() {
57  uint8_t data;
58  if (!this->peek_byte(&data))
59  return -1;
60  return data;
61  }
62 
64  void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits = 1,
65  UARTParityOptions parity = UART_CONFIG_PARITY_NONE, uint8_t data_bits = 8);
66 
67  protected:
69 };
70 
71 } // namespace uart
72 } // namespace esphome
void write_str(const char *str)
Definition: uart.h:27
bool peek_byte(uint8_t *data)
Definition: uart.h:30
virtual bool read_array(uint8_t *data, size_t len)=0
optional< std::array< uint8_t, N > > read_array()
Definition: uart.h:33
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
void write_byte(uint8_t data)
Definition: uart.h:19
void write_array(const std::vector< uint8_t > &data)
Definition: uart.h:22
bool read_byte(uint8_t *data)
void set_uart_parent(UARTComponent *parent)
Definition: uart.h:17
UARTComponent * parent_
Definition: uart.h:68
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition: uart.cpp:13
bool read_byte(uint8_t *data)
Definition: uart.h:29
UARTDevice(UARTComponent *parent)
Definition: uart.h:15
virtual bool peek_byte(uint8_t *data)=0
std::string size_t len
Definition: helpers.h:293
bool read_array(uint8_t *data, size_t len)
Definition: uart.h:32
void write_str(const char *str)
void write_byte(uint8_t data)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void write_array(const std::array< uint8_t, N > &data)
Definition: uart.h:23
size_t write(uint8_t data)
Definition: uart.h:52
void write_array(const std::vector< uint8_t > &data)