From 52a003edc8aa31ce608913027933a63c1a19a381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Koek?= Date: Wed, 31 Jan 2024 15:39:08 +0000 Subject: [PATCH] Loads more changes --- .../components/ebyte_lora_e220/__init__.py | 55 ++ .../ebyte_lora_e220/ebyte_lora_e220.h | 45 +- .../components/ebyte_lora_e220/lora_e220.cpp | 488 ++++-------------- .../components/ebyte_lora_e220/lora_e220.h | 207 +++----- .../components/ebyte_lora_e220/state_naming.h | 31 +- 5 files changed, 253 insertions(+), 573 deletions(-) diff --git a/esphome/components/ebyte_lora_e220/__init__.py b/esphome/components/ebyte_lora_e220/__init__.py index e69de29bb2..a01c4d66ba 100644 --- a/esphome/components/ebyte_lora_e220/__init__.py +++ b/esphome/components/ebyte_lora_e220/__init__.py @@ -0,0 +1,55 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import sensor, binary_sensor, text_sensor, uart +from esphome.const import * +ebyte_lora_e220_ns = cg.esphome_ns.namespace('ebyte_lora_e220') +EbyteLoraE220 = ebyte_lora_e220_ns.class_('EbyteLoraE220', cg.PollingComponent) + +DEPENDENCIES = ['uart'] +AUTO_LOAD = ['uart', 'sensor', 'text_sensor', 'binary_sensor'] + +CONF_PIN_AUX = "pin_aux" +CONF_PIN_M0 = "pin_m0" +CONF_PIN_M1 = "pin_m1" +CONF_LORA_STATUS = "lora_status" +CONF_LORA_MESSAGE = "lora_message" +CONF_LORA_RSSI = "lora_rssi" + +CONFIG_SCHEMA = cv.Schema({ + cv.GenerateID(): cv.declare_id(EbyteLoraE220), + cv.Required(CONF_PIN_AUX): pins.gpio_input_pin_schema, + cv.Required(CONF_PIN_M0): pins.gpio_output_pin_schema, + cv.Required(CONF_PIN_M1): pins.gpio_output_pin_schema, + cv.Optional(CONF_LORA_MESSAGE): text_sensor.text_sensor_schema(entity_category=ENTITY_CATEGORY_NONE,), + cv.Optional(CONF_LORA_STATUS): text_sensor.text_sensor_schema(entity_category=ENTITY_CATEGORY_DIAGNOSTIC,), + cv.Optional(CONF_LORA_RSSI): + sensor.sensor_schema(device_class=DEVICE_CLASS_SIGNAL_STRENGTH,unit_of_measurement=UNIT_DECIBEL_MILLIWATT,accuracy_decimals=0,state_class=STATE_CLASS_MEASUREMENT).extend(), + +}).extend(cv.polling_component_schema('60s')).extend(uart.UART_DEVICE_SCHEMA) + +def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + yield cg.register_component(var, config) + yield uart.register_uart_device(var, config) + + p = await cg.gpio_pin_expression(config[CONF_PIN_AUX]) + cg.add(var.set_pin_aux(p)) + + p = await cg.gpio_pin_expression(config[CONF_PIN_M0]) + cg.add(var.set_pin_m0(p)) + p = await cg.gpio_pin_expression(config[CONF_PIN_M1]) + cg.add(var.set_pin_m1(p)) + + if CONF_LORA_STATUS in config: + sens = await text_sensor.new_text_sensor(config[CONF_LORA_STATUS]) + cg.add(var.set_status_sensor(sens)) + + if CONF_LORA_MESSAGE in config: + sens = await text_sensor.new_text_sensor(config[CONF_LORA_MESSAGE]) + cg.add(var.set_message_sensor(sens)) + + if CONF_LORA_RSSI in config: + sens = await sensor.new_sensor(config[CONF_LORA_RSSI]) + cg.add(var.set_rssi_sensor(sens)) + + diff --git a/esphome/components/ebyte_lora_e220/ebyte_lora_e220.h b/esphome/components/ebyte_lora_e220/ebyte_lora_e220.h index ae63e041ab..fe5ad29ad0 100644 --- a/esphome/components/ebyte_lora_e220/ebyte_lora_e220.h +++ b/esphome/components/ebyte_lora_e220/ebyte_lora_e220.h @@ -1,43 +1,56 @@ #pragma once +#include #include "esphome/core/component.h" +#include "esphome/components/sensor/sensor.h" #include "esphome/components/text_sensor/text_sensor.h" -#include #include "esphome/core/helpers.h" -#include "lora_e220.h" #include "esphome/components/uart/uart.h" +#include "esphome/core/log.h" +#include "lora_e220.h" namespace esphome { namespace ebyte_lora_e220 { static const char *const TAG = "ebyte_lora_e220"; -// there are 3 UART ports, we are going to be using 0, which is D6 and D7 -HardwareSerial LoraSerial(0); -LoRa_E220 e220ttl(&LoraSerial, D2, D0, D1); // SERIAL AUX M0 M1 - -class LoRaSensors : public text_sensor::TextSensor, public PollingComponent, public uart::UARTDevice { +class EbyteLoraE220 : public PollingComponent, public uart::UARTDevice { public: - LoRaSensors() : PollingComponent(4000) {} - - void setup() override { e220ttl.begin(); } - + lora_e220::LoRa_E220 e220ttl = lora_e220::LoRa_E220(this, pin_aux, pin_m0, pin_m1); // SERIAL AUX M0 M1 + void set_message_sensor(text_sensor::TextSensor *s) { message_text_sensor = s; } + void set_status_sensor(text_sensor::TextSensor *s) { status_text_sensor = s; } + void set_rssi_sensor(sensor::Sensor *s) { rssi_sensor = s; } + void set_pin_aux(GPIOPin *s) { pin_aux = s; } + void set_pin_m0(GPIOPin *s) { pin_m0 = s; } + void set_pin_m1(GPIOPin *s) { pin_m1 = s; } + void setup() override {} + void dump_config() override { ESP_LOGCONFIG(TAG, "Ebyte Lora E220"); } + void loop() override {} void update() override { // This will be called by App.loop() if (e220ttl.available() > 1) { // read the String message - ResponseContainer rc = e220ttl.receiveMessageRSSI(); + lora_e220::ResponseContainer rc = e220ttl.receiveMessageRSSI(); // Is something goes wrong print error if (rc.status.code != 1) { - this->publish_state(rc.status.getResponseDescription()); + this->status_text_sensor->publish_state(rc.status.getResponseDescription()); } else { // Print the data received - this->publish_state(rc.status.getResponseDescription()); - this->publish_state(rc.data); - this->publish_state(rc.rssi + ""); + this->status_text_sensor->publish_state(rc.status.getResponseDescription()); + this->message_text_sensor->publish_state(rc.data); + this->rssi_sensor->publish_state(rc.rssi); } } } + + protected: + std::vector buffer_; + text_sensor::TextSensor *message_text_sensor; + text_sensor::TextSensor *status_text_sensor; + sensor::Sensor *rssi_sensor; + GPIOPin *pin_aux; + GPIOPin *pin_m0; + GPIOPin *pin_m1; }; } // namespace ebyte_lora_e220 diff --git a/esphome/components/ebyte_lora_e220/lora_e220.cpp b/esphome/components/ebyte_lora_e220/lora_e220.cpp index 68a847b106..dc3e4e6668 100644 --- a/esphome/components/ebyte_lora_e220/lora_e220.cpp +++ b/esphome/components/ebyte_lora_e220/lora_e220.cpp @@ -1,150 +1,15 @@ #include "lora_e220.h" - -LoRa_E220::LoRa_E220(HardwareSerial *serial, UART_BPS_RATE bpsRate) { //, uint32_t serialConfig - this->txE220pin = txE220pin; - this->rxE220pin = rxE220pin; - -#ifdef ACTIVATE_SOFTWARE_SERIAL - this->ss = NULL; -#endif - - this->hs = serial; - - // this->serialConfig = serialConfig; - - this->bpsRate = bpsRate; -} -LoRa_E220::LoRa_E220(HardwareSerial *serial, byte auxPin, UART_BPS_RATE bpsRate) { // , uint32_t serialConfig - this->txE220pin = txE220pin; - this->rxE220pin = rxE220pin; - this->auxPin = auxPin; - -#ifdef ACTIVATE_SOFTWARE_SERIAL - this->ss = NULL; -#endif - - this->hs = serial; - - // this->serialConfig = serialConfig; - - this->bpsRate = bpsRate; -} -LoRa_E220::LoRa_E220(HardwareSerial *serial, byte auxPin, byte m0Pin, byte m1Pin, - UART_BPS_RATE bpsRate) { //, uint32_t serialConfig - this->txE220pin = txE220pin; - this->rxE220pin = rxE220pin; - +namespace esphome { +namespace lora_e220 { +LoRa_E220::LoRa_E220(esphome::uart::UARTDevice *serial, GPIOPin *auxPin, GPIOPin *m0Pin, + GPIOPin *m1Pin) { //, uint32_t serialConfig + this->serial = serial; this->auxPin = auxPin; this->m0Pin = m0Pin; this->m1Pin = m1Pin; - -#ifdef ACTIVATE_SOFTWARE_SERIAL - this->ss = NULL; -#endif - - this->hs = serial; - // this->serialConfig = serialConfig; - - this->bpsRate = bpsRate; } - -#ifdef HARDWARE_SERIAL_SELECTABLE_PIN -LoRa_E220::LoRa_E220(byte txE220pin, byte rxE220pin, HardwareSerial *serial, UART_BPS_RATE bpsRate, - uint32_t serialConfig) { - this->txE220pin = txE220pin; - this->rxE220pin = rxE220pin; - -#ifdef ACTIVATE_SOFTWARE_SERIAL - this->ss = NULL; -#endif - - this->serialConfig = serialConfig; - - this->hs = serial; - - this->bpsRate = bpsRate; -} -LoRa_E220::LoRa_E220(byte txE220pin, byte rxE220pin, HardwareSerial *serial, byte auxPin, UART_BPS_RATE bpsRate, - uint32_t serialConfig) { - this->txE220pin = txE220pin; - this->rxE220pin = rxE220pin; - this->auxPin = auxPin; - -#ifdef ACTIVATE_SOFTWARE_SERIAL - this->ss = NULL; -#endif - - this->serialConfig = serialConfig; - - this->hs = serial; - - this->bpsRate = bpsRate; -} -LoRa_E220::LoRa_E220(byte txE220pin, byte rxE220pin, HardwareSerial *serial, byte auxPin, byte m0Pin, byte m1Pin, - UART_BPS_RATE bpsRate, uint32_t serialConfig) { - this->txE220pin = txE220pin; - this->rxE220pin = rxE220pin; - - this->auxPin = auxPin; - - this->m0Pin = m0Pin; - this->m1Pin = m1Pin; - -#ifdef ACTIVATE_SOFTWARE_SERIAL - this->ss = NULL; -#endif - - this->serialConfig = serialConfig; - - this->hs = serial; - - this->bpsRate = bpsRate; -} -#endif - -#ifdef ACTIVATE_SOFTWARE_SERIAL - -LoRa_E220::LoRa_E220(SoftwareSerial *serial, UART_BPS_RATE bpsRate) { - this->txE220pin = txE220pin; - this->rxE220pin = rxE220pin; - - this->ss = serial; - this->hs = NULL; - - this->bpsRate = bpsRate; -} -LoRa_E220::LoRa_E220(SoftwareSerial *serial, byte auxPin, UART_BPS_RATE bpsRate) { - this->txE220pin = txE220pin; - this->rxE220pin = rxE220pin; - this->auxPin = auxPin; - - this->ss = serial; - this->hs = NULL; - - this->bpsRate = bpsRate; -} -LoRa_E220::LoRa_E220(SoftwareSerial *serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate) { - this->txE220pin = txE220pin; - this->rxE220pin = rxE220pin; - - this->auxPin = auxPin; - - this->m0Pin = m0Pin; - this->m1Pin = m1Pin; - - this->ss = serial; - this->hs = NULL; - - this->bpsRate = bpsRate; -} -#endif - bool LoRa_E220::begin() { - ESP_LOGD(TAG, "RX MIC ---> "); - ESP_LOGD(TAG, this->txE220pin); - ESP_LOGD(TAG, "TX MIC ---> "); - ESP_LOGD(TAG, this->rxE220pin); ESP_LOGD(TAG, "AUX ---> "); ESP_LOGD(TAG, this->auxPin); ESP_LOGD(TAG, "M0 ---> "); @@ -153,63 +18,24 @@ bool LoRa_E220::begin() { ESP_LOGD(TAG, this->m1Pin); if (this->auxPin != -1) { - pinMode(this->auxPin, INPUT); + this->m0Pin->pin_mode(gpio::FLAG_INPUT); ESP_LOGD(TAG, "Init AUX pin!"); } if (this->m0Pin != -1) { - pinMode(this->m0Pin, OUTPUT); + this->m0Pin->pin_mode(gpio::FLAG_OUTPUT); ESP_LOGD(TAG, "Init M0 pin!"); - digitalWrite(this->m0Pin, HIGH); + this->m0Pin->digital_write(true); } if (this->m1Pin != -1) { - pinMode(this->m1Pin, OUTPUT); + this->m0Pin->pin_mode(gpio::FLAG_OUTPUT); ESP_LOGD(TAG, "Init M1 pin!"); - digitalWrite(this->m1Pin, HIGH); + this->m1Pin->digital_write(true); } ESP_LOGD(TAG, "Begin ex"); - if (this->hs) { - ESP_LOGD(TAG, "Begin Hardware Serial"); -#ifdef HARDWARE_SERIAL_SELECTABLE_PIN - if (this->txE220pin != -1 && this->rxE220pin != -1) { - ESP_LOGD(TAG, "PIN SELECTED!!"); - this->serialDef.begin(*this->hs, this->bpsRate, this->serialConfig, this->txE220pin, this->rxE220pin); - } else { - this->serialDef.begin(*this->hs, this->bpsRate, this->serialConfig); - } -#endif -#ifndef HARDWARE_SERIAL_SELECTABLE_PIN - this->serialDef.begin(*this->hs, this->bpsRate); -#endif - while (!this->hs) { - ; // wait for serial port to connect. Needed for native USB - } - -#ifdef ACTIVATE_SOFTWARE_SERIAL - } else if (this->ss) { - ESP_LOGD(TAG, "Begin Software Serial"); - - this->serialDef.begin(*this->ss, this->bpsRate); - } else { - ESP_LOGD(TAG, "Begin Software Serial Pin"); - SoftwareSerial *mySerial = new SoftwareSerial( - (int) this->txE220pin, (int) this->rxE220pin); // "RX TX" // @suppress("Abstract class cannot be instantiated") - this->ss = mySerial; - - // SoftwareSerial mySerial(this->txE220pin, this->rxE220pin); - ESP_LOGD(TAG, "RX Pin: "); - ESP_LOGD(TAG, (int) this->txE220pin); - ESP_LOGD(TAG, "TX Pin: "); - ESP_LOGD(TAG, (int) this->rxE220pin); - - this->serialDef.begin(*this->ss, this->bpsRate); -#endif - } - - this->serialDef.stream->setTimeout(100); - Status status = setMode(MODE_0_NORMAL); - return status == E220_SUCCESS; + state_naming::Status status = setMode(MODE_0_NORMAL); + return status == state_naming::E220_SUCCESS; } /* @@ -220,7 +46,7 @@ a timeout is provided to avoid an infinite loop */ Status LoRa_E220::waitCompleteResponse(unsigned long timeout, unsigned int waitNoAux) { - Status result = E220_SUCCESS; + state_naming::Status result = state_naming::E220_SUCCESS; unsigned long t = millis(); @@ -232,7 +58,7 @@ Status LoRa_E220::waitCompleteResponse(unsigned long timeout, unsigned int waitN // if AUX pin was supplied and look for HIGH state // note you can omit using AUX if no pins are available, but you will have to use delay() to let module finish if (this->auxPin != -1) { - while (digitalRead(this->auxPin) == LOW) { + while (this->auxPin->digital_read() == false) { if ((millis() - t) > timeout) { result = ERR_E220_TIMEOUT; ESP_LOGD(TAG, "Timeout error!"); @@ -244,12 +70,12 @@ Status LoRa_E220::waitCompleteResponse(unsigned long timeout, unsigned int waitN // if you can't use aux pin, use 4K7 pullup with Arduino // you may need to adjust this value if transmissions fail this->managedDelay(waitNoAux); - ESP_LOGD(TAG, F("Wait no AUX pin!")); + ESP_LOGD(TAG, "Wait no AUX pin!"); } // per data sheet control after aux goes high is 2ms so delay for at least that long) this->managedDelay(20); - ESP_LOGD(TAG, F("Complete!")); + ESP_LOGD(TAG, "Complete!"); return result; } @@ -278,48 +104,13 @@ Method to indicate availability */ -// int LoRa_E220::available(unsigned long timeout) { -int LoRa_E220::available() { - // unsigned long t = millis(); - // - // // make darn sure millis() is not about to reach max data type limit and start over - // if (((unsigned long) (t + timeout)) == 0){ - // t = 0; - // } - // - // if (this->auxPin != -1) { - // if (digitalRead(this->auxPin) == HIGH){ - // return 0; - // }else{ - // while (digitalRead(this->auxPin) == LOW) { - // if ((millis() - t) > timeout){ - // ESP_LOGD(TAG, "Timeout error!"); - // return 0; - // } - // } - // ESP_LOGD(TAG, "AUX HIGH!"); - // return 2; - // } - // }else{ - return this->serialDef.stream->available(); - // } -} +int LoRa_E220::available() { return this->serial->available(); } -/* - -Method to indicate availability - -*/ - -void LoRa_E220::flush() { this->serialDef.stream->flush(); } +void LoRa_E220::flush() { this->serial->flush(); } void LoRa_E220::cleanUARTBuffer() { - // bool IsNull = true; - - while (this->available()) { - // IsNull = false; - - this->serialDef.stream->read(); + while (this->serial->available()) { + this->serial->read(); } } @@ -343,28 +134,28 @@ Status LoRa_E220::sendStruct(void *structureManaged, uint16_t size_) { Status result = E220_SUCCESS; - uint8_t len = this->serialDef.stream->write((uint8_t *) structureManaged, size_); + uint8_t len = this->serial->write((uint8_t *) structureManaged, size_); if (len != size_) { - ESP_LOGD(TAG, F("Send... len:")) + ESP_LOGD(TAG, "Send... len:") ESP_LOGD(TAG, len); - ESP_LOGD(TAG, F(" size:")) + ESP_LOGD(TAG, " size:") ESP_LOGD(TAG, size_); if (len == 0) { - result = ERR_E220_NO_RESPONSE_FROM_DEVICE; + result = state_naming::ERR_E220_NO_RESPONSE_FROM_DEVICE; } else { - result = ERR_E220_DATA_SIZE_NOT_MATCH; + result = state_naming::ERR_E220_DATA_SIZE_NOT_MATCH; } } - if (result != E220_SUCCESS) + if (result != state_naming::E220_SUCCESS) return result; result = this->waitCompleteResponse(5000, 5000); - if (result != E220_SUCCESS) + if (result != state_naming::E220_SUCCESS) return result; - ESP_LOGD(TAG, F("Clear buffer...")) + ESP_LOGD(TAG, "Clear buffer...") this->cleanUARTBuffer(); - ESP_LOGD(TAG, F("ok!")) + ESP_LOGD(TAG, "ok!") return result; } @@ -394,16 +185,16 @@ Status LoRa_E220::receiveStruct(void *structureManaged, uint16_t size_) { if (len != size_) { if (len == 0) { - result = ERR_E220_NO_RESPONSE_FROM_DEVICE; + result = state_naming::ERR_E220_NO_RESPONSE_FROM_DEVICE; } else { - result = ERR_E220_DATA_SIZE_NOT_MATCH; + result = state_naming::ERR_E220_DATA_SIZE_NOT_MATCH; } } - if (result != E220_SUCCESS) + if (result != state_naming::E220_SUCCESS) return result; result = this->waitCompleteResponse(1000); - if (result != E220_SUCCESS) + if (result != state_naming::E220_SUCCESS) return result; return result; @@ -422,35 +213,35 @@ Status LoRa_E220::setMode(MODE_TYPE mode) { this->managedDelay(40); if (this->m0Pin == -1 && this->m1Pin == -1) { - ESP_LOGD(TAG, F("The M0 and M1 pins is not set, this mean that you are connect directly the pins as you need!")) + ESP_LOGD(TAG, "The M0 and M1 pins is not set, this mean that you are connect directly the pins as you need!") } else { switch (mode) { case MODE_0_NORMAL: // Mode 0 | normal operation - digitalWrite(this->m0Pin, LOW); - digitalWrite(this->m1Pin, LOW); + this->m0Pin->digital_write(false); + this->m1Pin->digital_write(false); ESP_LOGD(TAG, "MODE NORMAL!"); break; case MODE_1_WOR_TRANSMITTER: - digitalWrite(this->m0Pin, HIGH); - digitalWrite(this->m1Pin, LOW); + this->m0Pin->digital_write(true); + this->m1Pin->digital_write(false); ESP_LOGD(TAG, "MODE WOR!"); break; case MODE_2_WOR_RECEIVER: // case MODE_2_PROGRAM: - digitalWrite(this->m0Pin, LOW); - digitalWrite(this->m1Pin, HIGH); + this->m0Pin->digital_write(false); + this->m1Pin->digital_write(true); ESP_LOGD(TAG, "MODE RECEIVING!"); break; case MODE_3_CONFIGURATION: // Mode 3 | Setting operation - digitalWrite(this->m0Pin, HIGH); - digitalWrite(this->m1Pin, HIGH); + this->m0Pin->digital_write(true); + this->m1Pin->digital_write(true); ESP_LOGD(TAG, "MODE SLEEP CONFIG!"); break; default: - return ERR_E220_INVALID_PARAM; + return state_naming::ERR_E220_INVALID_PARAM; } } // data sheet says 2ms later control is returned, let's give just a bit more time @@ -458,9 +249,9 @@ Status LoRa_E220::setMode(MODE_TYPE mode) { this->managedDelay(40); // wait until aux pin goes back low - Status res = this->waitCompleteResponse(1000); + state_naming::Status res = this->waitCompleteResponse(1000); - if (res == E220_SUCCESS) { + if (res == state_naming::E220_SUCCESS) { this->mode = mode; } @@ -471,7 +262,7 @@ MODE_TYPE LoRa_E220::getMode() { return this->mode; } bool LoRa_E220::writeProgramCommand(PROGRAM_COMMAND cmd, REGISTER_ADDRESS addr, PACKET_LENGHT pl) { uint8_t CMD[3] = {cmd, addr, pl}; - uint8_t size = this->serialDef.stream->write(CMD, 3); + uint8_t size = this->serial->write_array(CMD, 3); ESP_LOGD(TAG, size); @@ -484,7 +275,7 @@ ResponseStructContainer LoRa_E220::getConfiguration() { ResponseStructContainer rc; rc.status.code = checkUARTConfiguration(MODE_3_PROGRAM); - if (rc.status.code != E220_SUCCESS) + if (rc.status.code != state_naming::E220_SUCCESS) return rc; MODE_TYPE prevMode = this->mode; @@ -524,7 +315,7 @@ ResponseStructContainer LoRa_E220::getConfiguration() { } RESPONSE_STATUS LoRa_E220::checkUARTConfiguration(MODE_TYPE mode) { - if (mode == MODE_3_PROGRAM && this->bpsRate != UART_BPS_RATE_9600) { + if (mode == MODE_3_PROGRAM) { return ERR_E220_WRONG_UART_CONFIG; } return E220_SUCCESS; @@ -595,7 +386,6 @@ ResponseStructContainer LoRa_E220::getModuleInformation() { rc.data = malloc(sizeof(ModuleInformation)); - // struct ModuleInformation *moduleInformation = (ModuleInformation *)malloc(sizeof(ModuleInformation)); rc.status.code = this->receiveStruct((uint8_t *) rc.data, sizeof(ModuleInformation)); if (rc.status.code != E220_SUCCESS) { this->setMode(prevMode); @@ -618,55 +408,27 @@ ResponseStructContainer LoRa_E220::getModuleInformation() { } ESP_LOGD(TAG, "----------------------------------------"); - ESP_LOGD(TAG, F("HEAD: ")); + ESP_LOGD(TAG, "HEAD: "); ESP_LOGD(TAG, ((ModuleInformation *) rc.data)->COMMAND, BIN); ESP_LOGD(TAG, " "); ESP_LOGD(TAG, ((ModuleInformation *) rc.data)->STARTING_ADDRESS, DEC); ESP_LOGD(TAG, " "); ESP_LOGD(TAG, ((ModuleInformation *) rc.data)->LENGHT, HEX); - ESP_LOGD(TAG, F("Model no.: ")); + ESP_LOGD(TAG, "Model no.: "); ESP_LOGD(TAG, ((ModuleInformation *) rc.data)->model, HEX); - ESP_LOGD(TAG, F("Version : ")); + ESP_LOGD(TAG, "Version : "); ESP_LOGD(TAG, ((ModuleInformation *) rc.data)->version, HEX); - ESP_LOGD(TAG, F("Features : ")); + ESP_LOGD(TAG, "Features : "); ESP_LOGD(TAG, (ModuleInformation *) rc.data)->features, HEX); - ESP_LOGD(TAG, F("Status : ")); + ESP_LOGD(TAG, "Status : "); ESP_LOGD(TAG, rc.status.getResponseDescription()); ESP_LOGD(TAG, "----------------------------------------"); - - // if (rc.status.code!=E220_SUCCESS) return rc; - - // rc.data = moduleInformation; // malloc(sizeof (moduleInformation)); - return rc; } ResponseStatus LoRa_E220::resetModule() { - // ResponseStatus status; - // - // status.code = checkUARTConfiguration(MODE_2_PROGRAM); - // if (status.code!=E220_SUCCESS) return status; - // - // MODE_TYPE prevMode = this->mode; - // - // status.code = this->setMode(MODE_2_PROGRAM); - // if (status.code!=E220_SUCCESS) return status; - // - // this->writeProgramCommand(WRITE_RESET_MODULE); - // - // status.code = this->waitCompleteResponse(1000); - // if (status.code!=E220_SUCCESS) { - // this->setMode(prevMode); - // return status; - // } - // - // - // status.code = this->setMode(prevMode); - // if (status.code!=E220_SUCCESS) return status; - // - // return status; - ESP_LOGD(TAG, F("No information to reset module!")); + ESP_LOGD(TAG, "No information to reset module!"); ResponseStatus status; status.code = ERR_E220_NOT_IMPLEMENT; return status; @@ -678,13 +440,18 @@ ResponseContainer LoRa_E220::receiveMessageRSSI() { return LoRa_E220::receiveMes ResponseContainer LoRa_E220::receiveMessageComplete(bool rssiEnabled) { ResponseContainer rc; rc.status.code = E220_SUCCESS; - std::string tmpData = this->serialDef.stream->readstd::string(); - - ESP_LOGD(TAG, tmpData); + std::string buffer; + uint8_t data; + while (this->available() > 0) { + if (this->read_byte(&data)) { + buffer += (char) data; + } + } + ESP_LOGD(TAG, buffer); if (rssiEnabled) { - rc.rssi = tmpData.charAt(tmpData.length() - 1); - rc.data = tmpData.substd::string(0, tmpData.length() - 1); + rc.rssi = buffer.charAt(tmpData.length() - 1); + rc.data = buffer.substd::string(0, tmpData.length() - 1); } else { rc.data = tmpData; } @@ -698,19 +465,6 @@ ResponseContainer LoRa_E220::receiveMessageComplete(bool rssiEnabled) { return rc; } -ResponseContainer LoRa_E220::receiveMessageUntil(char delimiter) { - ResponseContainer rc; - rc.status.code = E220_SUCCESS; - rc.data = this->serialDef.stream->readstd::stringUntil(delimiter); - // this->cleanUARTBuffer(); - if (rc.status.code != E220_SUCCESS) { - return rc; - } - - // rc.data = message; // malloc(sizeof (moduleInformation)); - - return rc; -} ResponseContainer LoRa_E220::receiveInitialMessage(uint8_t size) { ResponseContainer rc; rc.status.code = E220_SUCCESS; @@ -765,14 +519,14 @@ ResponseStatus LoRa_E220::sendMessage(const void *message, const uint8_t size) { return status; } ResponseStatus LoRa_E220::sendMessage(const std::string message) { - ESP_LOGD(TAG, F("Send message: ")); + ESP_LOGD(TAG, "Send message: "); ESP_LOGD(TAG, message); byte size = message.length(); // sizeof(message.c_str())+1; - ESP_LOGD(TAG, F(" size: ")); + ESP_LOGD(TAG, " size: "); ESP_LOGD(TAG, size); char messageFixed[size]; memcpy(messageFixed, message.c_str(), size); - ESP_LOGD(TAG, F(" memcpy ")); + ESP_LOGD(TAG, " memcpy "); ResponseStatus status; status.code = this->sendStruct((uint8_t *) &messageFixed, size); @@ -784,36 +538,7 @@ ResponseStatus LoRa_E220::sendMessage(const std::string message) { } ResponseStatus LoRa_E220::sendFixedMessage(byte ADDH, byte ADDL, byte CHAN, const std::string message) { - // ESP_LOGD(TAG,"std::string/size: "); - // ESP_LOGD(TAG,message); - // ESP_LOGD(TAG,"/"); - byte size = message.length(); // sizeof(message.c_str())+1; - // ESP_LOGD(TAG, size); - // - // #pragma pack(push, 1) - // struct FixedStransmissionstd::string { - // byte ADDH = 0; - // byte ADDL = 0; - // byte CHAN = 0; - // char message[]; - // } fixedStransmission; - // #pragma pack(pop) - // - // fixedStransmission.ADDH = ADDH; - // fixedStransmission.ADDL = ADDL; - // fixedStransmission.CHAN = CHAN; - // char* msg = (char*)message.c_str(); - // memcpy(fixedStransmission.message, (char*)msg, size); - //// fixedStransmission.message = message; - // - // ESP_LOGD(TAG,"Message: "); - // ESP_LOGD(TAG, fixedStransmission.message); - // - // ResponseStatus status; - // status.code = this->sendStruct((uint8_t *)&fixedStransmission, sizeof(fixedStransmission)); - // if (status.code!=E220_SUCCESS) return status; - // - // return status; + byte size = message.length(); char messageFixed[size]; memcpy(messageFixed, message.c_str(), size); return this->sendFixedMessage(ADDH, ADDL, CHAN, (uint8_t *) messageFixed, size); @@ -835,40 +560,15 @@ FixedStransmission *init_stack(int m) { } ResponseStatus LoRa_E220::sendFixedMessage(byte ADDH, byte ADDL, byte CHAN, const void *message, const uint8_t size) { - // #pragma pack(push, 1) - // struct FixedStransmission { - // byte ADDH = 0; - // byte ADDL = 0; - // byte CHAN = 0; - // unsigned char message[]; - // } fixedStransmission; - // #pragma pack(pop) - ESP_LOGD(TAG, ADDH); - FixedStransmission *fixedStransmission = init_stack(size); - - // STACK *resize_stack(STACK *st, int m){ - // if (m<=st->max){ - // return st; /* Take sure do not kill old values */ - // } - // STACK *st = (STACK *)realloc(sizeof(STACK)+m*sizeof(int)); - // st->max = m; - // return st; - // } - fixedStransmission->ADDH = ADDH; fixedStransmission->ADDL = ADDL; fixedStransmission->CHAN = CHAN; - // fixedStransmission.message = &message; - memcpy(fixedStransmission->message, (unsigned char *) message, size); - ResponseStatus status; status.code = this->sendStruct((uint8_t *) fixedStransmission, size + 3); - free(fixedStransmission); - if (status.code != E220_SUCCESS) return status; @@ -884,17 +584,12 @@ ResponseStatus LoRa_E220::sendConfigurationMessage(byte ADDH, byte ADDL, byte CH PROGRAM_COMMAND programCommand) { ResponseStatus rc; - // rc.code = this->setMode(MODE_2_PROGRAM); - // if (rc.code!=E220_SUCCESS) return rc; - configuration->COMMAND = programCommand; configuration->STARTING_ADDRESS = REG_ADDRESS_CFG; configuration->LENGHT = PL_CONFIGURATION; ConfigurationMessage *fixedStransmission = init_stack_conf(sizeof(Configuration)); - // fixedStransmission.message = &message; - memcpy(fixedStransmission->message, (unsigned char *) configuration, sizeof(Configuration)); fixedStransmission->specialCommand1 = SPECIAL_WIFI_CONF_COMMAND; @@ -903,13 +598,6 @@ ResponseStatus LoRa_E220::sendConfigurationMessage(byte ADDH, byte ADDL, byte CH ESP_LOGD(TAG, sizeof(Configuration) + 2); rc = sendFixedMessage(ADDH, ADDL, CHAN, fixedStransmission, sizeof(Configuration) + 2); - // - // ResponseStatus status; - // status.code = this->sendStruct((uint8_t *)fixedStransmission, sizeof(Configuration)+5); - // if (status.code!=E220_SUCCESS) return status; - - // free(fixedStransmission); - return rc; } @@ -960,62 +648,62 @@ unsigned long LoRa_E220::decrypt(unsigned long data) { void LoRa_E220::printParameters(struct Configuration *configuration) { ESP_LOGD(TAG, "----------------------------------------"); - ESP_LOGD(TAG, F("HEAD : ")); + ESP_LOGD(TAG, "HEAD : "); ESP_LOGD(TAG, configuration->COMMAND, HEX); ESP_LOGD(TAG, " "); ESP_LOGD(TAG, configuration->STARTING_ADDRESS, HEX); ESP_LOGD(TAG, " "); ESP_LOGD(TAG, configuration->LENGHT, HEX); - ESP_LOGD(TAG, F(" ")); - ESP_LOGD(TAG, F("AddH : ")); + ESP_LOGD(TAG, " "); + ESP_LOGD(TAG, "AddH : "); ESP_LOGD(TAG, configuration->ADDH, HEX); - ESP_LOGD(TAG, F("AddL : ")); + ESP_LOGD(TAG, "AddL : "); ESP_LOGD(TAG, configuration->ADDL, HEX); - ESP_LOGD(TAG, F(" ")); - ESP_LOGD(TAG, F("Chan : ")); + ESP_LOGD(TAG, " "); + ESP_LOGD(TAG, "Chan : "); ESP_LOGD(TAG, configuration->CHAN, DEC); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->getChannelDescription()); - ESP_LOGD(TAG, F(" ")); - ESP_LOGD(TAG, F("SpeedParityBit : ")); + ESP_LOGD(TAG, " "); + ESP_LOGD(TAG, "SpeedParityBit : "); ESP_LOGD(TAG, configuration->SPED.uartParity, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->SPED.getUARTParityDescription()); - ESP_LOGD(TAG, F("SpeedUARTDatte : ")); + ESP_LOGD(TAG, "SpeedUARTDatte : "); ESP_LOGD(TAG, configuration->SPED.uartBaudRate, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->SPED.getUARTBaudRateDescription()); - ESP_LOGD(TAG, F("SpeedAirDataRate : ")); + ESP_LOGD(TAG, "SpeedAirDataRate : "); ESP_LOGD(TAG, configuration->SPED.airDataRate, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->SPED.getAirDataRateDescription()); - ESP_LOGD(TAG, F(" ")); - ESP_LOGD(TAG, F("OptionSubPacketSett: ")); + ESP_LOGD(TAG, " "); + ESP_LOGD(TAG, "OptionSubPacketSett: "); ESP_LOGD(TAG, configuration->OPTION.subPacketSetting, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->OPTION.getSubPacketSetting()); - ESP_LOGD(TAG, F("OptionTranPower : ")); + ESP_LOGD(TAG, "OptionTranPower : "); ESP_LOGD(TAG, configuration->OPTION.transmissionPower, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->OPTION.getTransmissionPowerDescription()); - ESP_LOGD(TAG, F("OptionRSSIAmbientNo: ")); + ESP_LOGD(TAG, "OptionRSSIAmbientNo: "); ESP_LOGD(TAG, configuration->OPTION.RSSIAmbientNoise, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->OPTION.getRSSIAmbientNoiseEnable()); - ESP_LOGD(TAG, F(" ")); - ESP_LOGD(TAG, F("TransModeWORPeriod : ")); + ESP_LOGD(TAG, " "); + ESP_LOGD(TAG, "TransModeWORPeriod : "); ESP_LOGD(TAG, configuration->TRANSMISSION_MODE.WORPeriod, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->TRANSMISSION_MODE.getWORPeriodByParamsDescription()); - ESP_LOGD(TAG, F("TransModeEnableLBT : ")); + ESP_LOGD(TAG, "TransModeEnableLBT : "); ESP_LOGD(TAG, configuration->TRANSMISSION_MODE.enableLBT, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->TRANSMISSION_MODE.getLBTEnableByteDescription()); - ESP_LOGD(TAG, F("TransModeEnableRSSI: ")); + ESP_LOGD(TAG, "TransModeEnableRSSI: "); ESP_LOGD(TAG, configuration->TRANSMISSION_MODE.enableRSSI, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->TRANSMISSION_MODE.getRSSIEnableByteDescription()); - ESP_LOGD(TAG, F("TransModeFixedTrans: ")); + ESP_LOGD(TAG, "TransModeFixedTrans: "); ESP_LOGD(TAG, configuration->TRANSMISSION_MODE.fixedTransmission, BIN); ESP_LOGD(TAG, " -> "); ESP_LOGD(TAG, configuration->TRANSMISSION_MODE.getFixedTransmissionDescription()); @@ -1023,3 +711,5 @@ void LoRa_E220::printParameters(struct Configuration *configuration) { ESP_LOGD(TAG, "----------------------------------------"); } #endif +} // namespace lora_e220 +} // namespace esphome diff --git a/esphome/components/ebyte_lora_e220/lora_e220.h b/esphome/components/ebyte_lora_e220/lora_e220.h index e6c551da2c..f417d782aa 100644 --- a/esphome/components/ebyte_lora_e220/lora_e220.h +++ b/esphome/components/ebyte_lora_e220/lora_e220.h @@ -1,22 +1,12 @@ -#ifndef LoRa_E220_h -#define LoRa_E220_h - -#ifdef USE_ESP32 -#define HARDWARE_SERIAL_SELECTABLE_PIN -#endif - +#pragma once #include "state_naming.h" - -#include "Arduino.h" +#include "esphome/core/component.h" +#include "esphome/components/uart/uart.h" +namespace esphome { +namespace lora_e220 { #define MAX_SIZE_TX_PACKET 200 -// Uncomment to enable printing out nice debug messages. -// #define LoRa_E220_DEBUG - -// Define where debug output will be printed. -#define DEBUG_PRINTER Serial - static const char *const TAG = "ebyte_lora_e220"; enum MODE_TYPE { MODE_0_NORMAL = 0, @@ -61,67 +51,70 @@ enum PACKET_LENGHT { PL_PID = 0x03 }; -#pragma pack(push, 1) struct Speed { uint8_t airDataRate : 3; // bit 0-2 - std::string getAirDataRateDescription() { return getAirDataRateDescriptionByParams(this->airDataRate); } + std::string getAirDataRateDescription() { return state_naming::getAirDataRateDescriptionByParams(this->airDataRate); } uint8_t uartParity : 2; // bit 3-4 - std::string getUARTParityDescription() { return getUARTParityDescriptionByParams(this->uartParity); } + std::string getUARTParityDescription() { return state_naming::getUARTParityDescriptionByParams(this->uartParity); } uint8_t uartBaudRate : 3; // bit 5-7 - std::string getUARTBaudRateDescription() { return getUARTBaudRateDescriptionByParams(this->uartBaudRate); } + std::string getUARTBaudRateDescription() { + return state_naming::getUARTBaudRateDescriptionByParams(this->uartBaudRate); + } }; struct TransmissionMode { - byte WORPeriod : 3; // bit 2,1,0 - std::string getWORPeriodByParamsDescription() { return getWORPeriodByParams(this->WORPeriod); } - byte reserved2 : 1; // bit 3 - byte enableLBT : 1; // bit 4 - std::string getLBTEnableByteDescription() { return getLBTEnableByteByParams(this->enableLBT); } - byte reserved : 1; // bit 5 + uint8_t WORPeriod : 3; // bit 2,1,0 + std::string getWORPeriodByParamsDescription() { return state_naming::getWORPeriodByParams(this->WORPeriod); } + uint8_t reserved2 : 1; // bit 3 + uint8_t enableLBT : 1; // bit 4 + std::string getLBTEnableByteDescription() { return state_naming::getLBTEnableByteByParams(this->enableLBT); } + uint8_t reserved : 1; // bit 5 - byte fixedTransmission : 1; // bit 6 + uint8_t fixedTransmission : 1; // bit 6 std::string getFixedTransmissionDescription() { - return getFixedTransmissionDescriptionByParams(this->fixedTransmission); + return state_naming::getFixedTransmissionDescriptionByParams(this->fixedTransmission); } - byte enableRSSI : 1; // bit 7 - std::string getRSSIEnableByteDescription() { return getRSSIEnableByteByParams(this->enableRSSI); } + uint8_t enableRSSI : 1; // bit 7 + std::string getRSSIEnableByteDescription() { return state_naming::getRSSIEnableByteByParams(this->enableRSSI); } }; struct Option { uint8_t transmissionPower : 2; // bit 0-1 std::string getTransmissionPowerDescription() { - return getTransmissionPowerDescriptionByParams(this->transmissionPower); + return state_naming::getTransmissionPowerDescriptionByParams(this->transmissionPower); } uint8_t reserved : 3; // bit 2-4 uint8_t RSSIAmbientNoise : 1; // bit 5 - std::string getRSSIAmbientNoiseEnable() { return getRSSIAmbientNoiseEnableByParams(this->RSSIAmbientNoise); } + std::string getRSSIAmbientNoiseEnable() { + return state_naming::getRSSIAmbientNoiseEnableByParams(this->RSSIAmbientNoise); + } uint8_t subPacketSetting : 2; // bit 6-7 - std::string getSubPacketSetting() { return getSubPacketSettingByParams(this->subPacketSetting); } + std::string getSubPacketSetting() { return state_naming::getSubPacketSettingByParams(this->subPacketSetting); } }; struct Crypt { - byte CRYPT_H = 0; - byte CRYPT_L = 0; + uint8_t CRYPT_H = 0; + uint8_t CRYPT_L = 0; }; struct Configuration { - byte COMMAND = 0; - byte STARTING_ADDRESS = 0; - byte LENGHT = 0; + uint8_t COMMAND = 0; + uint8_t STARTING_ADDRESS = 0; + uint8_t LENGHT = 0; - byte ADDH = 0; - byte ADDL = 0; + uint8_t ADDH = 0; + uint8_t ADDL = 0; struct Speed SPED; struct Option OPTION; - byte CHAN = 0; - std::string getChannelDescription() { return std::string(this->CHAN + OPERATING_FREQUENCY) + F("MHz"); } + uint8_t CHAN = 0; + std::string getChannelDescription() { return std::string(this->CHAN + OPERATING_FREQUENCY + "MHz"); } struct TransmissionMode TRANSMISSION_MODE; @@ -129,163 +122,89 @@ struct Configuration { }; struct ModuleInformation { - byte COMMAND = 0; - byte STARTING_ADDRESS = 0; - byte LENGHT = 0; + uint8_t COMMAND = 0; + uint8_t STARTING_ADDRESS = 0; + uint8_t LENGHT = 0; - byte model = 0; - byte version = 0; - byte features = 0; + uint8_t model = 0; + uint8_t version = 0; + uint8_t features = 0; }; struct ResponseStatus { - std::std::string code; - std::std::string getResponseDescription() { return getResponseDescriptionByParams(this->code); } + uint8_t code; + std::string getResponseDescription() { return state_naming::getResponseDescriptionByParams(this->code); } }; struct ResponseStructContainer { void *data; - byte rssi; + uint8_t rssi; ResponseStatus status; void close() { free(this->data); } }; struct ResponseContainer { std::string data; - byte rssi; + uint8_t rssi; ResponseStatus status; }; struct ConfigurationMessage { - byte specialCommand1 = 0xCF; - byte specialCommand2 = 0xCF; + uint8_t specialCommand1 = 0xCF; + uint8_t specialCommand2 = 0xCF; unsigned char message[]; }; -// struct FixedStransmission { -// byte ADDL = 0; -// byte ADDH = 0; -// byte CHAN = 0; -// void *message; -// }; -#pragma pack(pop) - class LoRa_E220 { public: - LoRa_E220(HardwareSerial *serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600); - LoRa_E220(HardwareSerial *serial, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600); - LoRa_E220(HardwareSerial *serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600); -#ifdef HARDWARE_SERIAL_SELECTABLE_PIN - LoRa_E220(byte txE220pin, byte rxE220pin, HardwareSerial *serial, UART_BPS_RATE bpsRate, - uint32_t serialConfig = SERIAL_8N1); - LoRa_E220(byte txE220pin, byte rxE220pin, HardwareSerial *serial, byte auxPin, UART_BPS_RATE bpsRate, - uint32_t serialConfig = SERIAL_8N1); - LoRa_E220(byte txE220pin, byte rxE220pin, HardwareSerial *serial, byte auxPin, byte m0Pin, byte m1Pin, - UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1); -#endif + LoRa_E220(uart::UARTDevice *device, GPIOPin *auxPin, GPIOPin *m0Pin, GPIOPin *m1Pin); bool begin(); - Status setMode(MODE_TYPE mode); + state_naming::Status setMode(MODE_TYPE mode); MODE_TYPE getMode(); - ResponseStructContainer getConfiguration(); ResponseStatus setConfiguration(Configuration configuration, PROGRAM_COMMAND saveType = WRITE_CFG_PWR_DWN_LOSE); - ResponseStructContainer getModuleInformation(); ResponseStatus resetModule(); - ResponseStatus sendMessage(const void *message, const uint8_t size); - ResponseContainer receiveMessageUntil(char delimiter = '\0'); ResponseStructContainer receiveMessage(const uint8_t size); ResponseStructContainer receiveMessageRSSI(const uint8_t size); - ResponseStructContainer receiveMessageComplete(const uint8_t size, bool enableRSSI); ResponseContainer receiveMessageComplete(bool enableRSSI); - ResponseStatus sendMessage(const std::string message); ResponseContainer receiveMessage(); ResponseContainer receiveMessageRSSI(); - - ResponseStatus sendFixedMessage(byte ADDH, byte ADDL, byte CHAN, const std::string message); - - ResponseStatus sendFixedMessage(byte ADDH, byte ADDL, byte CHAN, const void *message, const uint8_t size); - ResponseStatus sendBroadcastFixedMessage(byte CHAN, const void *message, const uint8_t size); - ResponseStatus sendBroadcastFixedMessage(byte CHAN, const std::string message); - + ResponseStatus sendFixedMessage(uint8_t ADDH, uint8_t ADDL, uint8_t CHAN, const std::string message); + ResponseStatus sendFixedMessage(uint8_t ADDH, uint8_t ADDL, uint8_t CHAN, const void *message, const uint8_t size); + ResponseStatus sendBroadcastFixedMessage(uint8_t CHAN, const void *message, const uint8_t size); + ResponseStatus sendBroadcastFixedMessage(uint8_t CHAN, const std::string message); ResponseContainer receiveInitialMessage(const uint8_t size); - - ResponseStatus sendConfigurationMessage(byte ADDH, byte ADDL, byte CHAN, Configuration *configuration, + ResponseStatus sendConfigurationMessage(uint8_t ADDH, uint8_t ADDL, uint8_t CHAN, Configuration *configuration, PROGRAM_COMMAND programCommand = WRITE_CFG_PWR_DWN_SAVE); - int available(); private: - HardwareSerial *hs; - bool isSoftwareSerial = true; - - int8_t txE220pin = -1; - int8_t rxE220pin = -1; - int8_t auxPin = -1; - -#ifdef HARDWARE_SERIAL_SELECTABLE_PIN - uint32_t serialConfig = SERIAL_8N1; -#endif - - int8_t m0Pin = -1; - int8_t m1Pin = -1; + uart::UARTDevice *serial; + GPIOPin *auxPin; + GPIOPin *m0Pin; + GPIOPin *m1Pin; unsigned long halfKeyloqKey = 0x06660708; unsigned long encrypt(unsigned long data); unsigned long decrypt(unsigned long data); - UART_BPS_RATE bpsRate = UART_BPS_RATE_9600; - - struct NeedsStream { - template void begin(T &t, uint32_t baud) { - ESP_LOGD(TAG, "Begin "); - t.setTimeout(500); - t.begin(baud); - stream = &t; - } - -#ifdef HARDWARE_SERIAL_SELECTABLE_PIN - template void begin(T &t, uint32_t baud, uint32_t config) { - ESP_LOGD(TAG, "Begin "); - t.setTimeout(500); - t.begin(baud, config); - stream = &t; - } - - template void begin(T &t, uint32_t baud, uint32_t config, int8_t txE220pin, int8_t rxE220pin) { - ESP_LOGD(TAG, "Begin "); - t.setTimeout(500); - t.begin(baud, config, txE220pin, rxE220pin); - stream = &t; - } -#endif - - void listen() {} - - Stream *stream; - }; - NeedsStream serialDef; - MODE_TYPE mode = MODE_0_NORMAL; void managedDelay(unsigned long timeout); - Status waitCompleteResponse(unsigned long timeout = 1000, unsigned int waitNoAux = 100); + state_naming::Status waitCompleteResponse(unsigned long timeout = 1000, unsigned int waitNoAux = 100); void flush(); void cleanUARTBuffer(); - Status sendStruct(void *structureManaged, uint16_t size_); - Status receiveStruct(void *structureManaged, uint16_t size_); + state_naming::Status sendStruct(void *structureManaged, uint16_t size_); + state_naming::Status receiveStruct(void *structureManaged, uint16_t size_); bool writeProgramCommand(PROGRAM_COMMAND cmd, REGISTER_ADDRESS addr, PACKET_LENGHT pl); - RESPONSE_STATUS checkUARTConfiguration(MODE_TYPE mode); - -#ifdef LoRa_E220_DEBUG - void printParameters(struct Configuration *configuration); -#endif + state_naming::RESPONSE_STATUS checkUARTConfiguration(MODE_TYPE mode); }; - -#endif +} // namespace lora_e220 +} // namespace esphome diff --git a/esphome/components/ebyte_lora_e220/state_naming.h b/esphome/components/ebyte_lora_e220/state_naming.h index a20fbf7ba2..3bf1d5042d 100644 --- a/esphome/components/ebyte_lora_e220/state_naming.h +++ b/esphome/components/ebyte_lora_e220/state_naming.h @@ -1,4 +1,5 @@ -#include "Arduino.h" +namespace esphome { +namespace state_naming { #ifdef FREQUENCY_433 #define OPERATING_FREQUENCY 410 @@ -39,7 +40,7 @@ typedef enum RESPONSE_STATUS { ERR_E220_PACKET_TOO_BIG } Status; -static std::string getResponseDescriptionByParams(byte status) { +static std::string getResponseDescriptionByParams(uint8_t status) { switch (status) { case E220_SUCCESS: return "Success"; @@ -90,7 +91,7 @@ static std::string getResponseDescriptionByParams(byte status) { enum E220_UART_PARITY { MODE_00_8N1 = 0b00, MODE_01_8O1 = 0b01, MODE_10_8E1 = 0b10, MODE_11_8N1 = 0b11 }; -static std::string getUARTParityDescriptionByParams(byte uartParity) { +static std::string getUARTParityDescriptionByParams(uint8_t uartParity) { switch (uartParity) { case MODE_00_8N1: return "8N1 (Default)"; @@ -131,7 +132,7 @@ enum UART_BPS_RATE { UART_BPS_RATE_115200 = 115200 }; -static std::string getUARTBaudRateDescriptionByParams(byte uartBaudRate) { +static std::string getUARTBaudRateDescriptionByParams(uint8_t uartBaudRate) { switch (uartBaudRate) { case UART_BPS_1200: return "1200bps"; @@ -173,7 +174,7 @@ enum AIR_DATA_RATE { AIR_DATA_RATE_111_625 = 0b111 }; -static std::string getAirDataRateDescriptionByParams(byte airDataRate) { +static std::string getAirDataRateDescriptionByParams(uint8_t airDataRate) { switch (airDataRate) { case AIR_DATA_RATE_000_24: return "2.4kbps"; @@ -211,7 +212,7 @@ enum SUB_PACKET_SETTING { SPS_032_11 = 0b11 }; -static std::string getSubPacketSettingByParams(byte subPacketSetting) { +static std::string getSubPacketSettingByParams(uint8_t subPacketSetting) { switch (subPacketSetting) { case SPS_200_00: return "200bytes (default)"; @@ -231,7 +232,7 @@ static std::string getSubPacketSettingByParams(byte subPacketSetting) { } enum RSSI_AMBIENT_NOISE_ENABLE { RSSI_AMBIENT_NOISE_ENABLED = 0b1, RSSI_AMBIENT_NOISE_DISABLED = 0b0 }; -static std::string getRSSIAmbientNoiseEnableByParams(byte rssiAmbientNoiseEnabled) { +static std::string getRSSIAmbientNoiseEnableByParams(uint8_t rssiAmbientNoiseEnabled) { switch (rssiAmbientNoiseEnabled) { case RSSI_AMBIENT_NOISE_ENABLED: return "Enabled"; @@ -255,7 +256,7 @@ enum WOR_PERIOD { WOR_4000_111 = 0b111 }; -static std::string getWORPeriodByParams(byte WORPeriod) { +static std::string getWORPeriodByParams(uint8_t WORPeriod) { switch (WORPeriod) { case WOR_500_000: return "500ms"; @@ -286,7 +287,7 @@ static std::string getWORPeriodByParams(byte WORPeriod) { } } enum LBT_ENABLE_BYTE { LBT_ENABLED = 0b1, LBT_DISABLED = 0b0 }; -static std::string getLBTEnableByteByParams(byte LBTEnableByte) { +static std::string getLBTEnableByteByParams(uint8_t LBTEnableByte) { switch (LBTEnableByte) { case LBT_ENABLED: return "Enabled"; @@ -300,7 +301,7 @@ static std::string getLBTEnableByteByParams(byte LBTEnableByte) { } enum RSSI_ENABLE_BYTE { RSSI_ENABLED = 0b1, RSSI_DISABLED = 0b0 }; -static std::string getRSSIEnableByteByParams(byte RSSIEnableByte) { +static std::string getRSSIEnableByteByParams(uint8_t RSSIEnableByte) { switch (RSSIEnableByte) { case RSSI_ENABLED: return "Enabled"; @@ -315,7 +316,7 @@ static std::string getRSSIEnableByteByParams(byte RSSIEnableByte) { enum FIDEX_TRANSMISSION { FT_TRANSPARENT_TRANSMISSION = 0b0, FT_FIXED_TRANSMISSION = 0b1 }; -static std::string getFixedTransmissionDescriptionByParams(byte fixedTransmission) { +static std::string getFixedTransmissionDescriptionByParams(uint8_t fixedTransmission) { switch (fixedTransmission) { case FT_TRANSPARENT_TRANSMISSION: return "Transparent transmission (default)"; @@ -337,7 +338,7 @@ enum TRANSMISSION_POWER { }; -static std::string getTransmissionPowerDescriptionByParams(byte transmissionPower) { +static std::string getTransmissionPowerDescriptionByParams(uint8_t transmissionPower) { switch (transmissionPower) { case POWER_22: return "22dBm (Default)"; @@ -364,7 +365,7 @@ enum TRANSMISSION_POWER { }; -static std::string getTransmissionPowerDescriptionByParams(byte transmissionPower) { +static std::string getTransmissionPowerDescriptionByParams(uint8_t transmissionPower) { switch (transmissionPower) { case POWER_30: return "30dBm (Default)"; @@ -391,7 +392,7 @@ enum TRANSMISSION_POWER { }; -static std::string getTransmissionPowerDescriptionByParams(byte transmissionPower) { +static std::string getTransmissionPowerDescriptionByParams(uint8_t transmissionPower) { switch (transmissionPower) { case POWER_22: return "22dBm (Default)"; @@ -410,3 +411,5 @@ static std::string getTransmissionPowerDescriptionByParams(byte transmissionPowe } } #endif +} // namespace state_naming +} // namespace esphome