ESP-LiveControl  1.99.1
HTTP server, AJAX API backend and Vue.js web application implementing self-contained, zero-install WiFi remote control of hardware modules attached to the Espressif ESP32 SoC
app_state_model.hpp
Go to the documentation of this file.
1 
6 #ifndef APP_STATE_MODEL__
7 #define APP_STATE_MODEL__
8 
9 #include <ArduinoJson.h>
10 
11 #include "ps_pwm.h"
12 
15 #include "app_config.hpp"
16 
17 
18 /***************** AuxHwDrv state with initial values ******************/
23 {
24  float current_limit = 8.0f;
25  bool relay_ref_active = false;
26  bool relay_dut_active = false;
27  float temp_1 = 150.0f;
28  float temp_2 = 150.0f;
29  float temp_1_limit = 50.0f;
30  float temp_2_limit = 50.0f;
31  bool fan_active = true;
32  // Manual override, fan is permanently "on" when true
33  bool fan_override = false;
34  bool drv_supply_active = true;
35  bool drv_disabled = false;
36  // Overtemperature shutdown active flag
37  bool hw_overtemp = true;
38 };
39 
40 
49 struct AppState
50 {
52  //
53  // ATTENTION!
54  // Following constants need to be adapted if JSON object size is changed!
55  static constexpr size_t _key_strings_size = sizeof(
56  "setpoint_throttling_enabled"
57  "base_div"
58  "timer_div"
59  "frequency_min_hw"
60  "frequency_max_hw"
61  "frequency_min"
62  "frequency_max"
63  "frequency"
64  "frequency_changerate"
65  "duty_min"
66  "duty_max"
67  "duty"
68  "duty_changerate"
69  "dt_sum_max_hw"
70  "lead_dt"
71  "lag_dt"
72  "current_limit"
73  "temp_1_limit"
74  "temp_2_limit"
75  "temp_1"
76  "temp_2"
77  "fan_active"
78  "fan_override"
79  "relay_ref_active"
80  "relay_dut_active"
81  "drv_supply_active"
82  "drv_disabled"
83  "power_pwm_active"
84  "hw_oc_fault"
85  "hw_overtemp"
86  "oneshot_len"
87  );
88  // JSON_OBJECT_SIZE is provided with the number of properties as from above
89  static constexpr size_t _json_objects_size = JSON_OBJECT_SIZE(31);
90  // Prevent buffer overflow even if above calculations are wrong...
91  static constexpr size_t I_AM_SCARED_MARGIN = 50;
92  static constexpr size_t json_buf_len = _json_objects_size
93  + _key_strings_size
94  + I_AM_SCARED_MARGIN;
95 
96  // Initial values for AppController()
97  static constexpr AppConstants constants{};
98 
100 
101  // WiFi network configuration
102  NetworkConfig net_conf{};
103 
104  // State from AuxHwDrv module
105  AuxHwDrvState *aux_hw_drv_state = nullptr;
106 
107  // Setpoint throttling (rate of change limiting) takes place when enabled
108  bool setpoint_throttling_enabled = true;
109  // Internal setpoints of PSPWM C API
110  pspwm_clk_conf_t* pspwm_clk_conf = nullptr;
111  pspwm_setpoint_t* pspwm_setpoint = nullptr;
112  pspwm_setpoint_limits_t* pspwm_setpoint_limits = nullptr;
113  // Runtime user setpoint limits
114  float frequency_min = constants.frequency_min;
115  float frequency_max = constants.frequency_max;
116  // Runtime setpoints and throttling increment per fast event timer interval
117  float frequency_target = 100.0e3f;
118  float frequency_increment = 500.0f;
119  float duty_min = 0.0f;
120  float duty_max = 0.8f;
121  float duty_target = 0.0f;
122  float duty_increment = 0.05f;
123  // True when hardware OC shutdown condition is currently present
124  bool hw_oc_fault_present = true;
125  // Hardware Overcurrent Fault Shutdown Status is latched using this flag
126  bool hw_oc_fault_occurred = true;
127  // Pulse length for one-shot mode power output pulse
128  uint32_t oneshot_power_pulse_length_ms = 1;
129 
130 
134  size_t serialize_full_state(char *buf, size_t buf_len);
135 
139  bool deserialize_settings(const char *buf, size_t buf_len);
140 
144  bool save_to_file(const char *filename);
145 
149  bool restore_from_file(const char *filename);
150 };
151 
152 #endif
Application, network and hardware settings, constants and preset configuration.
Driver for the MCPWM hardware modules on the Espressif ESP32 SoC for generating a Phase-Shift-PWM wav...
Constant / compile-time config values go here!
Definition: app_config.hpp:131
Application state containing data and settings model.
Definition: app_state_model.hpp:50
bool deserialize_settings(const char *buf, size_t buf_len)
Restore application runtime configurable settings from json string in buffer back into this instance.
Definition: app_state_model.cpp:90
size_t serialize_full_state(char *buf, size_t buf_len)
Serialize application runtime state and configurable settings into buffer as a JSON string.
Definition: app_state_model.cpp:18
bool restore_from_file(const char *filename)
Restore application runtime configurable settings from SPIFFs file back into this instance.
Definition: app_state_model.cpp:151
bool save_to_file(const char *filename)
Write application runtime configurable settings to SPIFFs file as a JSON string.
Definition: app_state_model.cpp:132
Definition: app_state_model.hpp:23
WiFi network configuration structure with default values.
Definition: app_config.hpp:34
Definition: ps_pwm.h:70
Definition: ps_pwm.h:81
Definition: ps_pwm.h:46