Extend 'uart:' with 'invert:' for esp32 (#1586)

This commit is contained in:
needspeed 2021-03-06 14:25:07 +01:00 committed by GitHub
parent e62bf333a2
commit 0f151a8f6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 2 deletions

View file

@ -2,7 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import pins, automation from esphome import pins, automation
from esphome.const import CONF_BAUD_RATE, CONF_ID, CONF_RX_PIN, CONF_TX_PIN, CONF_UART_ID, \ from esphome.const import CONF_BAUD_RATE, CONF_ID, CONF_RX_PIN, CONF_TX_PIN, CONF_UART_ID, \
CONF_DATA, CONF_RX_BUFFER_SIZE CONF_DATA, CONF_RX_BUFFER_SIZE, CONF_INVERT
from esphome.core import CORE, coroutine from esphome.core import CORE, coroutine
CODEOWNERS = ['@esphome/core'] CODEOWNERS = ['@esphome/core']
@ -47,6 +47,8 @@ CONFIG_SCHEMA = cv.All(cv.Schema({
cv.Optional(CONF_TX_PIN): pins.output_pin, cv.Optional(CONF_TX_PIN): pins.output_pin,
cv.Optional(CONF_RX_PIN): validate_rx_pin, cv.Optional(CONF_RX_PIN): validate_rx_pin,
cv.Optional(CONF_RX_BUFFER_SIZE, default=256): cv.validate_bytes, cv.Optional(CONF_RX_BUFFER_SIZE, default=256): cv.validate_bytes,
cv.SplitDefault(CONF_INVERT, esp32=False): cv.All(cv.only_on_esp32,
cv.boolean),
cv.Optional(CONF_STOP_BITS, default=1): cv.one_of(1, 2, int=True), cv.Optional(CONF_STOP_BITS, default=1): cv.one_of(1, 2, int=True),
cv.Optional(CONF_DATA_BITS, default=8): cv.int_range(min=5, max=8), cv.Optional(CONF_DATA_BITS, default=8): cv.int_range(min=5, max=8),
cv.Optional(CONF_PARITY, default="NONE"): cv.enum(UART_PARITY_OPTIONS, upper=True) cv.Optional(CONF_PARITY, default="NONE"): cv.enum(UART_PARITY_OPTIONS, upper=True)
@ -65,6 +67,8 @@ def to_code(config):
if CONF_RX_PIN in config: if CONF_RX_PIN in config:
cg.add(var.set_rx_pin(config[CONF_RX_PIN])) cg.add(var.set_rx_pin(config[CONF_RX_PIN]))
cg.add(var.set_rx_buffer_size(config[CONF_RX_BUFFER_SIZE])) cg.add(var.set_rx_buffer_size(config[CONF_RX_BUFFER_SIZE]))
if CONF_INVERT in config:
cg.add(var.set_invert(config[CONF_INVERT]))
cg.add(var.set_stop_bits(config[CONF_STOP_BITS])) cg.add(var.set_stop_bits(config[CONF_STOP_BITS]))
cg.add(var.set_data_bits(config[CONF_DATA_BITS])) cg.add(var.set_data_bits(config[CONF_DATA_BITS]))
cg.add(var.set_parity(config[CONF_PARITY])) cg.add(var.set_parity(config[CONF_PARITY]))

View file

@ -90,6 +90,9 @@ class UARTComponent : public Component, public Stream {
void set_tx_pin(uint8_t tx_pin) { this->tx_pin_ = tx_pin; } void set_tx_pin(uint8_t tx_pin) { this->tx_pin_ = tx_pin; }
void set_rx_pin(uint8_t rx_pin) { this->rx_pin_ = rx_pin; } void set_rx_pin(uint8_t rx_pin) { this->rx_pin_ = rx_pin; }
void set_rx_buffer_size(size_t rx_buffer_size) { this->rx_buffer_size_ = rx_buffer_size; } void set_rx_buffer_size(size_t rx_buffer_size) { this->rx_buffer_size_ = rx_buffer_size; }
#ifdef ARDUINO_ARCH_ESP32
void set_invert(bool invert) { this->invert_ = invert; }
#endif
void set_stop_bits(uint8_t stop_bits) { this->stop_bits_ = stop_bits; } void set_stop_bits(uint8_t stop_bits) { this->stop_bits_ = stop_bits; }
void set_data_bits(uint8_t data_bits) { this->data_bits_ = data_bits; } void set_data_bits(uint8_t data_bits) { this->data_bits_ = data_bits; }
void set_parity(UARTParityOptions parity) { this->parity_ = parity; } void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
@ -106,6 +109,9 @@ class UARTComponent : public Component, public Stream {
optional<uint8_t> tx_pin_; optional<uint8_t> tx_pin_;
optional<uint8_t> rx_pin_; optional<uint8_t> rx_pin_;
size_t rx_buffer_size_; size_t rx_buffer_size_;
#ifdef ARDUINO_ARCH_ESP32
bool invert_;
#endif
uint32_t baud_rate_; uint32_t baud_rate_;
uint8_t stop_bits_; uint8_t stop_bits_;
uint8_t data_bits_; uint8_t data_bits_;

View file

@ -80,7 +80,7 @@ void UARTComponent::setup() {
} }
int8_t tx = this->tx_pin_.has_value() ? *this->tx_pin_ : -1; int8_t tx = this->tx_pin_.has_value() ? *this->tx_pin_ : -1;
int8_t rx = this->rx_pin_.has_value() ? *this->rx_pin_ : -1; int8_t rx = this->rx_pin_.has_value() ? *this->rx_pin_ : -1;
this->hw_serial_->begin(this->baud_rate_, get_config(), rx, tx); this->hw_serial_->begin(this->baud_rate_, get_config(), rx, tx, this->invert_);
this->hw_serial_->setRxBufferSize(this->rx_buffer_size_); this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
} }

View file

@ -169,6 +169,7 @@ uart:
data_bits: 8 data_bits: 8
stop_bits: 1 stop_bits: 1
rx_buffer_size: 512 rx_buffer_size: 512
invert: false
- id: adalight_uart - id: adalight_uart
tx_pin: GPIO25 tx_pin: GPIO25