add function set_state

This commit is contained in:
Chelios 2024-10-11 06:23:58 +03:00
parent cd82189d38
commit 88f0d365b6
2 changed files with 14 additions and 18 deletions

View file

@ -115,8 +115,7 @@ void ModemComponent::loop() {
if (this->dce->sync() == esp_modem::command_result::OK) { if (this->dce->sync() == esp_modem::command_result::OK) {
ESP_LOGD(TAG, "sync OK TURNING_ON_PWRKEY"); ESP_LOGD(TAG, "sync OK TURNING_ON_PWRKEY");
this->turn_off_pwrkey(); this->turn_off_pwrkey();
this->state_ = ModemComponentState::REGISTRATION_IN_NETWORK; this->set_state(ModemComponentState::REGISTRATION_IN_NETWORK);
time_change_state = millis();
} else { } else {
ESP_LOGD(TAG, "Wait sync TURNING_ON_PWRKEY"); ESP_LOGD(TAG, "Wait sync TURNING_ON_PWRKEY");
} }
@ -126,8 +125,7 @@ void ModemComponent::loop() {
if (get_rssi()) { if (get_rssi()) {
ESP_LOGD(TAG, "Starting modem connection"); ESP_LOGD(TAG, "Starting modem connection");
ESP_LOGD(TAG, "SIgnal quality: rssi=%d", get_rssi()); ESP_LOGD(TAG, "SIgnal quality: rssi=%d", get_rssi());
this->state_ = ModemComponentState::CONNECTING; this->set_state(ModemComponentState::CONNECTING);
time_change_state = millis();
this->dce->set_data(); this->dce->set_data();
// this->start_connect_(); // this->start_connect_();
} else { } else {
@ -158,8 +156,7 @@ void ModemComponent::loop() {
if (this->dce->sync() == esp_modem::command_result::OK) { if (this->dce->sync() == esp_modem::command_result::OK) {
ESP_LOGD(TAG, "sync OK TURNING_ON_RESET"); ESP_LOGD(TAG, "sync OK TURNING_ON_RESET");
this->turn_off_reset(); this->turn_off_reset();
this->state_ = ModemComponentState::REGISTRATION_IN_NETWORK; this->set_state(ModemComponentState::REGISTRATION_IN_NETWORK);
time_change_state = millis();
} else { } else {
ESP_LOGD(TAG, "Wait sync TURNING_ON_RESET"); ESP_LOGD(TAG, "Wait sync TURNING_ON_RESET");
} }
@ -219,35 +216,35 @@ bool ModemComponent::check_modem_component_state_timings() {
return false; return false;
} }
void ModemComponent::set_state(ModemComponentState state){
this->state_ = state;
time_change_state = millis();
}
void ModemComponent::turn_on_modem() { void ModemComponent::turn_on_modem() {
if (power_pin_) { if (power_pin_) {
this->power_pin_->digital_write(true); this->power_pin_->digital_write(true);
time_turn_on_modem = millis(); time_turn_on_modem = millis();
ESP_LOGD(TAG, "Modem turn on"); ESP_LOGD(TAG, "Modem turn on");
this->state_ = ModemComponentState::TURNING_ON_POWER; this->set_state(ModemComponentState::TURNING_ON_POWER);
time_change_state = millis();
} else { } else {
ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey"); ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey");
this->turn_on_pwrkey(); this->turn_on_pwrkey();
} }
// wait no more than 1.9 sec for signs of life to appear
} }
void ModemComponent::turn_off_modem() { void ModemComponent::turn_off_modem() {
this->power_pin_->digital_write(true); this->power_pin_->digital_write(true);
time_turn_off_modem = millis(); time_turn_off_modem = millis();
ESP_LOGD(TAG, "modem turn off"); ESP_LOGD(TAG, "modem turn off");
global_modem_component->state_ = ModemComponentState::STOPPED; this->set_state(ModemComponentState::STOPPED);
time_change_state = millis();
// wait no more than 1.9 sec for signs of life to appear
} }
void ModemComponent::turn_on_pwrkey() { void ModemComponent::turn_on_pwrkey() {
if (pwrkey_pin_) { if (pwrkey_pin_) {
this->pwrkey_pin_->digital_write(false); this->pwrkey_pin_->digital_write(false);
ESP_LOGD(TAG, "pwrkey turn on"); ESP_LOGD(TAG, "pwrkey turn on");
this->state_ = ModemComponentState::TURNING_ON_PWRKEY; this->set_state(ModemComponentState::TURNING_ON_PWRKEY);
time_change_state = millis();
} else { } else {
ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem"); ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem");
this->turn_on_reset(); this->turn_on_reset();
@ -263,7 +260,7 @@ void ModemComponent::turn_off_pwrkey() {
void ModemComponent::turn_on_reset() { void ModemComponent::turn_on_reset() {
this->reset_pin_->digital_write(false); this->reset_pin_->digital_write(false);
ESP_LOGD(TAG, "turn on reset"); ESP_LOGD(TAG, "turn on reset");
time_turn_on_reset = millis(); this->set_state(ModemComponentState::TURNING_ON_PWRKEY);
} }
void ModemComponent::turn_off_reset() { void ModemComponent::turn_off_reset() {
@ -333,8 +330,7 @@ void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base
ESP_LOGD(TAG, "IP event! %" PRIu32, event_id); ESP_LOGD(TAG, "IP event! %" PRIu32, event_id);
if (event_id == IP_EVENT_PPP_GOT_IP) { if (event_id == IP_EVENT_PPP_GOT_IP) {
global_modem_component->connected_ = true; global_modem_component->connected_ = true;
global_modem_component->state_ = ModemComponentState::CONNECTED; global_modem_component->set_state(ModemComponentState::CONNECTED);
time_change_state = millis();
esp_netif_dns_info_t dns_info; esp_netif_dns_info_t dns_info;
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
@ -386,7 +382,6 @@ void ModemComponent::start_connect_() {
} }
bool ModemComponent::is_connected() { bool ModemComponent::is_connected() {
time_change_state = millis();
return this->state_ == ModemComponentState::CONNECTED; return this->state_ == ModemComponentState::CONNECTED;
} }
void ModemComponent::set_power_pin(InternalGPIOPin *power_pin) { this->power_pin_ = power_pin; } void ModemComponent::set_power_pin(InternalGPIOPin *power_pin) { this->power_pin_ = power_pin; }

View file

@ -99,6 +99,7 @@ class ModemComponent : public Component {
int get_rssi(); int get_rssi();
int get_modem_voltage(); int get_modem_voltage();
const char *get_state(); const char *get_state();
void set_state(ModemComponentState state);
std::shared_ptr<esp_modem::DTE> dte{nullptr}; std::shared_ptr<esp_modem::DTE> dte{nullptr};
std::unique_ptr<esp_modem::DCE> dce{nullptr}; std::unique_ptr<esp_modem::DCE> dce{nullptr};