wipstate machine, rename g_Settings to gSettings

master
Ondřej Hruška 2 years ago
parent 5937417568
commit cbf3ebd8e0
  1. 2
      CMakeLists.txt
  2. 3
      main/CMakeLists.txt
  3. 2
      main/Kconfig.projbuild
  4. 16
      main/actuators.c
  5. 5
      main/actuators.h
  6. 2
      main/console/commands/cmd_dump.c
  7. 44
      main/console/commands/cmd_ip.c
  8. 4
      main/console/commands/cmd_pw.c
  9. 28
      main/console/commands/cmd_wifi.c
  10. 6
      main/console/console_ioimpl.c
  11. 113
      main/fancontrol.c
  12. 51
      main/fancontrol.h
  13. 20
      main/settings.c
  14. 14
      main/settings.h
  15. 4
      main/sntp_cli.c
  16. 4
      main/user_main.c
  17. 34
      main/wifi_conn.c
  18. 4
      sdkconfig

@ -3,4 +3,4 @@
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(fanctl)
project(rekuperator)

@ -1,5 +1,5 @@
idf_component_register(SRCS
fanctl_main.c
user_main.c
settings.c
shutdown_handlers.c
sntp_cli.c
@ -7,6 +7,7 @@ idf_component_register(SRCS
wifi_conn.c
mbiface.c
actuators.c
fancontrol.c
console/console_ioimpl.c
console/console_server.c
console/register_cmds.c

@ -1,4 +1,4 @@
menu "IRBLASTER Configuration"
menu "Project configuration"
menu "Pin mapping"
config PIN_BUZZER

@ -6,7 +6,7 @@
#include "actuators.h"
#include "settings.h"
struct actuators_status g_Act = {
struct actuators_status gAct = {
.blind = false,
.dir = MOTOR_DIR_IN,
.power = 0,
@ -35,7 +35,7 @@ static void buzzer_init() {
}
void act_buzzer_set(bool on) {
g_Act.buzzer = on;
gAct.buzzer = on;
if (on) {
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 127);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
@ -77,10 +77,10 @@ static void motor_init() {
void act_motor_power_set(uint16_t perc) {
if (perc > 0) {
if (perc < g_Settings.min_power) {
perc = g_Settings.min_power;
if (perc < gSettings.min_power) {
perc = gSettings.min_power;
}
g_Act.power = perc;
gAct.power = perc;
uint16_t duty = (uint16_t) (10.23f * (float)perc);
if (duty > 1023) {
duty = 1023;
@ -93,7 +93,7 @@ void act_motor_power_set(uint16_t perc) {
}
void act_motor_direction_set(enum motor_direction dir) {
g_Act.dir = dir;
gAct.dir = dir;
gpio_set_level(CONFIG_PIN_DIR, (int) dir); // TODO verify polarity
}
@ -107,7 +107,7 @@ static void blind_init() {
}
void act_blind_set(bool open) {
g_Act.blind = open;
gAct.blind = open;
gpio_set_level(CONFIG_PIN_BLIND, (int) open); // TODO verify polarity
}
@ -120,7 +120,7 @@ static void led_init() {
}
void act_led_set(bool on) {
g_Act.led = on;
gAct.led = on;
gpio_set_level(CONFIG_PIN_LED, (int) on); // TODO verify polarity
}

@ -5,6 +5,9 @@
#ifndef FANCTL_ACTUATORS_H
#define FANCTL_ACTUATORS_H
#include <stdbool.h>
#include <stdint.h>
enum motor_direction {
MOTOR_DIR_IN = 0,
MOTOR_DIR_OUT = 1
@ -18,7 +21,7 @@ struct actuators_status {
uint16_t power;
};
extern struct actuators_status g_Act;
extern struct actuators_status gAct;
void act_init();
void act_buzzer_set(bool on);

@ -21,7 +21,7 @@ static int cmd_dump(console_ctx_t *ctx, cmd_signature_t *reg)
return 0;
}
console_printf("WiFi enabled: %d\r\n", g_Settings.wifi_enabled);
console_printf("WiFi enabled: %d\r\n", gSettings.wifi_enabled);
// TODO show more settings

@ -16,7 +16,7 @@ static int cmd_ip_status(console_ctx_t *ctx, cmd_signature_t *reg)
{
EMPTY_CMD_SETUP("Show IP status");
if (!g_Settings.wifi_enabled || !g_State.wifi_inited) {
if (!gSettings.wifi_enabled || !g_State.wifi_inited) {
console_color_printf(COLOR_RED, "WiFi interface is disabled\n");
return 0;
}
@ -40,13 +40,13 @@ static int cmd_ip_status(console_ctx_t *ctx, cmd_signature_t *reg)
tcpip_adapter_ip_info_t ipinfo;
if (ESP_OK == tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipinfo)) {
// ! inet_ntoa uses a global static buffer, cant use multiple in one printf call
console_printf("DHCP: %s\n", g_Settings.dhcp_enable ? MSG_ENABLED : MSG_DISABLED " (static IP)");
console_printf("DHCP: %s\n", gSettings.dhcp_enable ? MSG_ENABLED : MSG_DISABLED " (static IP)");
console_printf("IP: %s\n", inet_ntoa(ipinfo.ip.addr));
console_printf("Mask: %s\n", inet_ntoa(ipinfo.netmask.addr));
console_printf("Gateway: %s\n", inet_ntoa(ipinfo.gw.addr));
if (!g_Settings.dhcp_enable) {
console_printf("DNS: %s\n", inet_ntoa(g_Settings.static_dns));
if (!gSettings.dhcp_enable) {
console_printf("DNS: %s\n", inet_ntoa(gSettings.static_dns));
}
uint8_t mac[6];
@ -85,11 +85,11 @@ static int cmd_ip_pingwd(console_ctx_t *ctx, cmd_signature_t *reg)
if (cmd_args.cmd->count) {
int b = parse_boolean_arg(cmd_args.cmd->sval[0]);
if (b < 0) return CONSOLE_ERR_INVALID_ARG;
g_Settings.dhcp_wd_enable = b;
gSettings.dhcp_wd_enable = b;
settings_persist(SETTINGS_dhcp_wd_enable);
}
console_printf("Ping WD = %s\n", g_Settings.dhcp_wd_enable? MSG_ENABLED : MSG_DISABLED);
console_printf("Ping WD = %s\n", gSettings.dhcp_wd_enable ? MSG_ENABLED : MSG_DISABLED);
if (cmd_args.cmd->count) {
console_printf("Restart to apply changes\n");
@ -200,7 +200,7 @@ static int cmd_ip_static_set(console_ctx_t *ctx, cmd_signature_t *reg)
console_println("Invalid IP");
return CONSOLE_ERR_INVALID_ARG;
}
g_Settings.static_ip = a; // aton output is already in network byte order
gSettings.static_ip = a; // aton output is already in network byte order
settings_persist(SETTINGS_static_ip);
any_change = true;
}
@ -211,7 +211,7 @@ static int cmd_ip_static_set(console_ctx_t *ctx, cmd_signature_t *reg)
console_println("Invalid GW");
return CONSOLE_ERR_INVALID_ARG;
}
g_Settings.static_ip_gw = a; // aton output is already in network byte order
gSettings.static_ip_gw = a; // aton output is already in network byte order
settings_persist(SETTINGS_static_ip_gw);
any_change = true;
}
@ -222,7 +222,7 @@ static int cmd_ip_static_set(console_ctx_t *ctx, cmd_signature_t *reg)
console_println("Invalid mask");
return CONSOLE_ERR_INVALID_ARG;
}
g_Settings.static_ip_mask = a; // aton output is already in network byte order
gSettings.static_ip_mask = a; // aton output is already in network byte order
settings_persist(SETTINGS_static_ip_mask);
any_change = true;
}
@ -233,7 +233,7 @@ static int cmd_ip_static_set(console_ctx_t *ctx, cmd_signature_t *reg)
console_println("Invalid DNS IP");
return CONSOLE_ERR_INVALID_ARG;
}
g_Settings.static_dns = a; // aton output is already in network byte order
gSettings.static_dns = a; // aton output is already in network byte order
settings_persist(SETTINGS_static_dns);
any_change = true;
}
@ -241,16 +241,16 @@ static int cmd_ip_static_set(console_ctx_t *ctx, cmd_signature_t *reg)
if (args.cmd->count) {
int b = parse_boolean_arg(args.cmd->sval[0]);
if (b < 0) return CONSOLE_ERR_INVALID_ARG;
g_Settings.dhcp_enable = !b;
gSettings.dhcp_enable = !b;
settings_persist(SETTINGS_dhcp_enable);
any_change = true;
}
console_printf("Static IP: %s\n", g_Settings.dhcp_enable ? MSG_DISABLED : MSG_ENABLED);
console_printf("- IP: %s\n", inet_ntoa(g_Settings.static_ip));
console_printf("- Mask: %s\n", inet_ntoa(g_Settings.static_ip_mask));
console_printf("- Gateway: %s\n", inet_ntoa(g_Settings.static_ip_gw));
console_printf("- DNS: %s\n", inet_ntoa(g_Settings.static_dns));
console_printf("Static IP: %s\n", gSettings.dhcp_enable ? MSG_DISABLED : MSG_ENABLED);
console_printf("- IP: %s\n", inet_ntoa(gSettings.static_ip));
console_printf("- Mask: %s\n", inet_ntoa(gSettings.static_ip_mask));
console_printf("- Gateway: %s\n", inet_ntoa(gSettings.static_ip_gw));
console_printf("- DNS: %s\n", inet_ntoa(gSettings.static_dns));
if (any_change) {
console_println("Restart to apply changes.");
@ -278,10 +278,10 @@ static int cmd_ip_ntp(console_ctx_t *ctx, cmd_signature_t *reg)
}
if (cmd_args.addr->count) {
strncpy(g_Settings.ntp_srv, cmd_args.addr->sval[0], NTP_SRV_LEN);
g_Settings.ntp_srv[NTP_SRV_LEN-1] = 0;
strncpy(gSettings.ntp_srv, cmd_args.addr->sval[0], NTP_SRV_LEN);
gSettings.ntp_srv[NTP_SRV_LEN - 1] = 0;
settings_persist(SETTINGS_ntp_srv);
console_printf("NTP server set to %s\n", g_Settings.ntp_srv);
console_printf("NTP server set to %s\n", gSettings.ntp_srv);
}
if (cmd_args.cmd->count) {
@ -297,13 +297,13 @@ static int cmd_ip_ntp(console_ctx_t *ctx, cmd_signature_t *reg)
int b = parse_boolean_arg(cmd_args.cmd->sval[0]);
if (b < 0) return CONSOLE_ERR_INVALID_ARG;
g_Settings.ntp_enable = b;
gSettings.ntp_enable = b;
settings_persist(SETTINGS_ntp_enable);
}
console_printf("Client status: %s\n", g_Settings.ntp_enable? MSG_ENABLED : MSG_DISABLED);
console_printf("NTP server: %s\n", g_Settings.ntp_srv);
console_printf("Client status: %s\n", gSettings.ntp_enable ? MSG_ENABLED : MSG_DISABLED);
console_printf("NTP server: %s\n", gSettings.ntp_srv);
/* show the current date */
time_t now;

@ -16,7 +16,7 @@ static int cmd_clear(console_ctx_t *ctx, cmd_signature_t *reg)
{
EMPTY_CMD_SETUP("Clear access password");
console_printf("Access password cleared.\n");
g_Settings.console_pw[0] = 0;
gSettings.console_pw[0] = 0;
settings_persist(SETTINGS_console_pw);
return 0;
}
@ -37,7 +37,7 @@ static int cmd_set(console_ctx_t *ctx, cmd_signature_t *reg)
return CONSOLE_OK;
}
strncpy(g_Settings.console_pw, args.pw->sval[0], CONSOLE_PW_LEN-1);
strncpy(gSettings.console_pw, args.pw->sval[0], CONSOLE_PW_LEN - 1);
console_printf("Access pw set to: \"%.*s\"\n", CONSOLE_PW_LEN, args.pw->sval[0]);
settings_persist(SETTINGS_console_pw);
return 0;

@ -17,7 +17,7 @@ static int cmd_disable(console_ctx_t *ctx, cmd_signature_t *reg)
{
EMPTY_CMD_SETUP("Disable WiFi");
console_printf("WiFi "MSG_DISABLED"\nRestart to apply.\n");
g_Settings.wifi_enabled = false;
gSettings.wifi_enabled = false;
settings_persist(SETTINGS_wifi_enabled);
return 0;
}
@ -26,7 +26,7 @@ static int cmd_enable(console_ctx_t *ctx, cmd_signature_t *reg)
{
EMPTY_CMD_SETUP("Enable WiFi");
console_printf("WiFi "MSG_ENABLED"\nRestart to apply.\n");
g_Settings.wifi_enabled = true;
gSettings.wifi_enabled = true;
settings_persist(SETTINGS_wifi_enabled);
return 0;
}
@ -70,13 +70,13 @@ static int cmd_ap_conf(console_ctx_t *ctx, cmd_signature_t *reg)
switch (match) {
case 0:
console_printf("AP mode "MSG_DISABLED"\nRestart to apply.\n");
g_Settings.ap_enabled = false;
gSettings.ap_enabled = false;
settings_persist(SETTINGS_ap_enabled);
break;
case 1:
console_printf("AP mode "MSG_ENABLED"\nRestart to apply.\n");
g_Settings.ap_enabled = true;
gSettings.ap_enabled = true;
settings_persist(SETTINGS_ap_enabled);
break;
@ -85,7 +85,7 @@ static int cmd_ap_conf(console_ctx_t *ctx, cmd_signature_t *reg)
}
} else {
// No cmd
console_printf("AP mode: %s\n", g_Settings.ap_enabled? MSG_ENABLED: MSG_DISABLED);
console_printf("AP mode: %s\n", gSettings.ap_enabled ? MSG_ENABLED : MSG_DISABLED);
}
if (args.ip->count) {
@ -94,7 +94,7 @@ static int cmd_ap_conf(console_ctx_t *ctx, cmd_signature_t *reg)
console_println("Invalid IP");
return CONSOLE_ERR_INVALID_ARG;
}
g_Settings.ap_ip = a; // aton output is already in network byte order
gSettings.ap_ip = a; // aton output is already in network byte order
settings_persist(SETTINGS_ap_ip);
console_println("AP IP changed, restart to apply.\n");
@ -138,7 +138,7 @@ static int cmd_ap_conf(console_ctx_t *ctx, cmd_signature_t *reg)
console_printf("AP SSID: \"%s\"\n", apconf.ap.ssid);
console_printf("AP PW: \"%s\"\n", apconf.ap.password);
console_printf("AP own IP: %s/24\n", inet_ntoa(g_Settings.ap_ip));
console_printf("AP own IP: %s/24\n", inet_ntoa(gSettings.ap_ip));
return 0;
}
@ -169,13 +169,13 @@ static int cmd_sta_conf(console_ctx_t *ctx, cmd_signature_t *reg)
switch (match) {
case 0:
console_printf("STA mode "MSG_DISABLED"\nRestart to apply.\n");
g_Settings.sta_enabled = false;
gSettings.sta_enabled = false;
settings_persist(SETTINGS_sta_enabled);
break;
case 1:
console_printf("STA mode "MSG_ENABLED"\nRestart to apply.\n");
g_Settings.sta_enabled = true;
gSettings.sta_enabled = true;
settings_persist(SETTINGS_sta_enabled);
break;
@ -184,7 +184,7 @@ static int cmd_sta_conf(console_ctx_t *ctx, cmd_signature_t *reg)
}
} else {
// No cmd
console_printf("STA mode: %s\n", g_Settings.sta_enabled? MSG_ENABLED: MSG_DISABLED);
console_printf("STA mode: %s\n", gSettings.sta_enabled ? MSG_ENABLED : MSG_DISABLED);
}
return 0;
@ -309,13 +309,13 @@ static int cmd_wifi_status(console_ctx_t *ctx, cmd_signature_t *reg)
return 0;
}
console_printf("WiFi support: %s\n", g_Settings.wifi_enabled ? MSG_ENABLED : MSG_DISABLED);
console_printf("STA mode: %s\n", g_Settings.sta_enabled? MSG_ENABLED: MSG_DISABLED);
console_printf("AP mode: %s\n", g_Settings.ap_enabled? MSG_ENABLED: MSG_DISABLED);
console_printf("WiFi support: %s\n", gSettings.wifi_enabled ? MSG_ENABLED : MSG_DISABLED);
console_printf("STA mode: %s\n", gSettings.sta_enabled ? MSG_ENABLED : MSG_DISABLED);
console_printf("AP mode: %s\n", gSettings.ap_enabled ? MSG_ENABLED : MSG_DISABLED);
console_printf("\n");
if (g_Settings.wifi_enabled) {
if (gSettings.wifi_enabled) {
wifi_config_t config;
if (ESP_OK == esp_wifi_get_config(ESP_IF_WIFI_STA, &config)) {
if (config.sta.ssid[0]) {

@ -70,9 +70,9 @@ static void my_console_task_freertos(void *param) {
assert(CONSOLE_IOIMPL_MAGIC == io->__magic);
if (io->kind == CONSOLE_IO_TELNET) {
const size_t pwlen = strnlen(g_Settings.console_pw, CONSOLE_PW_LEN);
const size_t pwlen = strnlen(gSettings.console_pw, CONSOLE_PW_LEN);
if (pwlen != 0) {
ESP_LOGE(TAG, "Pw=\"%.*s\"", pwlen, g_Settings.console_pw);
ESP_LOGE(TAG, "Pw=\"%.*s\"", pwlen, gSettings.console_pw);
console_print_ctx(&io->ctx, "Password: ");
@ -94,7 +94,7 @@ static void my_console_task_freertos(void *param) {
}
}
if (0 == strncmp(buf, g_Settings.console_pw, CONSOLE_PW_LEN)) {
if (0 == strncmp(buf, gSettings.console_pw, CONSOLE_PW_LEN)) {
console_print_ctx(&io->ctx, "Login OK!\n");
} else {
console_print_ctx(&io->ctx, "Login failed!\n");

@ -0,0 +1,113 @@
//
// Created by MightyPork on 2022/08/20.
//
#include "fancontrol.h"
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "settings.h"
#include "actuators.h"
struct FanControlState gState = {};
static void timerCallback(TimerHandle_t xTimer);
void settings_blind_time_set(uint16_t blind_time) {
bool nadoraz = (gState.blind_position >= gSettings.blind_time) || (gState.blind_position >= blind_time);
gSettings.blind_time = blind_time;
if (nadoraz) {
gState.blind_position = blind_time;
}
settings_persist(SETTINGS_blind_time);
}
void fancontrol_init() {
gState.vent_mode = gSettings.initial_mode;
gState.set_power = gSettings.initial_power;
xTimerCreate("fanctl",
pdMS_TO_TICKS(1000),
pdTRUE,
NULL,
timerCallback);
}
static void timerCallback(TimerHandle_t xTimer) {
// posun rolety
if (gAct.blind) {
if (gState.blind_position < gSettings.blind_time) {
gState.blind_position++;
}
} else {
if (gState.blind_position > 0) {
gState.blind_position--;
}
}
if (gAct.power > 0) {
if (gState.real_direction != gAct.dir) {
if (gState.ramp > 0) {
gState.ramp--;
} else {
gState.run_time = 0;
gState.real_direction = gAct.dir;
}
} else {
if (gState.ramp < gSettings.ramp_time) {
gState.ramp++;
}
}
gState.run_time++;
} else {
if (gState.ramp > 0) {
gState.ramp--;
} else {
gState.run_time = 0;
}
}
switch (gState.vent_mode) {
case VENT_MODE_OFF:
act_motor_power_set(0);
act_blind_set(0);
break;
case VENT_MODE_FREE:
act_motor_power_set(0);
act_blind_set(1);
break;
case VENT_MODE_OUT:
act_motor_direction_set(MOTOR_DIR_OUT);
act_blind_set(1);
if (gState.blind_position >= gSettings.blind_time) {
act_motor_power_set(gState.set_power);
}
break;
case VENT_MODE_IN:
act_motor_direction_set(MOTOR_DIR_IN);
act_blind_set(1);
if (gState.blind_position >= gSettings.blind_time) {
act_motor_power_set(gState.set_power);
}
break;
case VENT_MODE_RECUP:
act_blind_set(1);
if (gState.blind_position >= gSettings.blind_time) {
act_motor_power_set(gState.set_power);
}
// Stop condition
if (gState.run_time >= gSettings.recup_time) {
// zmena smeru
gState.run_time = 0;
act_motor_direction_set(1 - gAct.dir);
}
break;
}
if (gAct.dir == gState.real_direction && gState.ramp >= gSettings.ramp_time) {
// Measure temperatures
}
}

@ -0,0 +1,51 @@
//
// Created by MightyPork on 2022/08/20.
//
#ifndef FANCTL_FANCONTROL_H
#define FANCTL_FANCONTROL_H
#include <stdint.h>
#include "actuators.h"
void fancontrol_init();
enum ventilation_mode {
VENT_MODE_OFF = 0,
VENT_MODE_FREE = 1,
VENT_MODE_OUT = 3,
VENT_MODE_IN = 5,
VENT_MODE_RECUP = 7,
};
struct FanControlState {
/**
* Poloha roletky, 0<->blind_time, inkrement/dekrement 1 za sekundu.
* Pri zmene blind_time se musi hodnota aktualizovat, pokud je na doraze nebo za.
*/
uint16_t blind_position;
/**
* Power requested trough register or as the default value
*/
uint16_t set_power;
/**
* Cas chodu motoru v aktualnim smeru, inkrement 1 za sekundu.
*/
uint16_t run_time;
/**
* "stav chodu motoru". 0=stop, ramp_time = jede.
*/
uint16_t ramp;
/**
* skutecny aktualni pohyb motoru (zustava stejny, dokud ramp time nedosahne nuly)
*/
enum motor_direction real_direction;
/**
* rezim ventilace
*/
enum ventilation_mode vent_mode;
};
extern struct FanControlState gState;
#endif //FANCTL_FANCONTROL_H

@ -19,7 +19,7 @@ const struct cspemu_settings defaults = {
};
struct cspemu_settings g_Settings;
struct cspemu_settings gSettings;
struct cspemu_globals g_State = {
.wifi_inited = false,
};
@ -74,7 +74,7 @@ static esp_err_t nvs_set_none(nvs_handle handle, const char* key, void* value) {
void settings_load(void)
{
esp_err_t rv;
memcpy(&g_Settings, &defaults, sizeof(struct cspemu_settings));
memcpy(&gSettings, &defaults, sizeof(struct cspemu_settings));
// abort on failure other than "not found"
#define NVSCHECK(callback) \
@ -99,7 +99,7 @@ void settings_load(void)
#undef X
#define X(pre, name, post, def, gen_nvs, nvs_format, save_prefix) \
if (gen_nvs) { \
NVSCHECK(nvs_get_##nvs_format(storage, STR(name), save_prefix g_Settings. name)); \
NVSCHECK(nvs_get_##nvs_format(storage, STR(name), save_prefix gSettings. name)); \
}
SETTINGS_XTABLE()
@ -110,13 +110,13 @@ void settings_load(void)
{
size_t capacity = CONSOLE_PW_LEN;
NVSCHECK(nvs_get_str(storage, "tcpp_pw", g_Settings.console_pw, &capacity));
ensure_str_terminated(g_Settings.console_pw, CONSOLE_PW_LEN);
NVSCHECK(nvs_get_str(storage, "tcpp_pw", gSettings.console_pw, &capacity));
ensure_str_terminated(gSettings.console_pw, CONSOLE_PW_LEN);
}
{
size_t capacity = NTP_SRV_LEN;
NVSCHECK(nvs_get_str(storage, "ntp_srv", g_Settings.ntp_srv, &capacity));
ensure_str_terminated(g_Settings.ntp_srv, NTP_SRV_LEN);
NVSCHECK(nvs_get_str(storage, "ntp_srv", gSettings.ntp_srv, &capacity));
ensure_str_terminated(gSettings.ntp_srv, NTP_SRV_LEN);
}
}
@ -139,18 +139,18 @@ void settings_persist(enum settings_key_enum what)
#undef X
#define X(pre,name,post,def,gen_nvs,nvs_format,save_prefix) \
if ((gen_nvs) && ((what)==SETTINGS_ALL || (what)==SETTINGS_## name)) { \
NVSCHECK(nvs_set_##nvs_format(storage, STR(name), g_Settings. name)); \
NVSCHECK(nvs_set_##nvs_format(storage, STR(name), gSettings. name)); \
}
SETTINGS_XTABLE()
// custom values
if (what==SETTINGS_ALL || what==SETTINGS_console_pw) {
NVSCHECK(nvs_set_str(storage, "tcpp_pw", g_Settings.console_pw));
NVSCHECK(nvs_set_str(storage, "tcpp_pw", gSettings.console_pw));
}
if (what==SETTINGS_ALL || what==SETTINGS_ntp_srv) {
NVSCHECK(nvs_set_str(storage, "ntp_srv", g_Settings.ntp_srv));
NVSCHECK(nvs_set_str(storage, "ntp_srv", gSettings.ntp_srv));
}
}

@ -41,7 +41,7 @@ extern nvs_handle g_nvs_storage;
X(uint32_t , static_dns , , 0x08080808 , true , u32 , &) /* 8.8.8.8 */ \
X(uint16_t , recup_mode , , 0 , true , u16 , &) /* recuperator control mode - 0=time, 1=min time+temp equilibrium */ \
X(uint16_t , recup_time , , 60 , true , u16 , &) /* seconds */ \
X(uint16_t , start_stop_time, , 3 , true , u16 , &) /* cas rozjezdu nebo zastaveni motoru (s) */ \
X(uint16_t , ramp_time , , 3 , true , u16 , &) /* cas rozjezdu nebo zastaveni motoru (s) */ \
X(uint16_t , blind_time , , 30 , true , u16 , &) /* cas otevreni nebo zavreni rolety (s) */ \
X(uint16_t , initial_mode , , 0 , true , u16 , &) /* rezim po zapnuti napajeni */ \
X(uint16_t , initial_power , , 100 , true , u16 , &) /* vychozi vykon */ \
@ -73,17 +73,11 @@ void settings_load(void);
void settings_persist(enum settings_key_enum what);
/**
* Load default rtable into the settings struct
* (called when the saved config is rejected by CSP)
* Change blind time. This must be used instead of just writing the settings struct, because of side effects
*/
void settings_load_default_rtable(void);
extern void settings_blind_time_set(uint16_t blind_time);
/**
* Save the current CSP rtable into the settings object (can then be persisted)
*/
void cspemu_settings_store_rtable(void);
extern struct cspemu_settings g_Settings;
extern struct cspemu_settings gSettings;
extern struct cspemu_globals g_State;

@ -15,11 +15,11 @@ static volatile bool sntp_running = false;
static void vTaskSNTPclient(void * pvParameters)
{
(void)pvParameters; //UN-USED
ESP_LOGI(TAG, "Starting SNTP client, server %s", g_Settings.ntp_srv);
ESP_LOGI(TAG, "Starting SNTP client, server %s", gSettings.ntp_srv);
vTaskDelay(configTICK_RATE_HZ);
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, g_Settings.ntp_srv);
sntp_setservername(0, gSettings.ntp_srv);
sntp_init();
vTaskDelay(2*configTICK_RATE_HZ);

@ -21,6 +21,7 @@
#include "irblast.h"
#include "mbiface.h"
#include "actuators.h"
#include "fancontrol.h"
static const char *TAG = "main";
@ -37,7 +38,7 @@ void app_main(void) {
ESP_LOGD(TAG, "initing netif");
ESP_ERROR_CHECK(esp_netif_init());
if (g_Settings.wifi_enabled && (g_Settings.sta_enabled || g_Settings.ap_enabled)) {
if (gSettings.wifi_enabled && (gSettings.sta_enabled || gSettings.ap_enabled)) {
initialise_wifi();
g_State.wifi_inited = true;
} else {
@ -59,6 +60,7 @@ void app_main(void) {
act_init();
mbiface_setup();
fancontrol_init();
telnetsrv_start(CONSOLE_TELNET_PORT);
ESP_LOGI(TAG, "Startup finished, free heap = %u, cmds %"PRIu32, esp_get_free_heap_size(), console_count_commands());

@ -53,7 +53,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
s_retry_num = 0;
xEventGroupSetBits(g_wifi_event_group, WIFI_CONNECTED_BIT);
if (g_Settings.ntp_enable) {
if (gSettings.ntp_enable) {
sntp_cli_start();
}
@ -95,16 +95,16 @@ void initialise_wifi(void)
#define ERROR_CHECK(x) do { if (ESP_OK != (rv = (x))) goto fail; } while (0)
assert(g_Settings.sta_enabled || g_Settings.ap_enabled);
assert(gSettings.sta_enabled || gSettings.ap_enabled);
g_wifi_event_group = xEventGroupCreate();
ESP_LOGD(TAG, "create wifi netif");
esp_netif_t *netif_sta = NULL;
if (g_Settings.sta_enabled) {
if (gSettings.sta_enabled) {
netif_sta = esp_netif_create_default_wifi_sta();
}
if (g_Settings.ap_enabled) {
if (gSettings.ap_enabled) {
esp_netif_create_default_wifi_ap();
}
@ -116,9 +116,9 @@ void initialise_wifi(void)
ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
wifi_mode_t mode;
if (g_Settings.sta_enabled && g_Settings.ap_enabled) {
if (gSettings.sta_enabled && gSettings.ap_enabled) {
mode = WIFI_MODE_APSTA;
} else if (g_Settings.sta_enabled) {
} else if (gSettings.sta_enabled) {
mode = WIFI_MODE_STA;
} else {
// This function is never called if both STA and AP are disabled!
@ -128,25 +128,25 @@ void initialise_wifi(void)
ESP_LOGD(TAG, "set wifi mode");
ERROR_CHECK(esp_wifi_set_mode(mode));
if (g_Settings.ap_enabled) {
if (gSettings.ap_enabled) {
ESP_LOGD(TAG, "set AP IP config");
tcpip_adapter_ip_info_t ip_info;
ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
memset(&ip_info, 0, sizeof(ip_info));
ip_info.ip.addr = g_Settings.ap_ip;
ip_info.gw.addr = g_Settings.ap_ip;
ip_info.ip.addr = gSettings.ap_ip;
ip_info.gw.addr = gSettings.ap_ip;
ip_info.netmask.addr = 0x00ffffff;
ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info));
ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
}
const bool use_dhcp = (g_Settings.dhcp_enable || g_Settings.static_ip == 0)
&& g_Settings.sta_enabled;
const bool use_dhcp = (gSettings.dhcp_enable || gSettings.static_ip == 0)
&& gSettings.sta_enabled;
if (g_Settings.sta_enabled) {
if (gSettings.sta_enabled) {
if (!use_dhcp) {
ESP_LOGD(TAG, "set STA static IP");
@ -154,15 +154,15 @@ void initialise_wifi(void)
ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));
memset(&ip_info, 0, sizeof(ip_info));
ip_info.ip.addr = g_Settings.static_ip;
ip_info.gw.addr = g_Settings.static_ip_gw;
ip_info.netmask.addr = g_Settings.static_ip_mask;
ip_info.ip.addr = gSettings.static_ip;
ip_info.gw.addr = gSettings.static_ip_gw;
ip_info.netmask.addr = gSettings.static_ip_mask;
ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
tcpip_adapter_dns_info_t dnsinfo = {
// special esp version of ip address because why not I guess.
// ipv4 has union tag 0, so it needn't be set
.ip.u_addr.ip4.addr = g_Settings.static_dns,
.ip.u_addr.ip4.addr = gSettings.static_dns,
};
ERROR_CHECK(tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_STA, ESP_NETIF_DNS_MAIN, &dnsinfo));
@ -178,7 +178,7 @@ void initialise_wifi(void)
ESP_LOGI(TAG, "WiFi config done");
if (g_Settings.dhcp_wd_enable && use_dhcp) {
if (gSettings.dhcp_wd_enable && use_dhcp) {
ESP_LOGD(TAG, "Start gateway ping watchdog");
ERROR_CHECK(dhcp_watchdog_start(netif_sta, true, &dhcp_wd));
}

@ -133,7 +133,7 @@ CONFIG_PARTITION_TABLE_MD5=y
# end of Partition Table
#
# IRBLASTER Configuration
# Project configuration
#
#
@ -154,7 +154,7 @@ CONFIG_PIN_1WIRE_2=22
CONFIG_CONSOLE_TELNET_PORT=23
CONFIG_CONSOLE_PW_LEN=32
# end of Console
# end of IRBLASTER Configuration
# end of Project configuration
#
# Compiler options

Loading…
Cancel
Save