--- a/main/task_apds9930.c Thu Apr 20 15:19:31 2023 +0200 +++ b/main/task_apds9930.c Sun Sep 10 17:29:15 2023 +0200 @@ -14,8 +14,6 @@ APDS9930_State *apds9930_state; ///< Public state for other tasks float ambient_light; -uint16_t ch0 = 0; -uint16_t ch1 = 1; uint16_t ALS_maxcount; ///< Maximum ADC value uint16_t ALS_mincount; ///< Threshold to increase gain @@ -60,6 +58,8 @@ apds9930_state->fake = (apds9930_dev.i2c_dev.addr == 0) ? true:false; apds9930_state->address = apds9930_dev.i2c_dev.addr; apds9930_state->error = APDS9930_ERR_NONE; + uint16_t ch0 = 0; + uint16_t ch1 = 1; /* Gain settings are saved in NVS for quick startup. */ l_gain = nvsio_read_u8((char *)"l_gain"); @@ -97,16 +97,9 @@ err = ESP_OK; while (tries) { - if (err == ESP_OK) { - err = apds9930_readCh0Light(&apds9930_dev, &ch0); - if (err == ESP_OK) { - err = apds9930_readCh1Light(&apds9930_dev, &ch1); - } - } - if (err != ESP_OK) { - ESP_LOGE(TAG, "read APDS-9930 values error '%s'", esp_err_to_name(err)); - break; - } + /* Read channels never returns any error */ + apds9930_readCh0Light(&apds9930_dev, &ch0); + apds9930_readCh1Light(&apds9930_dev, &ch1); ambient_light = apds9930_floatAmbientToLux(&apds9930_dev, ch0, ch1); /* @@ -135,9 +128,7 @@ vTaskDelay(200 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "Gain increased to %d AGL: %d", l_gain, l_aglbit); } else { - if ((ch0 == ALS_maxcount) || (ch1 == ALS_maxcount)) { - ambient_light = -1; - } else if (l_aglbit) { + if (l_aglbit) { ambient_light = ambient_light / 0.1666; } nvsio_write_u8((char *)"l_gain", l_gain); @@ -160,6 +151,8 @@ apds9930_state->ambient_light = ambient_light; apds9930_state->gain = l_gain; apds9930_state->aglbit = l_aglbit; + apds9930_state->ch0 = ch0; + apds9930_state->ch1 = ch1; } else { apds9930_state->error = APDS9930_ERR_READ; apds9930_state->valid = false; @@ -171,12 +164,17 @@ } } else { /* Use fake values */ + ch0 = 12; + ch1 = 34; + ambient_light = apds9930_floatAmbientToLux(&apds9930_dev, ch0, ch1); if (xSemaphoreTake(xSemaphoreAPDS9930, 25) == pdTRUE) { apds9930_state->error = APDS9930_ERR_NONE; apds9930_state->valid = true; - apds9930_state->ambient_light = 12.34; + apds9930_state->ambient_light = ambient_light; apds9930_state->gain = 2; apds9930_state->aglbit = 0; + apds9930_state->ch0 = ch0; + apds9930_state->ch1 = ch1; xSemaphoreGive(xSemaphoreAPDS9930); } }