edit state machine

This commit is contained in:
Chelios 2024-10-11 08:21:49 +03:00
parent 88f0d365b6
commit d7d6cbfe34
3 changed files with 42 additions and 25 deletions

View file

@ -99,7 +99,9 @@ async def to_code(config):
cg.add_library("WiFi", None)
if CORE.using_esp_idf:
add_idf_sdkconfig_option("CONFIG_LWIP_PPP_SUPPORT", True)
# add_idf_sdkconfig_option("CONFIG_TASK_WDT_TIMEOUT_S", 10)
add_idf_sdkconfig_option("CONFIG_TASK_WDT_TIMEOUT_S", 60)
add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT", False)
add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE", True)
add_idf_component(
name="esp_modem",
repo="https://github.com/espressif/esp-protocols.git",

View file

@ -148,18 +148,12 @@ void ModemComponent::loop() {
}
break;
case ModemComponentState::TURNING_ON_RESET:
if (time_turn_on_reset + DELAY_HOLD_RESET_PIN > now) {
break;
}
if (time_check_reset + SYNCHRONIZATION_CHECK_PERIOD < now) {
time_check_reset = now;
if (this->dce->sync() == esp_modem::command_result::OK) {
ESP_LOGD(TAG, "sync OK TURNING_ON_RESET");
this->turn_off_reset();
this->set_state(ModemComponentState::REGISTRATION_IN_NETWORK);
} else {
ESP_LOGD(TAG, "Wait sync 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->set_state(ModemComponentState::REGISTRATION_IN_NETWORK);
} else {
ESP_LOGD(TAG, "Wait sync TURNING_ON_RESET");
}
break;
case ModemComponentState::TURNING_OFF_POWER:
@ -199,6 +193,9 @@ void ModemComponent::dce_init() {
bool ModemComponent::check_modem_component_state_timings() {
const int now = millis();
ModemComponentStateTiming timing = this->modemComponentStateTimingMap[this->state_];
if (timing.time_limit && ((time_change_state + timing.time_limit) < now)){
this->turn_on_reset();
}
if (!timing.poll_period){
return true;
}
@ -207,20 +204,37 @@ bool ModemComponent::check_modem_component_state_timings() {
last_pull_time = now;
return true;
}
if (!timing.time_limit){
if ((time_change_state + timing.time_limit) < now) {
// TODO: REBOOT return true;
return false;
}
}
return false;
}
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));
this->state_ = state;
time_change_state = millis();
}
const char *ModemComponent::state_to_string(ModemComponentState state) {
switch (state) {
case ModemComponentState::STOPPED:
return "STOPPED";
case ModemComponentState::TURNING_ON_POWER:
return "TURNING_ON_POWER";
case ModemComponentState::TURNING_ON_PWRKEY:
return "TURNING_ON_PWRKEY";
case ModemComponentState::REGISTRATION_IN_NETWORK:
return "REGISTRATION_IN_NETWORK";
case ModemComponentState::CONNECTING:
return "CONNECTING";
case ModemComponentState::CONNECTED:
return "CONNECTED";
case ModemComponentState::TURNING_ON_RESET:
return "TURNING_ON_RESET";
case ModemComponentState::TURNING_OFF_POWER:
return "TURNING_OFF_POWER";
}
return "UNKNOWN";
}
void ModemComponent::turn_on_modem() {
if (power_pin_) {
this->power_pin_->digital_write(true);
@ -260,7 +274,7 @@ void ModemComponent::turn_off_pwrkey() {
void ModemComponent::turn_on_reset() {
this->reset_pin_->digital_write(false);
ESP_LOGD(TAG, "turn on reset");
this->set_state(ModemComponentState::TURNING_ON_PWRKEY);
this->set_state(ModemComponentState::TURNING_ON_RESET);
}
void ModemComponent::turn_off_reset() {
@ -350,7 +364,7 @@ void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base
ESP_LOGD(TAG, "GOT ip event!!!");
} else if (event_id == IP_EVENT_PPP_LOST_IP) {
ESP_LOGD(TAG, "Modem Disconnect from PPP Server");
global_modem_component->turn_off_modem();
global_modem_component->turn_on_reset();
}
}

View file

@ -75,11 +75,11 @@ class ModemComponent : public Component {
std::map<ModemComponentState, ModemComponentStateTiming> modemComponentStateTimingMap = {
{ModemComponentState::STOPPED, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_POWER, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(1000, 20000)},
{ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(1000, 20000)},
{ModemComponentState::CONNECTING, ModemComponentStateTiming(1000, 0)},
{ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::CONNECTING, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::CONNECTED, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_RESET, ModemComponentStateTiming(2000, 0)},
{ModemComponentState::TURNING_ON_RESET, ModemComponentStateTiming(110, 0)},
{ModemComponentState::TURNING_OFF_POWER, ModemComponentStateTiming(2000, 0)},
};
@ -100,6 +100,7 @@ class ModemComponent : public Component {
int get_modem_voltage();
const char *get_state();
void set_state(ModemComponentState state);
const char *state_to_string(ModemComponentState state);
std::shared_ptr<esp_modem::DTE> dte{nullptr};
std::unique_ptr<esp_modem::DCE> dce{nullptr};