add SYNC FSM

This commit is contained in:
Alexandr Pyslar 2024-10-11 14:34:58 +00:00
parent d7d6cbfe34
commit 95ae144e70
2 changed files with 21 additions and 21 deletions

View file

@ -112,12 +112,14 @@ void ModemComponent::loop() {
this->dce_init(); this->dce_init();
break; break;
case ModemComponentState::TURNING_ON_PWRKEY: case ModemComponentState::TURNING_ON_PWRKEY:
if (this->dce->sync() == esp_modem::command_result::OK) {
ESP_LOGD(TAG, "sync OK TURNING_ON_PWRKEY");
this->turn_off_pwrkey(); this->turn_off_pwrkey();
break;
case ModemComponentState::SYNC:
if (this->dce->sync() == esp_modem::command_result::OK) {
ESP_LOGD(TAG, "sync OK");
this->set_state(ModemComponentState::REGISTRATION_IN_NETWORK); this->set_state(ModemComponentState::REGISTRATION_IN_NETWORK);
} else { } else {
ESP_LOGD(TAG, "Wait sync TURNING_ON_PWRKEY"); ESP_LOGD(TAG, "Wait sync");
} }
break; break;
@ -148,14 +150,7 @@ void ModemComponent::loop() {
} }
break; break;
case ModemComponentState::TURNING_ON_RESET: case ModemComponentState::TURNING_ON_RESET:
if (this->dce->sync() == esp_modem::command_result::OK) {
ESP_LOGD(TAG, "sync OK TURNING_ON_RESET");
this->turn_off_reset(); this->turn_off_reset();
this->set_state(ModemComponentState::REGISTRATION_IN_NETWORK);
} else {
ESP_LOGD(TAG, "Wait sync TURNING_ON_RESET");
}
break;
case ModemComponentState::TURNING_OFF_POWER: case ModemComponentState::TURNING_OFF_POWER:
if (time_turn_off_modem + TURN_OFF_MODEM_TIME < now) { if (time_turn_off_modem + TURN_OFF_MODEM_TIME < now) {
this->turn_on_modem(); this->turn_on_modem();
@ -208,7 +203,8 @@ bool ModemComponent::check_modem_component_state_timings() {
} }
void ModemComponent::set_state(ModemComponentState state) { void ModemComponent::set_state(ModemComponentState state) {
ESP_LOGCONFIG(TAG, "Mode component change state from %s to %s", this->state_to_string(this->state_), this->state_to_string(state)); ESP_LOGCONFIG(TAG, "Mode component change state from %s to %s", this->state_to_string(this->state_),
this->state_to_string(state));
this->state_ = state; this->state_ = state;
time_change_state = millis(); time_change_state = millis();
} }
@ -221,6 +217,8 @@ const char *ModemComponent::state_to_string(ModemComponentState state) {
return "TURNING_ON_POWER"; return "TURNING_ON_POWER";
case ModemComponentState::TURNING_ON_PWRKEY: case ModemComponentState::TURNING_ON_PWRKEY:
return "TURNING_ON_PWRKEY"; return "TURNING_ON_PWRKEY";
case ModemComponentState::SYNC:
return "SYNC";
case ModemComponentState::REGISTRATION_IN_NETWORK: case ModemComponentState::REGISTRATION_IN_NETWORK:
return "REGISTRATION_IN_NETWORK"; return "REGISTRATION_IN_NETWORK";
case ModemComponentState::CONNECTING: case ModemComponentState::CONNECTING:
@ -269,6 +267,7 @@ void ModemComponent::turn_on_pwrkey() {
void ModemComponent::turn_off_pwrkey() { void ModemComponent::turn_off_pwrkey() {
ESP_LOGD(TAG, "pwrkey turn off"); ESP_LOGD(TAG, "pwrkey turn off");
this->pwrkey_pin_->digital_write(true); this->pwrkey_pin_->digital_write(true);
this->set_state(ModemComponentState::SYNC);
} }
void ModemComponent::turn_on_reset() { void ModemComponent::turn_on_reset() {
@ -280,6 +279,7 @@ void ModemComponent::turn_on_reset() {
void ModemComponent::turn_off_reset() { void ModemComponent::turn_off_reset() {
this->reset_pin_->digital_write(true); this->reset_pin_->digital_write(true);
ESP_LOGD(TAG, "turn off reset"); ESP_LOGD(TAG, "turn off reset");
this->set_state(ModemComponentState::SYNC);
} }
void ModemComponent::dump_config() { void ModemComponent::dump_config() {
@ -395,9 +395,7 @@ void ModemComponent::start_connect_() {
// this->status_set_warning(); // this->status_set_warning();
} }
bool ModemComponent::is_connected() { bool ModemComponent::is_connected() { 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; }
void ModemComponent::set_pwrkey_pin(InternalGPIOPin *pwrkey_pin) { this->pwrkey_pin_ = pwrkey_pin; } void ModemComponent::set_pwrkey_pin(InternalGPIOPin *pwrkey_pin) { this->pwrkey_pin_ = pwrkey_pin; }
void ModemComponent::set_type(ModemType type) { this->type_ = type; } void ModemComponent::set_type(ModemType type) { this->type_ = type; }

View file

@ -29,6 +29,7 @@ enum class ModemComponentState {
STOPPED, STOPPED,
TURNING_ON_POWER, TURNING_ON_POWER,
TURNING_ON_PWRKEY, TURNING_ON_PWRKEY,
SYNC,
REGISTRATION_IN_NETWORK, REGISTRATION_IN_NETWORK,
CONNECTING, CONNECTING,
CONNECTED, CONNECTED,
@ -76,6 +77,7 @@ class ModemComponent : public Component {
{ModemComponentState::STOPPED, ModemComponentStateTiming(0, 0)}, {ModemComponentState::STOPPED, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_POWER, ModemComponentStateTiming(0, 0)}, {ModemComponentState::TURNING_ON_POWER, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::SYNC, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::CONNECTING, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::CONNECTING, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::CONNECTED, ModemComponentStateTiming(0, 0)}, {ModemComponentState::CONNECTED, ModemComponentStateTiming(0, 0)},