ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
uart.cpp
Go to the documentation of this file.
1#include "uart.h"
5#include "esphome/core/log.h"
7#include <cinttypes>
8
9namespace esphome::uart {
10
11ESPHOME_LOG_TAG(TAG, "uart");
12
13// UART parity strings indexed by UARTParityOptions enum (0-4): NONE, EVEN, ODD, MARK, SPACE
14PROGMEM_STRING_TABLE(UARTParityStrings, "NONE", "EVEN", "ODD", "MARK", "SPACE", "UNKNOWN");
15
16void UARTDevice::check_uart_settings(uint32_t baud_rate, uint8_t stop_bits, UARTParityOptions parity,
17 uint8_t data_bits) {
18 if (this->parent_->get_baud_rate() != baud_rate) {
19 ESP_LOGE(TAG, " Invalid baud_rate: Integration requested baud_rate %" PRIu32 " but you have %" PRIu32 "!",
20 baud_rate, this->parent_->get_baud_rate());
21 }
22 if (this->parent_->get_stop_bits() != stop_bits) {
23 ESP_LOGE(TAG, " Invalid stop bits: Integration requested stop_bits %u but you have %u!", stop_bits,
24 this->parent_->get_stop_bits());
25 }
26 if (this->parent_->get_data_bits() != data_bits) {
27 ESP_LOGE(TAG, " Invalid number of data bits: Integration requested %u data bits but you have %u!", data_bits,
28 this->parent_->get_data_bits());
29 }
30 if (this->parent_->get_parity() != parity) {
31 ESP_LOGE(TAG, " Invalid parity: Integration requested parity %s but you have %s!",
32 LOG_STR_ARG(parity_to_str(parity)), LOG_STR_ARG(parity_to_str(this->parent_->get_parity())));
33 }
34}
35
37 UARTComponent *parent = this->parent_;
38 uint8_t byte;
39 while (parent->available() && parent->read_byte(&byte)) {
40 }
41}
42
43const LogString *parity_to_str(UARTParityOptions parity) {
44 return UARTParityStrings::get_log_str(static_cast<uint8_t>(parity), UARTParityStrings::LAST_INDEX);
45}
46
47} // namespace esphome::uart
UARTParityOptions get_parity() const
bool read_byte(uint8_t *data)
virtual size_t available()=0
UARTComponent * parent_
Definition uart.h:134
void discard_available()
Read and drop every byte waiting in the receive buffer.
Definition uart.cpp:36
uint8_t UARTParityOptions parity
Definition uart.h:119
uint8_t UARTParityOptions uint8_t data_bits
Definition uart.h:119
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor")
const LogString * parity_to_str(UARTParityOptions parity)
Definition uart.cpp:43
PROGMEM_STRING_TABLE(UARTParityStrings, "NONE", "EVEN", "ODD", "MARK", "SPACE", "UNKNOWN")
static void uint32_t