safe NVS parsing

This commit is contained in:
Philippe G
2021-12-06 18:29:04 -08:00
parent 5ac153f808
commit a9efcfeca8
9 changed files with 114 additions and 101 deletions

View File

@@ -243,7 +243,7 @@ struct GDS_Device* SSD1675_Detect(char *Driver, struct GDS_Device* Device) {
char *p; char *p;
struct PrivateSpace* Private = (struct PrivateSpace*) Device->Private; struct PrivateSpace* Private = (struct PrivateSpace*) Device->Private;
Private->ReadyPin = -1; Private->ReadyPin = -1;
if ((p = strcasestr(Driver, "ready")) != NULL) Private->ReadyPin = atoi(strchr(p, '=') + 1); if ((p = strcasestr(Driver, "ready")) && (p = strchr(p, '='))) Private->ReadyPin = atoi(p + 1);
ESP_LOGI(TAG, "SSD1675 driver with ready GPIO %d", Private->ReadyPin); ESP_LOGI(TAG, "SSD1675 driver with ready GPIO %d", Private->ReadyPin);

View File

@@ -71,11 +71,11 @@ void display_init(char *welcome) {
char *config = config_alloc_get_str("display_config", CONFIG_DISPLAY_CONFIG, "N/A"); char *config = config_alloc_get_str("display_config", CONFIG_DISPLAY_CONFIG, "N/A");
int width = -1, height = -1, backlight_pin = -1; int width = -1, height = -1, backlight_pin = -1;
char *p, *drivername = strstr(config, "driver"); char *drivername = strstr(config, "driver");
if ((p = strcasestr(config, "width")) != NULL) width = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "width", '=', width);
if ((p = strcasestr(config, "height")) != NULL) height = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "height", '=', height);
if ((p = strcasestr(config, "back")) != NULL) backlight_pin = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "back", '=', backlight_pin);
// query drivers to see if we have a match // query drivers to see if we have a match
ESP_LOGI(TAG, "Trying to configure display with %s", config); ESP_LOGI(TAG, "Trying to configure display with %s", config);
@@ -89,13 +89,13 @@ void display_init(char *welcome) {
// so far so good // so far so good
if (display && width > 0 && height > 0) { if (display && width > 0 && height > 0) {
int RST_pin = -1; int RST_pin = -1;
if ((p = strcasestr(config, "reset")) != NULL) RST_pin = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "reset", '=', RST_pin);
// Detect driver interface // Detect driver interface
if (strcasestr(config, "I2C") && i2c_system_port != -1) { if (strcasestr(config, "I2C") && i2c_system_port != -1) {
int address = 0x3C; int address = 0x3C;
if ((p = strcasestr(config, "address")) != NULL) address = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "address", '=', address);
init = true; init = true;
GDS_I2CInit( i2c_system_port, -1, -1, i2c_system_speed ) ; GDS_I2CInit( i2c_system_port, -1, -1, i2c_system_speed ) ;
@@ -105,8 +105,8 @@ void display_init(char *welcome) {
} else if (strcasestr(config, "SPI") && spi_system_host != -1) { } else if (strcasestr(config, "SPI") && spi_system_host != -1) {
int CS_pin = -1, speed = 0; int CS_pin = -1, speed = 0;
if ((p = strcasestr(config, "cs")) != NULL) CS_pin = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "cs", '=', CS_pin);
if ((p = strcasestr(config, "speed")) != NULL) speed = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "speed", '=', speed);
init = true; init = true;
GDS_SPIInit( spi_system_host, spi_system_dc_gpio ); GDS_SPIInit( spi_system_host, spi_system_dc_gpio );
@@ -279,8 +279,8 @@ void displayer_metadata(char *artist, char *album, char *title) {
} }
// get optional scroll speed & pause // get optional scroll speed & pause
if ((p = strcasestr(displayer.metadata_config, "speed")) != NULL) sscanf(p, "%*[^=]=%d", &displayer.speed); PARSE_PARAM(displayer.metadata_config, "speed", '=', displayer.speed);
if ((p = strcasestr(displayer.metadata_config, "pause")) != NULL) sscanf(p, "%*[^=]=%d", &displayer.pause); PARSE_PARAM(displayer.metadata_config, "pause", '=', displayer.pause);
displayer.offset = 0; displayer.offset = 0;
utf8_decode(displayer.string); utf8_decode(displayer.string);

View File

@@ -9,6 +9,19 @@
extern "C" { extern "C" {
#endif #endif
#define PARSE_PARAM(S,P,C,V) do { \
char *__p; \
if ((__p = strcasestr(S, P)) && (__p = strchr(__p, C))) V = atoi(__p+1); \
} while (0)
#define PARSE_PARAM_STR(S,P,C,V,I) do { \
char *__p; \
if ((__p = strstr(S, P)) && (__p = strchr(__p, C))) { \
while (*++__p == ' '); \
sscanf(__p,"%" #I "[^,]", V); \
} \
} while (0)
#define DECLARE_SET_DEFAULT(t) void config_set_default_## t (const char *key, t value); #define DECLARE_SET_DEFAULT(t) void config_set_default_## t (const char *key, t value);
#define DECLARE_GET_NUM(t) esp_err_t config_get_## t (const char *key, t * value); #define DECLARE_GET_NUM(t) esp_err_t config_get_## t (const char *key, t * value);
#ifndef FREE_RESET #ifndef FREE_RESET

View File

@@ -15,7 +15,6 @@
#include "accessors.h" #include "accessors.h"
#include "globdefs.h" #include "globdefs.h"
#include "display.h" #include "display.h"
#include "display.h"
#include "cJSON.h" #include "cJSON.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "stdbool.h" #include "stdbool.h"
@@ -109,9 +108,9 @@ bool is_spdif_config_locked(){
static void set_i2s_pin(char *config, i2s_pin_config_t *pin_config) { static void set_i2s_pin(char *config, i2s_pin_config_t *pin_config) {
char *p; char *p;
pin_config->bck_io_num = pin_config->ws_io_num = pin_config->data_out_num = pin_config->data_in_num = -1; pin_config->bck_io_num = pin_config->ws_io_num = pin_config->data_out_num = pin_config->data_in_num = -1;
if ((p = strcasestr(config, "bck")) != NULL) pin_config->bck_io_num = atoi(strchr(p, '=') + 1); if ((p = strcasestr(config, "bck"))) sscanf(p, "bck%*[^=]=%d", &pin_config->bck_io_num);
if ((p = strcasestr(config, "ws")) != NULL) pin_config->ws_io_num = atoi(strchr(p, '=') + 1); if ((p = strcasestr(config, "ws"))) sscanf(p, "ws%*[^=]=%d", &pin_config->ws_io_num);
if ((p = strcasestr(config, "do")) != NULL) pin_config->data_out_num = atoi(strchr(p, '=') + 1); if ((p = strcasestr(config, "do"))) sscanf(p, "do%*[^=]=%d", &pin_config->data_out_num);
} }
/**************************************************************************************** /****************************************************************************************
@@ -129,15 +128,15 @@ const i2s_platform_config_t * config_get_i2s_from_str(char * dac_config ){
strcpy(i2s_dac_pin.model, "i2s"); strcpy(i2s_dac_pin.model, "i2s");
char * p=NULL; char * p=NULL;
if ((p = strcasestr(dac_config, "i2c")) != NULL) i2s_dac_pin.i2c_addr = atoi(strchr(p, '=') + 1); PARSE_PARAM(dac_config, "i2c", '=', i2s_dac_pin.i2c_addr);
if ((p = strcasestr(dac_config, "sda")) != NULL) i2s_dac_pin.sda = atoi(strchr(p, '=') + 1); PARSE_PARAM(dac_config, "sda", '=', i2s_dac_pin.sda);
if ((p = strcasestr(dac_config, "scl")) != NULL) i2s_dac_pin.scl = atoi(strchr(p, '=') + 1); PARSE_PARAM(dac_config, "scl", '=', i2s_dac_pin.scl);
if ((p = strcasestr(dac_config, "model")) != NULL) sscanf(p, "%*[^=]=%31[^,]", i2s_dac_pin.model); PARSE_PARAM_STR(dac_config, "model", '=', i2s_dac_pin.model, 31);
if ((p = strcasestr(dac_config, "mute")) != NULL) { if ((p = strcasestr(dac_config, "mute"))) {
char mute[8] = ""; char mute[8] = "";
sscanf(p, "%*[^=]=%7[^,]", mute); sscanf(p, "%*[^=]=%7[^,]", mute);
i2s_dac_pin.mute_gpio = atoi(mute); i2s_dac_pin.mute_gpio = atoi(mute);
if ((p = strchr(mute, ':')) != NULL) i2s_dac_pin.mute_level = atoi(p + 1); PARSE_PARAM(p, "mute", ':', i2s_dac_pin.mute_level);
} }
return &i2s_dac_pin; return &i2s_dac_pin;
} }
@@ -145,28 +144,29 @@ const i2s_platform_config_t * config_get_i2s_from_str(char * dac_config ){
/**************************************************************************************** /****************************************************************************************
* Get eth config structure from config string * Get eth config structure from config string
*/ */
const eth_config_t * config_get_eth_from_str(char * eth_config ){ const eth_config_t * config_get_eth_from_str(char * config ){
static eth_config_t eth_pin = { static eth_config_t eth_config = {
.rmii = false, .rmii = false,
.model = "", .model = "",
}; };
char * p=NULL;
PARSE_PARAM_STR(config, "model", '=', eth_config.model, 15);
PARSE_PARAM(config, "mdc", '=', eth_config.mdc);
PARSE_PARAM(config, "mdio", '=', eth_config.mdio);
PARSE_PARAM(config, "rst", '=', eth_config.rst);
PARSE_PARAM(config, "mosi", '=', eth_config.mosi);
PARSE_PARAM(config, "miso", '=', eth_config.miso);
PARSE_PARAM(config, "intr", '=', eth_config.intr);
PARSE_PARAM(config, "cs", '=', eth_config.cs);
PARSE_PARAM(config, "speed", '=', eth_config.speed);
PARSE_PARAM(config, "clk", '=', eth_config.clk);
if ((p = strcasestr(eth_config, "model")) != NULL) sscanf(p, "%*[^=]=%15[^,]", eth_pin.model); // only system host is available
if ((p = strcasestr(eth_config, "mdc")) != NULL) eth_pin.mdc = atoi(strchr(p, '=') + 1); eth_config.host = spi_system_host;
if ((p = strcasestr(eth_config, "mdio")) != NULL) eth_pin.mdio = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(eth_config, "rst")) != NULL) eth_pin.rst = atoi(strchr(p, '=') + 1); if (strcasestr(eth_config.model, "lan8720")) eth_config.rmii = true;
if ((p = strcasestr(eth_config, "mosi")) != NULL) eth_pin.mosi = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(eth_config, "miso")) != NULL) eth_pin.miso = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(eth_config, "intr")) != NULL) eth_pin.intr = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(eth_config, "cs")) != NULL) eth_pin.cs = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(eth_config, "speed")) != NULL) eth_pin.speed = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(eth_config, "clk")) != NULL) eth_pin.clk = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(eth_config, "host")) != NULL) eth_pin.host = atoi(strchr(p, '=') + 1);
if (strcasestr(eth_pin.model, "lan8720")) eth_pin.rmii = true; return &eth_config;
return &eth_pin;
} }
/**************************************************************************************** /****************************************************************************************
@@ -432,16 +432,18 @@ const display_config_t * config_display_get(){
sscanf(p, "%*[^:]:%u", &dstruct.depth); sscanf(p, "%*[^:]:%u", &dstruct.depth);
dstruct.drivername = display_conf_get_driver_name(strchr(p, '=') + 1); dstruct.drivername = display_conf_get_driver_name(strchr(p, '=') + 1);
} }
if ((p = strcasestr(config, "width")) != NULL) dstruct.width = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "width", '=', dstruct.width);
if ((p = strcasestr(config, "height")) != NULL) dstruct.height = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "height", '=', dstruct.height);
if ((p = strcasestr(config, "reset")) != NULL) dstruct.RST_pin = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "reset", '=', dstruct.RST_pin);
PARSE_PARAM(config, "address", '=', dstruct.address);
PARSE_PARAM(config, "cs", '=', dstruct.CS_pin);
PARSE_PARAM(config, "speed", '=', dstruct.speed);
PARSE_PARAM(config, "back", '=', dstruct.back);
if (strstr(config, "I2C") ) dstruct.type=i2c_name_type; if (strstr(config, "I2C") ) dstruct.type=i2c_name_type;
if ((p = strcasestr(config, "address")) != NULL) dstruct.address = atoi(strchr(p, '=') + 1);
if (strstr(config, "SPI") ) dstruct.type=spi_name_type; if (strstr(config, "SPI") ) dstruct.type=spi_name_type;
if ((p = strcasestr(config, "cs")) != NULL) dstruct.CS_pin = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(config, "speed")) != NULL) dstruct.speed = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(config, "back")) != NULL) dstruct.back = atoi(strchr(p, '=') + 1);
dstruct.hflip= strcasestr(config, "HFlip") ? true : false; dstruct.hflip= strcasestr(config, "HFlip") ? true : false;
dstruct.vflip= strcasestr(config, "VFlip") ? true : false; dstruct.vflip= strcasestr(config, "VFlip") ? true : false;
dstruct.rotate= strcasestr(config, "rotate") ? true : false; dstruct.rotate= strcasestr(config, "rotate") ? true : false;
@@ -452,7 +454,7 @@ const display_config_t * config_display_get(){
* *
*/ */
const i2c_config_t * config_i2c_get(int * i2c_port) { const i2c_config_t * config_i2c_get(int * i2c_port) {
char *nvs_item, *p; char *nvs_item;
static i2c_config_t i2c = { static i2c_config_t i2c = {
.mode = I2C_MODE_MASTER, .mode = I2C_MODE_MASTER,
.sda_io_num = -1, .sda_io_num = -1,
@@ -466,10 +468,10 @@ const i2c_config_t * config_i2c_get(int * i2c_port) {
nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config"); nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
if (nvs_item) { if (nvs_item) {
if ((p = strcasestr(nvs_item, "scl")) != NULL) i2c.scl_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "scl", '=', i2c.scl_io_num);
if ((p = strcasestr(nvs_item, "sda")) != NULL) i2c.sda_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "sda", '=', i2c.sda_io_num);
if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c.master.clk_speed = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "speed", '=', i2c.master.clk_speed);
if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_system_port = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "port", '=', i2c_system_port);
free(nvs_item); free(nvs_item);
} }
if(i2c_port) { if(i2c_port) {
@@ -504,13 +506,14 @@ const gpio_exp_config_t* config_gpio_exp_get(int index) {
} }
} }
if ((p = strcasestr(item, "addr")) != NULL) config.phy.addr = atoi(strchr(p, '=') + 1); PARSE_PARAM(item, "addr", '=', config.phy.addr);
if ((p = strcasestr(item, "cs_pin")) != NULL) config.phy.cs_pin = atoi(strchr(p, '=') + 1); PARSE_PARAM(item, "cs_pin", '=', config.phy.cs_pin);
if ((p = strcasestr(item, "speed")) != NULL) config.phy.speed = atoi(strchr(p, '=') + 1); PARSE_PARAM(item, "speed", '=', config.phy.speed);
if ((p = strcasestr(item, "intr")) != NULL) config.intr = atoi(strchr(p, '=') + 1); PARSE_PARAM(item, "intr", '=', config.intr);
if ((p = strcasestr(item, "base")) != NULL) config.base = atoi(strchr(p, '=') + 1); PARSE_PARAM(item, "base", '=', config.base);
if ((p = strcasestr(item, "count")) != NULL) config.count = atoi(strchr(p, '=') + 1); PARSE_PARAM(item, "count", '=', config.count);
if ((p = strcasestr(item, "model")) != NULL) sscanf(p, "%*[^=]=%31[^,]", config.model); PARSE_PARAM_STR(item, "model", '=', config.model, 31);
if ((p = strcasestr(item, "port")) != NULL) { if ((p = strcasestr(item, "port")) != NULL) {
char port[8] = ""; char port[8] = "";
sscanf(p, "%*[^=]=%7[^,]", port); sscanf(p, "%*[^=]=%7[^,]", port);
@@ -599,7 +602,7 @@ const set_GPIO_struct_t * get_gpio_struct(){
* *
*/ */
const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) { const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
char *nvs_item, *p; char *nvs_item;
static spi_bus_config_t spi = { static spi_bus_config_t spi = {
.mosi_io_num = -1, .mosi_io_num = -1,
.sclk_io_num = -1, .sclk_io_num = -1,
@@ -610,11 +613,11 @@ const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
nvs_item = config_alloc_get_str("spi_config", CONFIG_SPI_CONFIG, NULL); nvs_item = config_alloc_get_str("spi_config", CONFIG_SPI_CONFIG, NULL);
if (nvs_item) { if (nvs_item) {
if ((p = strcasestr(nvs_item, "data")) != NULL) spi.mosi_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "data", '=', spi.mosi_io_num);
if ((p = strcasestr(nvs_item, "mosi")) != NULL) spi.mosi_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "mosi", '=', spi.mosi_io_num);
if ((p = strcasestr(nvs_item, "miso")) != NULL) spi.miso_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "miso", '=', spi.miso_io_num);
if ((p = strcasestr(nvs_item, "clk")) != NULL) spi.sclk_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "clk", '=', spi.sclk_io_num);
if ((p = strcasestr(nvs_item, "dc")) != NULL) spi_system_dc_gpio = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "dd", '=', spi_system_dc_gpio);
// only VSPI (1) can be used as Flash and PSRAM run at 80MHz // only VSPI (1) can be used as Flash and PSRAM run at 80MHz
// if ((p = strcasestr(nvs_item, "host")) != NULL) spi_system_host = atoi(strchr(p, '=') + 1); // if ((p = strcasestr(nvs_item, "host")) != NULL) spi_system_host = atoi(strchr(p, '=') + 1);
free(nvs_item); free(nvs_item);
@@ -651,11 +654,11 @@ const rotary_struct_t * config_rotary_get() {
char *config = config_alloc_get_default(NVS_TYPE_STR, "rotary_config", NULL, 0); char *config = config_alloc_get_default(NVS_TYPE_STR, "rotary_config", NULL, 0);
if (config && *config) { if (config && *config) {
char *p; char *p;
// parse config // parse config
if ((p = strcasestr(config, "A")) != NULL) rotary.A = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "A", '=', rotary.A);
if ((p = strcasestr(config, "B")) != NULL) rotary.B = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "B", '=', rotary.B);
if ((p = strcasestr(config, "SW")) != NULL) rotary.SW = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "SW", '=', rotary.SW);
if ((p = strcasestr(config, "knobonly")) != NULL) { if ((p = strcasestr(config, "knobonly")) != NULL) {
p = strchr(p, '='); p = strchr(p, '=');
rotary.knobonly = true; rotary.knobonly = true;
@@ -700,13 +703,10 @@ cJSON * add_gpio_for_value(cJSON * list,const char * name,int gpio, const char *
*/ */
cJSON * add_gpio_for_name(cJSON * list,const char * nvs_entry,const char * name, const char * prefix, bool fixed){ cJSON * add_gpio_for_name(cJSON * list,const char * nvs_entry,const char * name, const char * prefix, bool fixed){
cJSON * llist = list?list:cJSON_CreateArray(); cJSON * llist = list?list:cJSON_CreateArray();
char *p;
int gpioNum=0; int gpioNum=0;
if ((p = strcasestr(nvs_entry, name)) != NULL) { PARSE_PARAM(nvs_entry, name, '=', gpioNum);
gpioNum = atoi(strchr(p, '=') + 1); if(gpioNum>=0){
if(gpioNum>=0){ cJSON_AddItemToArray(llist,get_gpio_entry(name,prefix,gpioNum,fixed));
cJSON_AddItemToArray(llist,get_gpio_entry(name,prefix,gpioNum,fixed));
}
} }
return llist; return llist;
} }
@@ -1028,14 +1028,11 @@ cJSON * get_gpio_list(bool refresh) {
#ifndef CONFIG_BAT_LOCKED #ifndef CONFIG_BAT_LOCKED
char *bat_config = config_alloc_get_default(NVS_TYPE_STR, "bat_config", NULL, 0); char *bat_config = config_alloc_get_default(NVS_TYPE_STR, "bat_config", NULL, 0);
if (bat_config) { if (bat_config) {
char *p; int channel = -1;
int channel; PARSE_PARAM(bat_config, "channem", '=', channel);
if ((p = strcasestr(bat_config, "channel") ) != NULL) { if(channel != -1){
channel = atoi(strchr(p, '=') + 1); if(adc1_pad_get_io_num(channel,&gpio_num )==ESP_OK){
if(channel != -1){ cJSON_AddItemToArray(gpio_list,get_gpio_entry("bat","other",gpio_num,false));
if(adc1_pad_get_io_num(channel,&gpio_num )==ESP_OK){
cJSON_AddItemToArray(gpio_list,get_gpio_entry("bat","other",gpio_num,false));
}
} }
} }
free(bat_config); free(bat_config);

View File

@@ -137,10 +137,10 @@ esp_err_t actrls_init(const char *profile_name) {
int A = -1, B = -1, SW = -1, longpress = 0; int A = -1, B = -1, SW = -1, longpress = 0;
// parse config // parse config
if ((p = strcasestr(config, "A")) != NULL) A = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "A", '=', A);
if ((p = strcasestr(config, "B")) != NULL) B = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "B", '=', B);
if ((p = strcasestr(config, "SW")) != NULL) SW = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "SW", '=', SW);
if ((p = strcasestr(config, "knobonly")) != NULL) { if ((p = strcasestr(config, "knobonly"))) {
p = strchr(p, '='); p = strchr(p, '=');
int double_press = p ? atoi(p + 1) : 350; int double_press = p ? atoi(p + 1) : 350;
rotary.timer = xTimerCreate("knobTimer", double_press / portTICK_RATE_MS, pdFALSE, NULL, rotary_timer); rotary.timer = xTimerCreate("knobTimer", double_press / portTICK_RATE_MS, pdFALSE, NULL, rotary_timer);

View File

@@ -79,13 +79,12 @@ void battery_svc_init(void) {
char *nvs_item = config_alloc_get_default(NVS_TYPE_STR, "bat_config", "n", 0); char *nvs_item = config_alloc_get_default(NVS_TYPE_STR, "bat_config", "n", 0);
if (nvs_item) { if (nvs_item) {
char *p;
#ifndef CONFIG_BAT_LOCKED #ifndef CONFIG_BAT_LOCKED
if ((p = strcasestr(nvs_item, "channel")) != NULL) battery.channel = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "channel", '=', battery.channel);
if ((p = strcasestr(nvs_item, "scale")) != NULL) battery.scale = atof(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "scale", '=', battery.scale);
if ((p = strcasestr(nvs_item, "atten")) != NULL) battery.attenuation = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "atten", '=', battery.attenuation);
#endif #endif
if ((p = strcasestr(nvs_item, "cells")) != NULL) battery.cells = atof(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "cells", '=', battery.cells);
free(nvs_item); free(nvs_item);
} }

View File

@@ -234,10 +234,10 @@ void led_svc_init(void) {
parse_set_GPIO(set_led_gpio); parse_set_GPIO(set_led_gpio);
#endif #endif
char *nvs_item = config_alloc_get(NVS_TYPE_STR, "led_brightness"), *p; char *nvs_item = config_alloc_get(NVS_TYPE_STR, "led_brightness");
if (nvs_item) { if (nvs_item) {
if ((p = strcasestr(nvs_item, "green")) != NULL) green.pwm = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "green", '=', green.pwm);
if ((p = strcasestr(nvs_item, "red")) != NULL) red.pwm = atoi(strchr(p, '=') + 1); PARSE_PARAM(nvs_item, "red", '=', red.pwm);
free(nvs_item); free(nvs_item);
} }

View File

@@ -17,6 +17,11 @@
#include "esp_log.h" #include "esp_log.h"
#include "adac.h" #include "adac.h"
#define PARSE_PARAM(S,P,C,V) do { \
char *__p; \
if ((__p = strcasestr(S, P)) && (__p = strchr(__p, C))) V = atoi(__p+1); \
} while (0)
static const char TAG[] = "DAC core"; static const char TAG[] = "DAC core";
static int i2c_port = -1; static int i2c_port = -1;
@@ -46,9 +51,9 @@ int adac_init(char *config, int i2c_port_num) {
.master.clk_speed = 250000, .master.clk_speed = 250000,
}; };
if ((p = strcasestr(config, "i2c")) != NULL) i2c_addr = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "i2c", '=', i2c_addr);
if ((p = strcasestr(config, "sda")) != NULL) i2c_config.sda_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "sda", '=', i2c_config.sda_io_num);
if ((p = strcasestr(config, "scl")) != NULL) i2c_config.scl_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "scl", '=', i2c_config.scl_io_num);
if (i2c_config.sda_io_num == -1 || i2c_config.scl_io_num == -1) { if (i2c_config.sda_io_num == -1 || i2c_config.scl_io_num == -1) {
ESP_LOGW(TAG, "DAC does not use i2c"); ESP_LOGW(TAG, "DAC does not use i2c");

View File

@@ -190,11 +190,10 @@ static void set_amp_gpio(int gpio, char *value) {
* Set pin from config string * Set pin from config string
*/ */
static void set_i2s_pin(char *config, i2s_pin_config_t *pin_config) { static void set_i2s_pin(char *config, i2s_pin_config_t *pin_config) {
char *p;
pin_config->bck_io_num = pin_config->ws_io_num = pin_config->data_out_num = pin_config->data_in_num = -1; pin_config->bck_io_num = pin_config->ws_io_num = pin_config->data_out_num = pin_config->data_in_num = -1;
if ((p = strcasestr(config, "bck")) != NULL) pin_config->bck_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "bck", '=', pin_config->bck_io_num);
if ((p = strcasestr(config, "ws")) != NULL) pin_config->ws_io_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "ws", '=', pin_config->ws_io_num);
if ((p = strcasestr(config, "do")) != NULL) pin_config->data_out_num = atoi(strchr(p, '=') + 1); PARSE_PARAM(config, "do", '=', pin_config->data_out_num);
} }
/**************************************************************************************** /****************************************************************************************