From 9ebe717e742b0c48e427c907d05c868153599bc8 Mon Sep 17 00:00:00 2001 From: philippe44 Date: Tue, 10 Oct 2023 13:51:47 -0700 Subject: [PATCH] add delay and gpio on dac_controlset --- .../squeezelite/external/dac_external.c | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/components/squeezelite/external/dac_external.c b/components/squeezelite/external/dac_external.c index ab5c3730a..67c0b3f7f 100644 --- a/components/squeezelite/external/dac_external.c +++ b/components/squeezelite/external/dac_external.c @@ -14,6 +14,7 @@ #include #include "driver/i2c.h" #include "esp_log.h" +#include "gpio_exp.h" #include "cJSON.h" #include "platform_config.h" #include "adac.h" @@ -123,9 +124,28 @@ bool i2c_json_execute(char *set) { if (!json_set) return true; cJSON_ArrayForEach(item, json_set) { - cJSON *reg = cJSON_GetObjectItemCaseSensitive(item, "reg"); + cJSON *action; + + // is this a delay + if ((action = cJSON_GetObjectItemCaseSensitive(item, "delay")) != NULL) { + vTaskDelay(pdMS_TO_TICKS(action->valueint)); + ESP_LOGI(TAG, "DAC waiting %d ms", action->valueint); + continue; + } + + // is this a gpio toggle + if ((action = cJSON_GetObjectItemCaseSensitive(item, "gpio")) != NULL) { + cJSON *level = cJSON_GetObjectItemCaseSensitive(item, "level"); + ESP_LOGI(TAG, "set GPIO %d at %d", action->valueint, level->valueint); + gpio_set_direction_x(action->valueint, GPIO_MODE_OUTPUT); + gpio_set_level_x(action->valueint, level->valueint); + continue; + } + + action= cJSON_GetObjectItemCaseSensitive(item, "reg"); cJSON *val = cJSON_GetObjectItemCaseSensitive(item, "val"); + // this is gpio register setting or crap if (cJSON_IsArray(val)) { cJSON *value; uint8_t *data = malloc(cJSON_GetArraySize(val)); @@ -137,23 +157,23 @@ bool i2c_json_execute(char *set) { data[count++] = value->valueint; } - adac_write(i2c_addr, reg->valueint, data, count); + adac_write(i2c_addr, action->valueint, data, count); free(data); } else { cJSON *mode = cJSON_GetObjectItemCaseSensitive(item, "mode"); - if (!reg || !val) continue; + if (!action || !val) continue; if (!mode) { - adac_write_byte(i2c_addr, reg->valueint, val->valueint); + adac_write_byte(i2c_addr, action->valueint, val->valueint); } else if (!strcasecmp(mode->valuestring, "or")) { - uint8_t data = adac_read_byte(i2c_addr,reg->valueint); + uint8_t data = adac_read_byte(i2c_addr, action->valueint); data |= (uint8_t) val->valueint; - adac_write_byte(i2c_addr, reg->valueint, data); + adac_write_byte(i2c_addr, action->valueint, data); } else if (!strcasecmp(mode->valuestring, "and")) { - uint8_t data = adac_read_byte(i2c_addr, reg->valueint); + uint8_t data = adac_read_byte(i2c_addr, action->valueint); data &= (uint8_t) val->valueint; - adac_write_byte(i2c_addr, reg->valueint, data); + adac_write_byte(i2c_addr, action->valueint, data); } } }