new state machine

This commit is contained in:
Alexandr Pyslar 2024-10-10 17:27:03 +00:00
parent deb18247ed
commit c5580bbbdb
2 changed files with 75 additions and 38 deletions

View file

@ -4,7 +4,6 @@
#include "esphome/core/util.h"
#include "esphome/core/application.h"
#ifdef USE_ESP32
#include "esp_modem_c_api_types.h"
@ -16,14 +15,16 @@
#include <lwip/dns.h>
#include "esp_event.h"
uint32_t time_info_print = 0;
uint32_t time_hard_reset_modem = 0;
uint32_t time_check_rssi = 0;
uint32_t time_check_pwrkey = 0;
uint32_t time_check_reset = 0;
uint32_t time_turn_on_reset = 0;
uint32_t time_turn_on_modem = 0;
uint32_t time_turn_off_modem = 0;
int time_info_print = 0;
int time_hard_reset_modem = 0;
int time_check_rssi = 0;
int time_check_pwrkey = 0;
int time_check_reset = 0;
int time_turn_on_reset = 0;
int time_turn_on_modem = 0;
int time_turn_off_modem = 0;
int last_pull_time = 0;
int time_change_state = 0;
#define TIME_TO_NEXT_HARD_RESET 30000
#define TIME_TO_START_MODEM 9000
@ -98,7 +99,10 @@ void ModemComponent::setup() {
}
void ModemComponent::loop() {
const uint32_t now = millis();
const int now = millis();
if (!ModemComponent::check_modem_component_state_timings()) {
return;
}
switch (this->state_) {
case ModemComponentState::STOPPED:
this->turn_on_modem();
@ -139,7 +143,6 @@ void ModemComponent::loop() {
if (time_info_print < now) {
// ESP_LOGI(TAG, "voltage %dV.", get_modem_voltage() / 1000);
if (esp_netif_is_netif_up(this->modem_netif_)) {
ESP_LOGD(TAG, "esp_netif_is_netif_UP");
} else {
ESP_LOGD(TAG, "esp_netif_is_netif_DOWN");
@ -196,6 +199,18 @@ void ModemComponent::dce_init(){
this->dce = esp_modem::create_SIM800_dce(&dce_config, dte, this->modem_netif_);
}
bool ModemComponent::check_modem_component_state_timings() {
const int now = millis();
ModemComponentStateTiming timing = this->modemComponentStateTimingMap[this->state_];
if (last_pull_time + timing.poll_period > now) {
last_pull_time = now;
return true;
} else if (time_change_state + timing.time_limit < now) {
return true;
}
return false;
}
void ModemComponent::turn_on_modem() {
if (power_pin_) {
this->power_pin_->digital_write(true);
@ -303,7 +318,7 @@ network::IPAddress ModemComponent::get_ip_address() {
return network::IPAddress(&ip.ip);
}
void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int event_id, void *event_data) {
ESP_LOGD(TAG, "IP event! %" PRIu32, event_id);
if (event_id == IP_EVENT_PPP_GOT_IP) {
global_modem_component->connected_ = true;
@ -364,8 +379,8 @@ void ModemComponent::set_pwrkey_pin(InternalGPIOPin *pwrkey_pin) { this->pwrkey_
void ModemComponent::set_type(ModemType type) { this->type_ = type; }
void ModemComponent::set_reset_pin(InternalGPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
void ModemComponent::set_apn(const std::string &apn) { this->apn_ = apn; }
void ModemComponent::set_tx_pin(int tx_pin) { this->tx_pin_ = tx_pin; }
void ModemComponent::set_rx_pin(int rx_pin) { this->rx_pin_ = rx_pin; }
void ModemComponent::set_tx_pin(uint8_t tx_pin) { this->tx_pin_ = tx_pin; }
void ModemComponent::set_rx_pin(uint8_t rx_pin) { this->rx_pin_ = rx_pin; }
void ModemComponent::set_uart_event_task_stack_size(int uart_event_task_stack_size) {
this->uart_event_task_stack_size_ = uart_event_task_stack_size;
}

View file

@ -6,9 +6,9 @@
#include "esphome/core/hal.h"
#include "esphome/components/network/ip_address.h"
#ifdef USE_ESP32
#include <map>
using esphome::esp_log_printf_;
#include "esp_netif.h"
@ -36,6 +36,13 @@ enum class ModemComponentState {
TURNING_OFF_POWER,
};
struct ModemComponentStateTiming {
int time_limit;
int poll_period;
ModemComponentStateTiming() : time_limit(0), poll_period(0) {}
ModemComponentStateTiming(int time_limit, int poll_period) : time_limit(time_limit), poll_period(poll_period) {}
};
class ModemComponent : public Component {
public:
ModemComponent();
@ -51,8 +58,8 @@ class ModemComponent : public Component {
void set_type(ModemType type);
void set_reset_pin(InternalGPIOPin *reset_pin);
void set_apn(const std::string &apn);
void set_tx_pin(int tx_pin);
void set_rx_pin(int rx_pin);
void set_tx_pin(uint8_t tx_pin);
void set_rx_pin(uint8_t rx_pin);
void set_uart_event_task_stack_size(int uart_event_task_stack_size);
void set_uart_event_task_priority(int uart_event_task_priority);
void set_uart_event_queue_size(int uart_event_queue_size);
@ -65,11 +72,23 @@ class ModemComponent : public Component {
bool powerdown();
protected:
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
std::map<ModemComponentState, ModemComponentStateTiming> modemComponentStateTimingMap = {
{ModemComponentState::STOPPED, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_POWER, ModemComponentStateTiming(1000, 10000)},
{ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(1000, 5000)},
{ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(1000, 4000)},
{ModemComponentState::CONNECTING, ModemComponentStateTiming(1000, 4000)},
{ModemComponentState::CONNECTED, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_RESET, ModemComponentStateTiming(1000, 6000)},
{ModemComponentState::TURNING_OFF_POWER, ModemComponentStateTiming(0, 0)},
};
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int event_id, void *event_data);
void modem_netif_init();
void dte_init();
void dce_init();
bool check_modem_component_state_timings();
void turn_on_modem();
void turn_off_modem();
void turn_on_pwrkey();
@ -86,8 +105,8 @@ class ModemComponent : public Component {
InternalGPIOPin *reset_pin_{nullptr};
InternalGPIOPin *power_pin_{nullptr};
InternalGPIOPin *pwrkey_pin_{nullptr};
int tx_pin_{-1};
int rx_pin_{-1};
uint8_t tx_pin_{0};
uint8_t rx_pin_{0};
std::string apn_{""};
std::string use_address_;
int uart_event_task_stack_size_{0};
@ -96,11 +115,14 @@ class ModemComponent : public Component {
int uart_tx_buffer_size_{0};
int uart_rx_buffer_size_{0};
int pull_time_{0};
int change_state_{0};
bool started_{false};
bool connected_{false};
ModemComponentState state_{ModemComponentState::STOPPED};
uint32_t connect_begin_;
int connect_begin_;
esp_netif_t *modem_netif_{nullptr};
// esp_eth_phy_t *phy_{nullptr};
};