This commit is contained in:
Daniël Koek 2024-03-25 14:34:36 +00:00
parent 6617baeae6
commit 6b7e92b80f
4 changed files with 62 additions and 61 deletions

View file

@ -2,11 +2,11 @@
namespace esphome { namespace esphome {
namespace lora { 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::setup() { pin_mode(flags_); }
void LoraGPIOPin::pin_mode(gpio::Flags flags) { void LoraGPIOPin::pin_mode(gpio::Flags flags) {
if (flags != gpio::FLAG_OUTPUT) { if (flags != gpio::FLAG_OUTPUT) {
ESP_LOGD(TAGPin, "Output only supported"); ESP_LOGD(TAG_PIN, "Output only supported");
} }
} }
bool LoraGPIOPin::digital_read() { return false; } bool LoraGPIOPin::digital_read() { return false; }

View file

@ -16,7 +16,6 @@ class LoraGPIOPin : public GPIOPin {
bool digital_read() override; bool digital_read() override;
void digital_write(bool value) override; void digital_write(bool value) override;
std::string dump_summary() const override; std::string dump_summary() const override;
void set_parent(Lora *parent) { parent_ = parent; } void set_parent(Lora *parent) { parent_ = parent; }
void set_pin(uint8_t pin) { pin_ = pin; } void set_pin(uint8_t pin) { pin_ = pin; }
void set_inverted(bool inverted) { inverted_ = inverted; } void set_inverted(bool inverted) { inverted_ = inverted; }

View file

@ -3,69 +3,70 @@
namespace esphome { namespace esphome {
namespace lora { namespace lora {
void Lora::update() { void Lora::update() {
if (this->rssi_sensor != nullptr) if (this->rssi_sensor_ != nullptr)
this->rssi_sensor->publish_state(this->rssi_); this->rssi_sensor_->publish_state(this->rssi_);
// raw info // raw info
if (this->message_text_sensor != nullptr) if (this->message_text_sensor_ != nullptr)
this->message_text_sensor->publish_state(this->raw_message_); this->message_text_sensor_->publish_state(this->raw_message_);
} }
void Lora::setup() { void Lora::setup() {
this->pin_aux->setup(); this->pin_aux_->setup();
this->pin_m0->setup(); this->pin_m0_->setup();
this->pin_m1->setup(); this->pin_m1_->setup();
if (this->pin_aux != nullptr) { if (this->pin_aux_ != nullptr) {
this->pin_aux->pin_mode(gpio::FLAG_INPUT); this->pin_aux_->pin_mode(gpio::FLAG_INPUT);
ESP_LOGD(TAG, "Init AUX pin!"); ESP_LOGD(TAG, "Init AUX pin!");
} }
if (this->pin_m0 != nullptr) { if (this->pin_m0_ != nullptr) {
this->pin_m0->pin_mode(gpio::FLAG_OUTPUT); this->pin_m0_->pin_mode(gpio::FLAG_OUTPUT);
ESP_LOGD(TAG, "Init M0 pin!"); ESP_LOGD(TAG, "Init M0 pin!");
this->pin_m0->digital_write(true); this->pin_m0_->digital_write(true);
} }
if (this->pin_m1 != nullptr) { if (this->pin_m1_ != nullptr) {
this->pin_m1->pin_mode(gpio::FLAG_OUTPUT); this->pin_m1_->pin_mode(gpio::FLAG_OUTPUT);
ESP_LOGD(TAG, "Init M1 pin!"); ESP_LOGD(TAG, "Init M1 pin!");
this->pin_m1->digital_write(true); this->pin_m1_->digital_write(true);
} }
bool status = setMode(MODE_0_NORMAL); bool status = set_mode_(MODE_0_NORMAL);
if (status) if (status) {
ESP_LOGD(TAG, "Setup success"); ESP_LOGD(TAG, "Setup success");
else } else {
ESP_LOGD(TAG, "Something went wrong"); 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) // data sheet claims module needs some extra time after mode setting (2ms)
// most of my projects uses 10 ms, but 40ms is safer // most of my projects uses 10 ms, but 40ms is safer
delay(40); 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!"); ESP_LOGD(TAG, "The M0 and M1 pins is not set, this mean that you are connect directly the pins as you need!");
} else { } else {
switch (mode) { switch (mode) {
case MODE_0_NORMAL: case MODE_0_NORMAL:
// Mode 0 | normal operation // Mode 0 | normal operation
this->pin_m0->digital_write(false); this->pin_m0_->digital_write(false);
this->pin_m1->digital_write(false); this->pin_m1_->digital_write(false);
ESP_LOGD(TAG, "MODE NORMAL!"); ESP_LOGD(TAG, "MODE NORMAL!");
break; break;
case MODE_1_WOR_TRANSMITTER: case MODE_1_WOR_TRANSMITTER:
this->pin_m0->digital_write(true); this->pin_m0_->digital_write(true);
this->pin_m1->digital_write(false); this->pin_m1_->digital_write(false);
ESP_LOGD(TAG, "MODE WOR!"); ESP_LOGD(TAG, "MODE WOR!");
break; break;
case MODE_2_WOR_RECEIVER: case MODE_2_WOR_RECEIVER:
// case MODE_2_PROGRAM: // case MODE_2_PROGRAM:
this->pin_m0->digital_write(false); this->pin_m0_->digital_write(false);
this->pin_m1->digital_write(true); this->pin_m1_->digital_write(true);
ESP_LOGD(TAG, "MODE RECEIVING!"); ESP_LOGD(TAG, "MODE RECEIVING!");
break; break;
case MODE_3_CONFIGURATION: case MODE_3_CONFIGURATION:
// Mode 3 | Setting operation // Mode 3 | Setting operation
this->pin_m0->digital_write(true); this->pin_m0_->digital_write(true);
this->pin_m1->digital_write(true); this->pin_m1_->digital_write(true);
ESP_LOGD(TAG, "MODE SLEEP CONFIG!"); ESP_LOGD(TAG, "MODE SLEEP CONFIG!");
break; break;
@ -78,10 +79,10 @@ bool Lora::setMode(MODE_TYPE mode) {
delay(40); delay(40);
// wait until aux pin goes back low // wait until aux pin goes back low
bool result = this->waitCompleteResponse(1000); bool result = this->wait_complete_response_(1000);
if (result) { if (result) {
this->mode = mode; this->mode_ = mode;
ESP_LOGD(TAG, "Mode set"); ESP_LOGD(TAG, "Mode set");
return true; return true;
} else { } else {
@ -89,18 +90,18 @@ bool Lora::setMode(MODE_TYPE mode) {
return false; return false;
} }
} }
bool Lora::waitCompleteResponse(unsigned long timeout, unsigned int waitNoAux) { bool Lora::wait_complete_response_(uint32_t timeout, uint32_t wait_no_aux) {
unsigned long t = millis(); uint32_t t = millis();
// make darn sure millis() is not about to reach max data type limit and start over // 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; t = 0;
} }
// if AUX pin was supplied and look for HIGH state // 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 // 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) { if (this->pin_aux_ != nullptr) {
while (this->pin_aux->digital_read() == false) { while (this->pin_aux_->digital_read() == false) {
if ((millis() - t) > timeout) { if ((millis() - t) > timeout) {
ESP_LOGD(TAG, "Timeout error!"); ESP_LOGD(TAG, "Timeout error!");
return false; return false;
@ -110,7 +111,7 @@ bool Lora::waitCompleteResponse(unsigned long timeout, unsigned int waitNoAux) {
} else { } else {
// if you can't use aux pin, use 4K7 pullup with Arduino // if you can't use aux pin, use 4K7 pullup with Arduino
// you may need to adjust this value if transmissions fail // you may need to adjust this value if transmissions fail
delay(waitNoAux); delay(wait_no_aux);
ESP_LOGD(TAG, "Wait no AUX pin!"); ESP_LOGD(TAG, "Wait no AUX pin!");
} }
@ -120,12 +121,12 @@ bool Lora::waitCompleteResponse(unsigned long timeout, unsigned int waitNoAux) {
} }
void Lora::dump_config() { void Lora::dump_config() {
ESP_LOGCONFIG(TAG, "Ebyte Lora E220"); ESP_LOGCONFIG(TAG, "Ebyte Lora E220");
LOG_PIN("Aux pin:", this->pin_aux); LOG_PIN("Aux pin:", this->pin_aux_);
LOG_PIN("M0 Pin:", this->pin_m0); LOG_PIN("M0 Pin:", this->pin_m0_);
LOG_PIN("M1 Pin:", this->pin_m1); LOG_PIN("M1 Pin:", this->pin_m1_);
}; };
void Lora::digital_write(uint8_t pin, bool value) { this->sendPinInfo(pin, value); } void Lora::digital_write(uint8_t pin, bool value) { this->send_pin_info_(pin, value); }
bool Lora::sendPinInfo(uint8_t pin, bool value) { bool Lora::send_pin_info_(uint8_t pin, bool value) {
uint8_t data[3]; uint8_t data[3];
data[1] = 0xA5; // just some bit to indicate, yo this is pin info data[1] = 0xA5; // just some bit to indicate, yo this is pin info
data[1] = pin; // Pin to send data[1] = pin; // Pin to send
@ -156,14 +157,15 @@ void Lora::loop() {
data.push_back(c); data.push_back(c);
} }
} }
ESP_LOGD(TAG, "Got %s", buffer); ESP_LOGD(TAG, "Got %s", buffer.c_str());
if (data.size() != 0) { if (!data.empty()) {
ESP_LOGD(TAG, "Found pin data!"); ESP_LOGD(TAG, "Found pin data!");
ESP_LOGD(TAG, "PIN: %u ", data[1]); ESP_LOGD(TAG, "PIN: %u ", data[1]);
ESP_LOGD(TAG, "VALUE: %u ", data[2]); ESP_LOGD(TAG, "VALUE: %u ", data[2]);
} }
char *ptr;
// set the rssi // 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_); 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);

View file

@ -14,7 +14,7 @@ namespace lora {
static const char *const TAG = "lora"; static const char *const TAG = "lora";
static const uint8_t MAX_SIZE_TX_PACKET = 200; static const uint8_t MAX_SIZE_TX_PACKET = 200;
// the mode the receiver is in // the mode the receiver is in
enum MODE_TYPE { enum ModeType {
MODE_0_NORMAL = 0, MODE_0_NORMAL = 0,
MODE_0_TRANSMISSION = 0, MODE_0_TRANSMISSION = 0,
MODE_1_WOR_TRANSMITTER = 1, 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. /// Helper function to set the pin mode of a pin.
void pin_mode(uint8_t pin, gpio::Flags flags); void pin_mode(uint8_t pin, gpio::Flags flags);
void set_message_sensor(text_sensor::TextSensor *s) { message_text_sensor = s; } void set_message_sensor(text_sensor::TextSensor *s) { message_text_sensor = s; }
void set_rssi_sensor(sensor::Sensor *s) { rssi_sensor = s; } void set_rssi_sensor(sensor::Sensor *s) { rssi_sensor_ = s; }
void set_pin_aux(GPIOPin *s) { pin_aux = s; } void set_pin_aux(GPIOPin *s) { pin_aux_ = s; }
void set_pin_m0(GPIOPin *s) { pin_m0 = s; } void set_pin_m0(GPIOPin *s) { pin_m0_ = s; }
void set_pin_m1(GPIOPin *s) { pin_m1 = s; } void set_pin_m1(GPIOPin *s) { pin_m1_ = s; }
private: private:
MODE_TYPE mode = MODE_0_NORMAL; ModeType mode_ = MODE_0_NORMAL;
// set WOR mode // set WOR mode
bool setMode(MODE_TYPE type); bool set_mode_(ModeType type);
// checks the aux port to see if it is done setting // checks the aux port to see if it is done setting
bool waitCompleteResponse(unsigned long timeout = 1000, unsigned int waitNoAux = 100); bool wait_complete_response_(uint32_t timeout = 1000, uint32_t waitNoAux = 100);
bool sendPinInfo(uint8_t pin, bool value); bool send_pin_info_(uint8_t pin, bool value);
protected: protected:
int rssi_ = 0; int rssi_ = 0;
float latitude_ = -1; float latitude_ = -1;
float longitude_ = -1; float longitude_ = -1;
std::string raw_message_; std::string raw_message_;
text_sensor::TextSensor *message_text_sensor{nullptr}; text_sensor::TextSensor *message_text_sensor_{nullptr};
sensor::Sensor *rssi_sensor{nullptr}; sensor::Sensor *rssi_sensor_{nullptr};
GPIOPin *pin_aux{nullptr}; GPIOPin *pin_aux_{nullptr};
GPIOPin *pin_m0{nullptr}; GPIOPin *pin_m0_{nullptr};
GPIOPin *pin_m1{nullptr}; GPIOPin *pin_m1_{nullptr};
}; };
} // namespace lora } // namespace lora