This commit is contained in:
Daniël Koek 2024-04-25 15:10:21 +01:00
parent 27e3b8616b
commit b7b93d44db

View file

@ -10,7 +10,131 @@ void EbyteLoraComponent::update() {
this->get_current_config_();
return;
} else {
ESP_LOGD(TAG, "Got a config:");
ESP_LOGD(TAG, "Checking config");
}
if (this->get_mode_() != NORMAL) {
ESP_LOGD(TAG, "Mode was not set right");
this->set_mode_(NORMAL);
}
this->send_switch_info_();
}
void EbyteLoraComponent::setup() {
this->pin_aux_->setup();
this->pin_m0_->setup();
this->pin_m1_->setup();
this->get_current_config_();
ESP_LOGD(TAG, "Setup success");
}
void EbyteLoraComponent::get_current_config_() {
this->set_mode_(CONFIGURATION);
uint8_t data[3] = {PROGRAM_CONF, 0x00, 0x08};
this->write_array(data, sizeof(data));
ESP_LOGD(TAG, "Config info requested");
}
ModeType EbyteLoraComponent::get_mode_() {
ModeType internal_mode = MODE_INIT;
if (!this->can_send_message_()) {
return internal_mode;
}
bool pin1 = this->pin_m0_->digital_read();
bool pin2 = this->pin_m1_->digital_read();
if (!pin1 && !pin2) {
// ESP_LOGD(TAG, "MODE NORMAL!");
internal_mode = NORMAL;
}
if (pin1 && !pin2) {
// ESP_LOGD(TAG, "MODE WOR!");
internal_mode = WOR_SEND;
}
if (!pin1 && pin2) {
// ESP_LOGD(TAG, "MODE WOR!");
internal_mode = WOR_RECEIVER;
}
if (pin1 && pin2) {
// ESP_LOGD(TAG, "MODE Conf!");
internal_mode = CONFIGURATION;
}
if (internal_mode != this->mode_) {
ESP_LOGD(TAG, "Modes are not equal, calling the set function!! , checked: %u, expected: %u", internal_mode,
this->mode_);
this->set_mode_(internal_mode);
}
return internal_mode;
}
void EbyteLoraComponent::set_mode_(ModeType mode) {
if (!this->can_send_message_()) {
return;
}
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 NORMAL:
// Mode 0 | normal operation
this->pin_m0_->digital_write(false);
this->pin_m1_->digital_write(false);
ESP_LOGD(TAG, "MODE NORMAL!");
break;
case WOR_SEND:
this->pin_m0_->digital_write(true);
this->pin_m1_->digital_write(false);
ESP_LOGD(TAG, "MODE WOR SEND!");
break;
case WOR_RECEIVER:
// case MODE_2_PROGRAM:
this->pin_m0_->digital_write(false);
this->pin_m1_->digital_write(true);
ESP_LOGD(TAG, "MODE RECEIVING!");
break;
case CONFIGURATION:
// Mode 3 | Setting operation
this->pin_m0_->digital_write(true);
this->pin_m1_->digital_write(true);
ESP_LOGD(TAG, "MODE SLEEP and CONFIG!");
break;
case MODE_INIT:
ESP_LOGD(TAG, "Don't call this!");
break;
}
}
// wait until aux pin goes back low
this->setup_wait_response_(1000);
this->mode_ = mode;
ESP_LOGD(TAG, "Mode is going to be set");
}
bool EbyteLoraComponent::can_send_message_() {
// High means no more information is needed
if (this->pin_aux_->digital_read()) {
if (!(this->starting_to_check_ == 0) && !(this->time_out_after_ == 0)) {
this->starting_to_check_ = 0;
this->time_out_after_ = 0;
this->flush();
ESP_LOGD(TAG, "Aux pin is High! Can send again!");
}
return true;
} else {
// it has taken too long to complete, error out!
if ((millis() - this->starting_to_check_) > this->time_out_after_) {
ESP_LOGD(TAG, "Timeout error! Resetting timers");
this->starting_to_check_ = 0;
this->time_out_after_ = 0;
}
return false;
}
}
void EbyteLoraComponent::setup_wait_response_(uint32_t timeout) {
if (!(this->starting_to_check_ == 0) && !(this->time_out_after_ == 0)) {
ESP_LOGD(TAG, "Wait response already set!! %u", timeout);
}
ESP_LOGD(TAG, "Setting a timer for %u", timeout);
this->starting_to_check_ = millis();
this->time_out_after_ = timeout;
}
void EbyteLoraComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Ebyte Lora E220");
switch (this->current_config_.air_data_rate) {
case AIR_2_4KB:
ESP_LOGD(TAG, "air_data_rate: 2.4kb");
@ -155,130 +279,6 @@ void EbyteLoraComponent::update() {
ESP_LOGD(TAG, "wor_period: 4000");
break;
}
}
if (this->get_mode_() != NORMAL) {
ESP_LOGD(TAG, "Mode was not set right");
this->set_mode_(NORMAL);
}
this->send_switch_info_();
}
void EbyteLoraComponent::setup() {
this->pin_aux_->setup();
this->pin_m0_->setup();
this->pin_m1_->setup();
this->get_current_config_();
ESP_LOGD(TAG, "Setup success");
}
void EbyteLoraComponent::get_current_config_() {
this->set_mode_(CONFIGURATION);
uint8_t data[3] = {PROGRAM_CONF, 0x00, 0x08};
this->write_array(data, sizeof(data));
ESP_LOGD(TAG, "Config info requested");
}
ModeType EbyteLoraComponent::get_mode_() {
ModeType internal_mode = MODE_INIT;
if (!this->can_send_message_()) {
return internal_mode;
}
bool pin1 = this->pin_m0_->digital_read();
bool pin2 = this->pin_m1_->digital_read();
if (!pin1 && !pin2) {
// ESP_LOGD(TAG, "MODE NORMAL!");
internal_mode = NORMAL;
}
if (pin1 && !pin2) {
// ESP_LOGD(TAG, "MODE WOR!");
internal_mode = WOR_SEND;
}
if (!pin1 && pin2) {
// ESP_LOGD(TAG, "MODE WOR!");
internal_mode = WOR_RECEIVER;
}
if (pin1 && pin2) {
// ESP_LOGD(TAG, "MODE Conf!");
internal_mode = CONFIGURATION;
}
if (internal_mode != this->mode_) {
ESP_LOGD(TAG, "Modes are not equal, calling the set function!! , checked: %u, expected: %u", internal_mode,
this->mode_);
this->set_mode_(internal_mode);
}
return internal_mode;
}
void EbyteLoraComponent::set_mode_(ModeType mode) {
if (!this->can_send_message_()) {
return;
}
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 NORMAL:
// Mode 0 | normal operation
this->pin_m0_->digital_write(false);
this->pin_m1_->digital_write(false);
ESP_LOGD(TAG, "MODE NORMAL!");
break;
case WOR_SEND:
this->pin_m0_->digital_write(true);
this->pin_m1_->digital_write(false);
ESP_LOGD(TAG, "MODE WOR SEND!");
break;
case WOR_RECEIVER:
// case MODE_2_PROGRAM:
this->pin_m0_->digital_write(false);
this->pin_m1_->digital_write(true);
ESP_LOGD(TAG, "MODE RECEIVING!");
break;
case CONFIGURATION:
// Mode 3 | Setting operation
this->pin_m0_->digital_write(true);
this->pin_m1_->digital_write(true);
ESP_LOGD(TAG, "MODE SLEEP and CONFIG!");
break;
case MODE_INIT:
ESP_LOGD(TAG, "Don't call this!");
break;
}
}
// wait until aux pin goes back low
this->setup_wait_response_(1000);
this->mode_ = mode;
ESP_LOGD(TAG, "Mode is going to be set");
}
bool EbyteLoraComponent::can_send_message_() {
// High means no more information is needed
if (this->pin_aux_->digital_read()) {
if (!(this->starting_to_check_ == 0) && !(this->time_out_after_ == 0)) {
this->starting_to_check_ = 0;
this->time_out_after_ = 0;
this->flush();
ESP_LOGD(TAG, "Aux pin is High! Can send again!");
}
return true;
} else {
// it has taken too long to complete, error out!
if ((millis() - this->starting_to_check_) > this->time_out_after_) {
ESP_LOGD(TAG, "Timeout error! Resetting timers");
this->starting_to_check_ = 0;
this->time_out_after_ = 0;
}
return false;
}
}
void EbyteLoraComponent::setup_wait_response_(uint32_t timeout) {
if (!(this->starting_to_check_ == 0) && !(this->time_out_after_ == 0)) {
ESP_LOGD(TAG, "Wait response already set!! %u", timeout);
}
ESP_LOGD(TAG, "Setting a timer for %u", timeout);
this->starting_to_check_ = millis();
this->time_out_after_ = timeout;
}
void EbyteLoraComponent::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_);
@ -312,12 +312,12 @@ void EbyteLoraComponent::loop() {
}
// if it is only push info
if (data[0] == SWITCH_PUSH) {
ESP_LOGD(TAG, "GOT SWITCH PUSH", data.size());
ESP_LOGD(TAG, "GOT SWITCH PUSH");
ESP_LOGD(TAG, "Total: %u", data.size());
ESP_LOGD(TAG, "Start bit: %u", data[0]);
ESP_LOGD(TAG, "PIN: %u", data[1]);
ESP_LOGD(TAG, "VALUE: %u", data[2]);
ESP_LOGD(TAG, "RSSI: %f %", (data[3] / 255.0) * 100);
ESP_LOGD(TAG, "RSSI: %f", (data[3] / 255.0) * 100);
if (this->rssi_sensor_ != nullptr)
this->rssi_sensor_->publish_state((data[3] / 255.0) * 100);
@ -333,7 +333,7 @@ void EbyteLoraComponent::loop() {
if (data[0] == SWITCH_INFO) {
for (int i = 0; i < data.size(); i++) {
if (data[i] == SWITCH_INFO) {
ESP_LOGD(TAG, "GOT INFO", data.size());
ESP_LOGD(TAG, "GOT INFO");
uint8_t pin = data[i + 1];
bool value = data[i + 2];
for (auto *sensor : this->sensors_) {
@ -344,7 +344,7 @@ void EbyteLoraComponent::loop() {
}
}
this->rssi_sensor_->publish_state((data[data.size() - 1] / 255.0) * 100);
ESP_LOGD(TAG, "RSSI: %f %", (data[data.size() - 1] / 255.0) * 100);
ESP_LOGD(TAG, "RSSI: %f", (data[data.size() - 1] / 255.0) * 100);
}
if (data[0] == PROGRAM_CONF) {
ESP_LOGD(TAG, "GOT PROGRAM_CONF");