diff --git a/components/services/accessors.c b/components/services/accessors.c index e2e4607b..cd12279e 100644 --- a/components/services/accessors.c +++ b/components/services/accessors.c @@ -58,3 +58,22 @@ const i2c_config_t * config_i2c_get(int * i2c_port) { if(i2c_port) *i2c_port=i2c_system_port; return &i2c; } + +/**************************************************************************************** + * + */ +void parse_set_GPIO(void (*cb)(int gpio, char *value)) { + char *nvs_item, *p, type[4]; + int gpio; + + if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "set_GPIO")) == NULL) return; + + p = nvs_item; + + do { + if (sscanf(p, "%d=%3[^,]", &gpio, type) > 0) cb(gpio, type); + p = strchr(p, ','); + } while (p++); + + free(nvs_item); +} \ No newline at end of file diff --git a/components/services/accessors.h b/components/services/accessors.h index 615a8c52..d5daada7 100644 --- a/components/services/accessors.h +++ b/components/services/accessors.h @@ -13,3 +13,4 @@ esp_err_t config_i2c_set(const i2c_config_t * config, int port); const i2c_config_t * config_i2c_get(int * i2c_port); +void parse_set_GPIO(void (*cb)(int gpio, char *value)); diff --git a/components/services/driver_SSD1306.c b/components/services/driver_SSD1306.c index 835e6f0e..963fc66b 100644 --- a/components/services/driver_SSD1306.c +++ b/components/services/driver_SSD1306.c @@ -164,7 +164,7 @@ static bool set_font(int num, enum display_font_e font, int space) { lines[0].y = lines[0].space; for (int i = 1; i <= num; i++) lines[i].y = lines[i-1].y + lines[i-1].font->Height + lines[i].space; - ESP_LOGI(TAG, "adding line %u at %d (height:%u)", num + 1, lines[num].y, lines[num].font->Height); + ESP_LOGI(TAG, "Adding line %u at %d (height:%u)", num + 1, lines[num].y, lines[num].font->Height); if (lines[num].y + lines[num].font->Height > Display.Height) { ESP_LOGW(TAG, "line does not fit display"); diff --git a/components/services/monitor.c b/components/services/monitor.c index 79906db4..e6c31536 100644 --- a/components/services/monitor.c +++ b/components/services/monitor.c @@ -84,18 +84,31 @@ bool spkfault_svc (void) { #endif } +/**************************************************************************************** + * + */ +void set_jack_gpio(int gpio, char *value) { + if (!strcasecmp(value, "jack")) { + ESP_LOGI(TAG,"Adding jack detection gpio %d", gpio); + + gpio_pad_select_gpio(JACK_GPIO); + gpio_set_direction(JACK_GPIO, GPIO_MODE_INPUT); + + // re-use button management for jack handler, it's a GPIO after all + button_create(NULL, JACK_GPIO, BUTTON_LOW, false, 250, jack_handler_default, 0, -1); + } + } + /**************************************************************************************** * */ void monitor_svc_init(void) { ESP_LOGI(TAG, "Initializing monitoring"); -#if defined(JACK_GPIO) && JACK_GPIO != -1 - gpio_pad_select_gpio(JACK_GPIO); - gpio_set_direction(JACK_GPIO, GPIO_MODE_INPUT); - - // re-use button management for jack handler, it's a GPIO after all - button_create(NULL, JACK_GPIO, BUTTON_LOW, false, 250, jack_handler_default, 0, -1); +#if !defined(JACK_GPIO) || JACK_GPIO == -1 + parse_set_GPIO(set_jack_gpio); +#else + set_jack_gpio(JACK_GPIO, "jack"); #endif #ifdef SPKFAULT_GPIO diff --git a/components/services/services.c b/components/services/services.c index cd13d950..f038b870 100644 --- a/components/services/services.c +++ b/components/services/services.c @@ -25,6 +25,25 @@ int i2c_system_port = I2C_SYSTEM_PORT; static const char *TAG = "services"; +/**************************************************************************************** + * + */ +void set_power_gpio(int gpio, char *value) { + bool parsed = true; + + if (!strcasecmp(value, "vcc") ) { + gpio_pad_select_gpio(gpio); + gpio_set_direction(gpio, GPIO_MODE_OUTPUT); + gpio_set_level(gpio, 1); + } else if (!strcasecmp(value, "gnd")) { + gpio_pad_select_gpio(gpio); + gpio_set_direction(gpio, GPIO_MODE_OUTPUT); + gpio_set_level(gpio, 0); + } else parsed = false ; + + if (parsed) ESP_LOGI(TAG, "set GPIO %u to %s", gpio, value); + } + /**************************************************************************************** * */ @@ -40,22 +59,8 @@ void services_init(void) { } #endif - // set fixed gpio if any - if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "set_GPIO")) != NULL) { - char *p = nvs_item, type[4]; - int gpio; - do { - if (sscanf(p, "%d=%3[^,]", &gpio, type) > 0) { - gpio_pad_select_gpio(gpio); - gpio_set_direction(gpio, GPIO_MODE_OUTPUT); - if (!strcasecmp(type, "vcc")) gpio_set_level(gpio, 1); - else if (!strcasecmp(type, "gnd")) gpio_set_level(gpio, 0); - ESP_LOGI(TAG, "set GPIO %u to %s", gpio, type); - } - p = strchr(p, ','); - } while (p++); - free(nvs_item); - } + // set potential power GPIO + parse_set_GPIO(set_power_gpio); const i2c_config_t * i2c_config = config_i2c_get(&i2c_system_port); diff --git a/components/squeezelite/output_i2s.c b/components/squeezelite/output_i2s.c index 884339a1..6d95feb6 100644 --- a/components/squeezelite/output_i2s.c +++ b/components/squeezelite/output_i2s.c @@ -52,6 +52,7 @@ sure that using rate_delay would fix that #include "led.h" #include "monitor.h" #include "config.h" +#include "accessors.h" #define LOCK mutex_lock(outputbuf->mutex) #define UNLOCK mutex_unlock(outputbuf->mutex) @@ -151,6 +152,21 @@ static void jack_handler(bool inserted) { if (jack_handler_chain) (jack_handler_chain)(inserted); } +/**************************************************************************************** + * amp GPIO + */ +void set_amp_gpio(int gpio, char *value) { + if (!strcasecmp(value, "amp")) { + amp_gpio = gpio; + + gpio_pad_select_gpio(amp_gpio); + gpio_set_direction(amp_gpio, GPIO_MODE_OUTPUT); + gpio_set_level(amp_gpio, 0); + + LOG_INFO("setting amplifier GPIO %d", amp_gpio); + } +} + /**************************************************************************************** * Initialize the DAC output */ @@ -258,16 +274,8 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch if (jack_mutes_amp && jack_inserted_svc()) adac->speaker(false); else adac->speaker(true); - p = config_alloc_get_default(NVS_TYPE_STR, "amp_GPIO", NULL, 0); - if (p && *p) { - amp_gpio = atoi(p); - gpio_pad_select_gpio(amp_gpio); - gpio_set_direction(amp_gpio, GPIO_MODE_OUTPUT); - gpio_set_level(amp_gpio, 0); - LOG_INFO("setting amplifier GPIO %d", amp_gpio); - } - free(p); - + parse_set_GPIO(set_amp_gpio); + esp_pthread_cfg_t cfg = esp_pthread_get_default_config(); cfg.thread_name= "output_i2s"; diff --git a/main/esp_app_main.c b/main/esp_app_main.c index e53c3a90..616f1776 100644 --- a/main/esp_app_main.c +++ b/main/esp_app_main.c @@ -325,9 +325,6 @@ void register_default_nvs(){ ESP_LOGD(TAG,"Registering default value for key %s", "stats"); config_set_default(NVS_TYPE_STR, "stats", "n", 0); - ESP_LOGD(TAG,"Registering default value for key %s", "amp_GPIO"); - config_set_default(NVS_TYPE_STR, "amp_GPIO", "", 0); - ESP_LOGD(TAG,"Done setting default values in nvs."); }