--- a/main/task_apds9930.c Tue Apr 04 20:57:49 2023 +0200 +++ b/main/task_apds9930.c Wed Apr 05 20:00:26 2023 +0200 @@ -61,6 +61,10 @@ apds9930_state->address = apds9930_dev.i2c_dev.addr; apds9930_state->error = APDS9930_ERR_NONE; + // Calculate these for auto gain scaling. + ALS_maxcount = (256 - APDS9930_DEFAULT_ATIME) * 1024; + ALS_mincount = ALS_maxcount / 64; + /* event handler and event group for this task */ xEventGroupAPDS9930 = xEventGroupCreate(); EventBits_t uxBits; @@ -77,23 +81,25 @@ if (! apds9930_state->fake) { /* Real sensor is present */ - apds9930_enablePower(&apds9930_dev); - apds9930_enableLightSensor(&apds9930_dev, false); - apds9930_disableProximitySensor(&apds9930_dev); if (xSemaphoreTake(xSemaphoreAPDS9930, 25) == pdTRUE) { - l_gain = apds9930_state->gain; - l_aglbit = apds9930_state->aglbit; - xSemaphoreGive(xSemaphoreAPDS9930); - } - if (l_gain > 3) - l_gain = 0; - apds9930_setAmbientLightGain(&apds9930_dev, l_gain); - apds9930_setAmbientGainLevel(&apds9930_dev, l_aglbit); + l_gain = apds9930_state->gain; + l_aglbit = apds9930_state->aglbit; + xSemaphoreGive(xSemaphoreAPDS9930); + } + if (l_gain > 3) + l_gain = 0; + ESP_LOGI(TAG, "1"); + // ESP_ERROR_CHECK(apds9930_enablePower(&apds9930_dev)); + ESP_ERROR_CHECK(apds9930_enableLightSensor(&apds9930_dev, false)); // Does apds9930_enablePower() too. + ESP_ERROR_CHECK(apds9930_disableProximitySensor(&apds9930_dev)); + ESP_ERROR_CHECK(apds9930_setAmbientLightGain(&apds9930_dev, l_gain)); + ESP_ERROR_CHECK(apds9930_setAmbientGainLevel(&apds9930_dev, l_aglbit)); vTaskDelay(200 / portTICK_PERIOD_MS); tries = 6; err = ESP_OK; while (tries) { + ESP_LOGI(TAG, "2 tries %d", tries); err = apds9930_readAmbientLightLux(&apds9930_dev, &ambient_light); if (err == ESP_OK) { err = apds9930_readCh0Light(&apds9930_dev, &ch0); @@ -105,6 +111,7 @@ ESP_LOGE(TAG, "read APDS-9930 values error '%s'", esp_err_to_name(err)); break; } + ESP_LOGI(TAG, "2a ALS_maxcount=%d ALS_mincount=%d", ALS_maxcount, ALS_mincount); /* * Check ranges