mirror of
https://github.com/esphome/esphome.git
synced 2025-07-29 06:36:45 +00:00
[sx127x] Improve error handling (#9351)
This commit is contained in:
parent
e5a699a004
commit
8da322fe9e
@ -252,15 +252,17 @@ size_t SX127x::get_max_packet_size() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SX127x::transmit_packet(const std::vector<uint8_t> &packet) {
|
SX127xError SX127x::transmit_packet(const std::vector<uint8_t> &packet) {
|
||||||
if (this->payload_length_ > 0 && this->payload_length_ != packet.size()) {
|
if (this->payload_length_ > 0 && this->payload_length_ != packet.size()) {
|
||||||
ESP_LOGE(TAG, "Packet size does not match config");
|
ESP_LOGE(TAG, "Packet size does not match config");
|
||||||
return;
|
return SX127xError::INVALID_PARAMS;
|
||||||
}
|
}
|
||||||
if (packet.empty() || packet.size() > this->get_max_packet_size()) {
|
if (packet.empty() || packet.size() > this->get_max_packet_size()) {
|
||||||
ESP_LOGE(TAG, "Packet size out of range");
|
ESP_LOGE(TAG, "Packet size out of range");
|
||||||
return;
|
return SX127xError::INVALID_PARAMS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SX127xError ret = SX127xError::NONE;
|
||||||
if (this->modulation_ == MOD_LORA) {
|
if (this->modulation_ == MOD_LORA) {
|
||||||
this->set_mode_standby();
|
this->set_mode_standby();
|
||||||
if (this->payload_length_ == 0) {
|
if (this->payload_length_ == 0) {
|
||||||
@ -278,11 +280,13 @@ void SX127x::transmit_packet(const std::vector<uint8_t> &packet) {
|
|||||||
this->write_fifo_(packet);
|
this->write_fifo_(packet);
|
||||||
this->set_mode_tx();
|
this->set_mode_tx();
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait until transmit completes, typically the delay will be less than 100 ms
|
// wait until transmit completes, typically the delay will be less than 100 ms
|
||||||
uint32_t start = millis();
|
uint32_t start = millis();
|
||||||
while (!this->dio0_pin_->digital_read()) {
|
while (!this->dio0_pin_->digital_read()) {
|
||||||
if (millis() - start > 4000) {
|
if (millis() - start > 4000) {
|
||||||
ESP_LOGE(TAG, "Transmit packet failure");
|
ESP_LOGE(TAG, "Transmit packet failure");
|
||||||
|
ret = SX127xError::TIMEOUT;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -291,6 +295,7 @@ void SX127x::transmit_packet(const std::vector<uint8_t> &packet) {
|
|||||||
} else {
|
} else {
|
||||||
this->set_mode_sleep();
|
this->set_mode_sleep();
|
||||||
}
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SX127x::call_listeners_(const std::vector<uint8_t> &packet, float rssi, float snr) {
|
void SX127x::call_listeners_(const std::vector<uint8_t> &packet, float rssi, float snr) {
|
||||||
@ -335,13 +340,7 @@ void SX127x::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SX127x::run_image_cal() {
|
void SX127x::run_image_cal() {
|
||||||
uint32_t start = millis();
|
if (this->modulation_ == MOD_LORA) {
|
||||||
uint8_t mode = this->read_register_(REG_OP_MODE);
|
|
||||||
if ((mode & MODE_MASK) != MODE_STDBY) {
|
|
||||||
ESP_LOGE(TAG, "Need to be in standby for image cal");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (mode & MOD_LORA) {
|
|
||||||
this->set_mode_(MOD_FSK, MODE_SLEEP);
|
this->set_mode_(MOD_FSK, MODE_SLEEP);
|
||||||
this->set_mode_(MOD_FSK, MODE_STDBY);
|
this->set_mode_(MOD_FSK, MODE_STDBY);
|
||||||
}
|
}
|
||||||
@ -350,13 +349,15 @@ void SX127x::run_image_cal() {
|
|||||||
} else {
|
} else {
|
||||||
this->write_register_(REG_IMAGE_CAL, IMAGE_CAL_START);
|
this->write_register_(REG_IMAGE_CAL, IMAGE_CAL_START);
|
||||||
}
|
}
|
||||||
|
uint32_t start = millis();
|
||||||
while (this->read_register_(REG_IMAGE_CAL) & IMAGE_CAL_RUNNING) {
|
while (this->read_register_(REG_IMAGE_CAL) & IMAGE_CAL_RUNNING) {
|
||||||
if (millis() - start > 20) {
|
if (millis() - start > 20) {
|
||||||
ESP_LOGE(TAG, "Image cal failure");
|
ESP_LOGE(TAG, "Image cal failure");
|
||||||
|
this->mark_failed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (mode & MOD_LORA) {
|
if (this->modulation_ == MOD_LORA) {
|
||||||
this->set_mode_(this->modulation_, MODE_SLEEP);
|
this->set_mode_(this->modulation_, MODE_SLEEP);
|
||||||
this->set_mode_(this->modulation_, MODE_STDBY);
|
this->set_mode_(this->modulation_, MODE_STDBY);
|
||||||
}
|
}
|
||||||
@ -375,6 +376,7 @@ void SX127x::set_mode_(uint8_t modulation, uint8_t mode) {
|
|||||||
}
|
}
|
||||||
if (millis() - start > 20) {
|
if (millis() - start > 20) {
|
||||||
ESP_LOGE(TAG, "Set mode failure");
|
ESP_LOGE(TAG, "Set mode failure");
|
||||||
|
this->mark_failed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,8 @@ enum SX127xBw : uint8_t {
|
|||||||
SX127X_BW_500_0,
|
SX127X_BW_500_0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class SX127xError { NONE = 0, TIMEOUT, INVALID_PARAMS };
|
||||||
|
|
||||||
class SX127xListener {
|
class SX127xListener {
|
||||||
public:
|
public:
|
||||||
virtual void on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) = 0;
|
virtual void on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) = 0;
|
||||||
@ -79,7 +81,7 @@ class SX127x : public Component,
|
|||||||
void set_sync_value(const std::vector<uint8_t> &sync_value) { this->sync_value_ = sync_value; }
|
void set_sync_value(const std::vector<uint8_t> &sync_value) { this->sync_value_ = sync_value; }
|
||||||
void run_image_cal();
|
void run_image_cal();
|
||||||
void configure();
|
void configure();
|
||||||
void transmit_packet(const std::vector<uint8_t> &packet);
|
SX127xError transmit_packet(const std::vector<uint8_t> &packet);
|
||||||
void register_listener(SX127xListener *listener) { this->listeners_.push_back(listener); }
|
void register_listener(SX127xListener *listener) { this->listeners_.push_back(listener); }
|
||||||
Trigger<std::vector<uint8_t>, float, float> *get_packet_trigger() const { return this->packet_trigger_; };
|
Trigger<std::vector<uint8_t>, float, float> *get_packet_trigger() const { return this->packet_trigger_; };
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user