Version 0.4.3. Attempt to fix the sunlight overflow of the APDS9930 sensor in the private part of the esp-idf-lib. Removed some error checks from functions that always return OK. Store light sensor registers in the state record and report the values in the json result string. default tip

Sun, 10 Sep 2023 17:29:15 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 10 Sep 2023 17:29:15 +0200
changeset 37
50dbb626fbab
parent 36
7b5c1fbeda7b

Version 0.4.3. Attempt to fix the sunlight overflow of the APDS9930 sensor in the private part of the esp-idf-lib. Removed some error checks from functions that always return OK. Store light sensor registers in the state record and report the values in the json result string.

CMakeLists.txt file | annotate | diff | comparison | revisions
esp-idf-lib/components/apds9930/apds9930.c file | annotate | diff | comparison | revisions
main/task_apds9930.c file | annotate | diff | comparison | revisions
main/task_apds9930.h file | annotate | diff | comparison | revisions
main/task_mqtt.c file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Thu Apr 20 15:19:31 2023 +0200
+++ b/CMakeLists.txt	Sun Sep 10 17:29:15 2023 +0200
@@ -4,7 +4,7 @@
 # CMakeLists in this exact order for cmake to work correctly
 cmake_minimum_required(VERSION 3.16)
 
-set(PROJECT_VER "0.4.2")
+set(PROJECT_VER "0.4.3")
 set(PROJECT_NAME "iotbalkon")
 
 set(EXTRA_COMPONENT_DIRS esp-idf-lib/components)
--- a/esp-idf-lib/components/apds9930/apds9930.c	Thu Apr 20 15:19:31 2023 +0200
+++ b/esp-idf-lib/components/apds9930/apds9930.c	Sun Sep 10 17:29:15 2023 +0200
@@ -360,10 +360,18 @@
     float	iac;
 
     float ALSIT = 2.73 * (256 - APDS9930_DEFAULT_ATIME);
-    if ((Ch0 - APDS9930_ALS_B * Ch1) > (APDS9930_ALS_C * Ch0 - APDS9930_ALS_D * Ch1))
-	iac = Ch0 - APDS9930_ALS_B * Ch1;
+    float iac0 = Ch0 - APDS9930_ALS_B * Ch1;
+    float iac1 = APDS9930_ALS_C * Ch0 - APDS9930_ALS_D * Ch1;
+
+    if ((iac0 < 0) && (iac1 < 0)) {
+	/* Overflow, too much light */
+	return 32767.0;
+    }
+
+    if (iac0 > iac1)
+	iac = iac0;
     else
-	iac = APDS9930_ALS_C * Ch0 - APDS9930_ALS_D * Ch1;
+	iac = iac1;
     //float iac  = fmax(Ch0 - APDS9930_ALS_B * Ch1, APDS9930_ALS_C * Ch0 - APDS9930_ALS_D * Ch1);
     if (iac < 0)
 	iac = 0;
@@ -378,10 +386,18 @@
     unsigned long	iac;
 
     unsigned long ALSIT = 2.73 * (256 - APDS9930_DEFAULT_ATIME);
-    if ((Ch0 - APDS9930_ALS_B * Ch1) > (APDS9930_ALS_C * Ch0 - APDS9930_ALS_D * Ch1))
-	iac = Ch0 - APDS9930_ALS_B * Ch1;
+    long iac0 = Ch0 - APDS9930_ALS_B * Ch1;
+    long iac1 = APDS9930_ALS_C * Ch0 - APDS9930_ALS_D * Ch1;
+
+    if ((iac0 < 0) && (iac1 < 0)) {
+        /* Overflow, too much light */
+        return 32767;
+    }
+
+    if (iac0 > iac1)
+	iac = iac0;
     else
-	iac = APDS9930_ALS_C * Ch0 - APDS9930_ALS_D * Ch1;
+	iac = iac1;
     //unsigned long iac  = max(Ch0 - APDS9930_ALS_B * Ch1, APDS9930_ALS_C * Ch0 - APDS9930_ALS_D * Ch1);
 //	if (iac < 0) iac = 0;
     unsigned long lpc  = APDS9930_GA * APDS9930_DF / (ALSIT * x[apds9930_getAmbientLightGain(dev)]);
--- 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);
 		}
 	    }
--- a/main/task_apds9930.h	Thu Apr 20 15:19:31 2023 +0200
+++ b/main/task_apds9930.h	Sun Sep 10 17:29:15 2023 +0200
@@ -26,6 +26,8 @@
     float		ambient_light;		///< Kind of Lux.
     uint8_t		gain;			///< APDS9930 last gain setting
     uint8_t		aglbit;			///< APDS9930 ALS Gain Level bit
+    uint16_t		ch0;			///< APDS9930 Channel 0
+    uint16_t		ch1;			///< APDS9930 Channel 1
     int			error;			///< Error result
 } APDS9930_State;
 
--- a/main/task_mqtt.c	Thu Apr 20 15:19:31 2023 +0200
+++ b/main/task_mqtt.c	Sun Sep 10 17:29:15 2023 +0200
@@ -190,6 +190,12 @@
 	payload = xstrcat(payload, (char *)",\"agl\":");
 	sprintf(buf, "%d", apds9930_state->aglbit);
 	payload = xstrcat(payload, buf);
+	payload = xstrcat(payload, (char *)",\"ch0\":");
+        sprintf(buf, "%d", apds9930_state->ch0);
+        payload = xstrcat(payload, buf);
+	payload = xstrcat(payload, (char *)",\"ch1\":");
+        sprintf(buf, "%d", apds9930_state->ch1);
+        payload = xstrcat(payload, buf);
 	xSemaphoreGive(xSemaphoreAPDS9930);
     }
     payload = xstrcat(payload, (char *)"}},\"solar\":{\"voltage\":");

mercurial