add uart_mitm component
This commit is contained in:
11
components/uart_mitm/README.md
Normal file
11
components/uart_mitm/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# component that acts as a mitm for two uart ports
|
||||
|
||||
Two configured uarts are required.
|
||||
|
||||
Example:
|
||||
```yaml
|
||||
uart_mitm:
|
||||
uart1: uart1
|
||||
uart2: uart2
|
||||
```
|
||||
|
||||
29
components/uart_mitm/__init__.py
Normal file
29
components/uart_mitm/__init__.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import uart
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
DEPENDENCIES = ['uart']
|
||||
|
||||
serial_ns = cg.esphome_ns.namespace('serial')
|
||||
|
||||
UARTMITM = serial_ns.class_('UARTMITM', cg.Component)
|
||||
|
||||
CONF_UART1 = "uart1"
|
||||
CONF_UART2 = "uart2"
|
||||
|
||||
CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(UARTMITM),
|
||||
cv.Required(CONF_UART1): cv.use_id(uart.UARTComponent),
|
||||
cv.Required(CONF_UART2): cv.use_id(uart.UARTComponent),
|
||||
})
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
uart1 = await cg.get_variable(config[CONF_UART1])
|
||||
cg.add(var.set_uart1(uart1))
|
||||
uart2 = await cg.get_variable(config[CONF_UART2])
|
||||
cg.add(var.set_uart2(uart2))
|
||||
24
components/uart_mitm/uart_mitm.cpp
Normal file
24
components/uart_mitm/uart_mitm.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "uart_mitm.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace serial {
|
||||
|
||||
static const char *const TAG = "uart_mitm";
|
||||
|
||||
void UARTMITM::loop() {
|
||||
uint8_t c;
|
||||
while (this->uart1_->available()) {
|
||||
this->uart1_->read_byte(&c);
|
||||
this->uart2_->write_byte(c);
|
||||
}
|
||||
while (this->uart2_->available()) {
|
||||
this->uart2_->read_byte(&c);
|
||||
this->uart1_->write_byte(c);
|
||||
}
|
||||
}
|
||||
|
||||
void UARTMITM::dump_config() { ESP_LOGCONFIG(TAG, "UART MITM"); }
|
||||
|
||||
} // namespace serial
|
||||
} // namespace esphome
|
||||
23
components/uart_mitm/uart_mitm.h
Normal file
23
components/uart_mitm/uart_mitm.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace serial {
|
||||
|
||||
class UARTMITM : public Component {
|
||||
public:
|
||||
float get_setup_priority() const override { return setup_priority::LATE; }
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
void set_uart1(uart::UARTComponent *uart) { this->uart1_ = uart; }
|
||||
void set_uart2(uart::UARTComponent *uart) { this->uart2_ = uart; }
|
||||
|
||||
protected:
|
||||
uart::UARTComponent *uart1_;
|
||||
uart::UARTComponent *uart2_;
|
||||
};
|
||||
|
||||
} // namespace serial
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user