cleanup and new build

This commit is contained in:
Daniël Koek 2024-03-25 12:12:25 +00:00
parent 5f878cb4be
commit 64c09811df
6 changed files with 109 additions and 77 deletions

View file

@ -2,15 +2,19 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import pins from esphome import pins
from esphome.components import sensor, text_sensor, uart from esphome.components import sensor, text_sensor, uart
from esphome.const import * from esphome.const import (
DEVICE_CLASS_SIGNAL_STRENGTH,
UNIT_DECIBEL_MILLIWATT,
STATE_CLASS_MEASUREMENT,
CONF_ID,
ENTITY_CATEGORY_NONE,
)
DEPENDENCIES = ["uart"] DEPENDENCIES = ["uart"]
AUTO_LOAD = ["uart", "sensor", "text_sensor"] AUTO_LOAD = ["uart", "sensor", "text_sensor"]
MULTI_CONF = True
lora_ns = cg.esphome_ns.namespace("lora") lora_ns = cg.esphome_ns.namespace("lora")
Lora = lora_ns.class_("Lora", cg.Component, uart.UARTDevice) Lora = lora_ns.class_("Lora", cg.PollingComponent, uart.UARTDevice)
LoraGPIOPin = lora_ns.class_("LoraGPIOPin", cg.GPIOPin)
CONF_PIN_AUX = "pin_aux" CONF_PIN_AUX = "pin_aux"
CONF_PIN_M0 = "pin_m0" CONF_PIN_M0 = "pin_m0"
CONF_PIN_M1 = "pin_m1" CONF_PIN_M1 = "pin_m1"
@ -40,8 +44,8 @@ CONFIG_SCHEMA = (
), ),
} }
) )
# check values every 20s .extend(cv.polling_component_schema("20s"))
.extend(cv.polling_component_schema("20s")).extend(uart.UART_DEVICE_SCHEMA) .extend(uart.UART_DEVICE_SCHEMA)
) )
@ -65,36 +69,3 @@ async def to_code(config):
if CONF_LORA_RSSI in config: if CONF_LORA_RSSI in config:
sens = await sensor.new_sensor(config[CONF_LORA_RSSI]) sens = await sensor.new_sensor(config[CONF_LORA_RSSI])
cg.add(var.set_rssi_sensor(sens)) cg.add(var.set_rssi_sensor(sens))
def validate_mode(value):
if not (value[CONF_OUTPUT]):
raise cv.Invalid("Mode must be output")
return value
Lora_PIN_SCHEMA = pins.gpio_base_schema(
LoraGPIOPin,
cv.int_range(min=0, max=17),
modes=[CONF_OUTPUT],
mode_validator=validate_mode,
invertable=True,
).extend(
{
cv.Required(CONF_LORA): cv.use_id(Lora),
}
)
@pins.PIN_SCHEMA_REGISTRY.register(CONF_LORA, Lora_PIN_SCHEMA)
async def lora_pin_to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
parent = await cg.get_variable(config[CONF_LORA])
cg.add(var.set_parent(parent))
num = config[CONF_NUMBER]
cg.add(var.set_pin(num))
cg.add(var.set_inverted(config[CONF_INVERTED]))
cg.add(var.set_flags(pins.gpio_flags_expr(config[CONF_MODE])))
return var

View file

@ -0,0 +1,40 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.const import CONF_ID, CONF_OUTPUT, CONF_NUMBER, CONF_INVERTED, CONF_MODE
from . import CONF_LORA, Lora, lora_ns
LoraGPIOPin = lora_ns.class_("LoraGPIOPin", cg.GPIOPin)
def validate_mode(value):
if not (value[CONF_OUTPUT]):
raise cv.Invalid("Mode must be output")
return value
Lora_PIN_SCHEMA = pins.gpio_base_schema(
LoraGPIOPin,
cv.int_range(min=0, max=17),
modes=[CONF_OUTPUT],
mode_validator=validate_mode,
invertable=True,
).extend(
{
cv.Required(CONF_LORA): cv.use_id(Lora),
}
)
@pins.PIN_SCHEMA_REGISTRY.register(CONF_LORA, Lora_PIN_SCHEMA)
async def lora_pin_to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
parent = await cg.get_variable(config[CONF_LORA])
cg.add(var.set_parent(parent))
num = config[CONF_NUMBER]
cg.add(var.set_pin(num))
cg.add(var.set_inverted(config[CONF_INVERTED]))
cg.add(var.set_flags(pins.gpio_flags_expr(config[CONF_MODE])))
return var

View file

@ -0,0 +1,20 @@
#include "gpio_lora.h"
namespace esphome {
namespace lora {
static const char *const TAGPin = "lora.pin";
void LoraGPIOPin::setup() { pin_mode(flags_); }
void LoraGPIOPin::pin_mode(gpio::Flags flags) {
if (flags != gpio::FLAG_OUTPUT) {
ESP_LOGD(TAGPin, "Output only supported");
}
}
bool LoraGPIOPin::digital_read() { return false; }
void LoraGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
std::string LoraGPIOPin::dump_summary() const {
char buffer[32];
snprintf(buffer, sizeof(buffer), "%u via Lora", pin_);
return buffer;
}
} // namespace lora
} // namespace esphome

View file

@ -0,0 +1,36 @@
#pragma once
#include <utility>
#include <vector>
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "lora.h"
namespace esphome {
namespace lora {
static const char *const TAG = "lora";
// pin stuff
class LoraGPIOPin : public GPIOPin {
public:
void setup() override;
void pin_mode(gpio::Flags flags) override;
bool digital_read() override;
void digital_write(bool value) override;
std::string dump_summary() const override;
void set_parent(Lora *parent) { parent_ = parent; }
void set_pin(uint8_t pin) { pin_ = pin; }
void set_inverted(bool inverted) { inverted_ = inverted; }
void set_flags(gpio::Flags flags) { flags_ = flags; }
protected:
Lora *parent_;
uint8_t pin_;
bool inverted_;
gpio::Flags flags_;
};
} // namespace lora
} // namespace esphome

View file

@ -142,6 +142,8 @@ void Lora::loop() {
std::vector<uint8_t> data; std::vector<uint8_t> data;
bool pin_data_found = false; bool pin_data_found = false;
ESP_LOGD(TAG, "Starting to check for messages"); ESP_LOGD(TAG, "Starting to check for messages");
if (!this->available())
return;
while (this->available()) { while (this->available()) {
uint8_t c; uint8_t c;
if (this->read_byte(&c)) { if (this->read_byte(&c)) {
@ -162,25 +164,9 @@ void Lora::loop() {
} }
// set the rssi // set the rssi
rssi_ = atoi(buffer.substr(buffer.length() - 1, 1).c_str()); rssi_ = atoi(buffer.substr(buffer.length() - 1, 1).c_str());
ESP_LOGD(TAG, "RSSI: %u ", rssi_);
// set the raw message // set the raw message
raw_message_ = buffer.substr(0, buffer.length() - 1); raw_message_ = buffer.substr(0, buffer.length() - 1);
} }
// pin stuff
static const char *const TAGPin = "lora.pin";
void LoraGPIOPin::setup() { pin_mode(flags_); }
void LoraGPIOPin::pin_mode(gpio::Flags flags) {
if (flags != gpio::FLAG_OUTPUT) {
ESP_LOGD(TAGPin, "Output only supported");
}
}
bool LoraGPIOPin::digital_read() { return false; }
void LoraGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
std::string LoraGPIOPin::dump_summary() const {
char buffer[32];
snprintf(buffer, sizeof(buffer), "%u via Lora", pin_);
return buffer;
}
} // namespace lora } // namespace lora
} // namespace esphome } // namespace esphome

View file

@ -65,26 +65,5 @@ class Lora : public PollingComponent, public uart::UARTDevice {
GPIOPin *pin_m1{nullptr}; GPIOPin *pin_m1{nullptr};
}; };
// pin stuff
class LoraGPIOPin : public GPIOPin {
public:
void setup() override;
void pin_mode(gpio::Flags flags) override;
bool digital_read() override;
void digital_write(bool value) override;
std::string dump_summary() const override;
void set_parent(Lora *parent) { parent_ = parent; }
void set_pin(uint8_t pin) { pin_ = pin; }
void set_inverted(bool inverted) { inverted_ = inverted; }
void set_flags(gpio::Flags flags) { flags_ = flags; }
protected:
Lora *parent_;
uint8_t pin_;
bool inverted_;
gpio::Flags flags_;
};
} // namespace lora } // namespace lora
} // namespace esphome } // namespace esphome