diff --git a/esphome/components/lora/gpio/gpio_lora.cpp b/esphome/components/lora/gpio/gpio_lora.cpp index 06d8cbadd1..8b22f33a45 100644 --- a/esphome/components/lora/gpio/gpio_lora.cpp +++ b/esphome/components/lora/gpio/gpio_lora.cpp @@ -2,11 +2,11 @@ namespace esphome { namespace lora { -static const char *const TAGPin = "lora.pin"; +static const char *const TAG_PIN = "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"); + ESP_LOGD(TAG_PIN, "Output only supported"); } } bool LoraGPIOPin::digital_read() { return false; } diff --git a/esphome/components/lora/gpio/gpio_lora.h b/esphome/components/lora/gpio/gpio_lora.h index 3ee6f35529..b412ad09ad 100644 --- a/esphome/components/lora/gpio/gpio_lora.h +++ b/esphome/components/lora/gpio/gpio_lora.h @@ -16,7 +16,6 @@ class LoraGPIOPin : public GPIOPin { 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; } diff --git a/esphome/components/lora/lora.cpp b/esphome/components/lora/lora.cpp index d68a390ca5..6b4e91b173 100644 --- a/esphome/components/lora/lora.cpp +++ b/esphome/components/lora/lora.cpp @@ -3,69 +3,70 @@ namespace esphome { namespace lora { void Lora::update() { - if (this->rssi_sensor != nullptr) - this->rssi_sensor->publish_state(this->rssi_); + if (this->rssi_sensor_ != nullptr) + this->rssi_sensor_->publish_state(this->rssi_); // raw info - if (this->message_text_sensor != nullptr) - this->message_text_sensor->publish_state(this->raw_message_); + if (this->message_text_sensor_ != nullptr) + this->message_text_sensor_->publish_state(this->raw_message_); } void Lora::setup() { - this->pin_aux->setup(); - this->pin_m0->setup(); - this->pin_m1->setup(); - if (this->pin_aux != nullptr) { - this->pin_aux->pin_mode(gpio::FLAG_INPUT); + this->pin_aux_->setup(); + this->pin_m0_->setup(); + this->pin_m1_->setup(); + if (this->pin_aux_ != nullptr) { + this->pin_aux_->pin_mode(gpio::FLAG_INPUT); ESP_LOGD(TAG, "Init AUX pin!"); } - if (this->pin_m0 != nullptr) { - this->pin_m0->pin_mode(gpio::FLAG_OUTPUT); + if (this->pin_m0_ != nullptr) { + this->pin_m0_->pin_mode(gpio::FLAG_OUTPUT); ESP_LOGD(TAG, "Init M0 pin!"); - this->pin_m0->digital_write(true); + this->pin_m0_->digital_write(true); } - if (this->pin_m1 != nullptr) { - this->pin_m1->pin_mode(gpio::FLAG_OUTPUT); + if (this->pin_m1_ != nullptr) { + this->pin_m1_->pin_mode(gpio::FLAG_OUTPUT); ESP_LOGD(TAG, "Init M1 pin!"); - this->pin_m1->digital_write(true); + this->pin_m1_->digital_write(true); } - bool status = setMode(MODE_0_NORMAL); - if (status) + bool status = set_mode_(MODE_0_NORMAL); + if (status) { ESP_LOGD(TAG, "Setup success"); - else + } else { ESP_LOGD(TAG, "Something went wrong"); + } } -bool Lora::setMode(MODE_TYPE mode) { +bool Lora::set_mode_(ModeType mode) { // data sheet claims module needs some extra time after mode setting (2ms) // most of my projects uses 10 ms, but 40ms is safer delay(40); - if (this->pin_m0 == nullptr && this->pin_m1 == nullptr) { + if (this->pin_m0_ == nullptr && this->pin_m1_ == nullptr) { 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 - this->pin_m0->digital_write(false); - this->pin_m1->digital_write(false); + this->pin_m0_->digital_write(false); + this->pin_m1_->digital_write(false); ESP_LOGD(TAG, "MODE NORMAL!"); break; case MODE_1_WOR_TRANSMITTER: - this->pin_m0->digital_write(true); - this->pin_m1->digital_write(false); + this->pin_m0_->digital_write(true); + this->pin_m1_->digital_write(false); ESP_LOGD(TAG, "MODE WOR!"); break; case MODE_2_WOR_RECEIVER: // case MODE_2_PROGRAM: - this->pin_m0->digital_write(false); - this->pin_m1->digital_write(true); + this->pin_m0_->digital_write(false); + this->pin_m1_->digital_write(true); ESP_LOGD(TAG, "MODE RECEIVING!"); break; case MODE_3_CONFIGURATION: // Mode 3 | Setting operation - this->pin_m0->digital_write(true); - this->pin_m1->digital_write(true); + this->pin_m0_->digital_write(true); + this->pin_m1_->digital_write(true); ESP_LOGD(TAG, "MODE SLEEP CONFIG!"); break; @@ -78,10 +79,10 @@ bool Lora::setMode(MODE_TYPE mode) { delay(40); // wait until aux pin goes back low - bool result = this->waitCompleteResponse(1000); + bool result = this->wait_complete_response_(1000); if (result) { - this->mode = mode; + this->mode_ = mode; ESP_LOGD(TAG, "Mode set"); return true; } else { @@ -89,18 +90,18 @@ bool Lora::setMode(MODE_TYPE mode) { return false; } } -bool Lora::waitCompleteResponse(unsigned long timeout, unsigned int waitNoAux) { - unsigned long t = millis(); +bool Lora::wait_complete_response_(uint32_t timeout, uint32_t wait_no_aux) { + uint32_t t = millis(); // make darn sure millis() is not about to reach max data type limit and start over - if (((unsigned long) (t + timeout)) == 0) { + if ((t + timeout) == 0) { t = 0; } // 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->pin_aux != nullptr) { - while (this->pin_aux->digital_read() == false) { + if (this->pin_aux_ != nullptr) { + while (this->pin_aux_->digital_read() == false) { if ((millis() - t) > timeout) { ESP_LOGD(TAG, "Timeout error!"); return false; @@ -110,7 +111,7 @@ bool Lora::waitCompleteResponse(unsigned long timeout, unsigned int waitNoAux) { } else { // if you can't use aux pin, use 4K7 pullup with Arduino // you may need to adjust this value if transmissions fail - delay(waitNoAux); + delay(wait_no_aux); ESP_LOGD(TAG, "Wait no AUX pin!"); } @@ -120,12 +121,12 @@ bool Lora::waitCompleteResponse(unsigned long timeout, unsigned int waitNoAux) { } void Lora::dump_config() { ESP_LOGCONFIG(TAG, "Ebyte Lora E220"); - LOG_PIN("Aux pin:", this->pin_aux); - LOG_PIN("M0 Pin:", this->pin_m0); - LOG_PIN("M1 Pin:", this->pin_m1); + LOG_PIN("Aux pin:", this->pin_aux_); + LOG_PIN("M0 Pin:", this->pin_m0_); + LOG_PIN("M1 Pin:", this->pin_m1_); }; -void Lora::digital_write(uint8_t pin, bool value) { this->sendPinInfo(pin, value); } -bool Lora::sendPinInfo(uint8_t pin, bool value) { +void Lora::digital_write(uint8_t pin, bool value) { this->send_pin_info_(pin, value); } +bool Lora::send_pin_info_(uint8_t pin, bool value) { uint8_t data[3]; data[1] = 0xA5; // just some bit to indicate, yo this is pin info data[1] = pin; // Pin to send @@ -156,14 +157,15 @@ void Lora::loop() { data.push_back(c); } } - ESP_LOGD(TAG, "Got %s", buffer); - if (data.size() != 0) { + ESP_LOGD(TAG, "Got %s", buffer.c_str()); + if (!data.empty()) { ESP_LOGD(TAG, "Found pin data!"); ESP_LOGD(TAG, "PIN: %u ", data[1]); ESP_LOGD(TAG, "VALUE: %u ", data[2]); } + char *ptr; // set the rssi - rssi_ = atoi(buffer.substr(buffer.length() - 1, 1).c_str()); + rssi_ = strtol(buffer.substr(buffer.length() - 1, 1).c_str(), &ptr, 2); ESP_LOGD(TAG, "RSSI: %u ", rssi_); // set the raw message raw_message_ = buffer.substr(0, buffer.length() - 1); diff --git a/esphome/components/lora/lora.h b/esphome/components/lora/lora.h index c3a5744f5b..e5f74e357f 100644 --- a/esphome/components/lora/lora.h +++ b/esphome/components/lora/lora.h @@ -14,7 +14,7 @@ namespace lora { static const char *const TAG = "lora"; static const uint8_t MAX_SIZE_TX_PACKET = 200; // the mode the receiver is in -enum MODE_TYPE { +enum ModeType { MODE_0_NORMAL = 0, MODE_0_TRANSMISSION = 0, MODE_1_WOR_TRANSMITTER = 1, @@ -40,29 +40,29 @@ class Lora : public PollingComponent, public uart::UARTDevice { /// Helper function to set the pin mode of a pin. void pin_mode(uint8_t pin, gpio::Flags flags); void set_message_sensor(text_sensor::TextSensor *s) { message_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 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; } private: - MODE_TYPE mode = MODE_0_NORMAL; + ModeType mode_ = MODE_0_NORMAL; // set WOR mode - bool setMode(MODE_TYPE type); + bool set_mode_(ModeType type); // checks the aux port to see if it is done setting - bool waitCompleteResponse(unsigned long timeout = 1000, unsigned int waitNoAux = 100); - bool sendPinInfo(uint8_t pin, bool value); + bool wait_complete_response_(uint32_t timeout = 1000, uint32_t waitNoAux = 100); + bool send_pin_info_(uint8_t pin, bool value); protected: int rssi_ = 0; float latitude_ = -1; float longitude_ = -1; std::string raw_message_; - text_sensor::TextSensor *message_text_sensor{nullptr}; - sensor::Sensor *rssi_sensor{nullptr}; - GPIOPin *pin_aux{nullptr}; - GPIOPin *pin_m0{nullptr}; - GPIOPin *pin_m1{nullptr}; + text_sensor::TextSensor *message_text_sensor_{nullptr}; + sensor::Sensor *rssi_sensor_{nullptr}; + GPIOPin *pin_aux_{nullptr}; + GPIOPin *pin_m0_{nullptr}; + GPIOPin *pin_m1_{nullptr}; }; } // namespace lora