From d03f549fa5327ff2e138547e702877cb12a82189 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Sat, 22 Nov 2025 20:24:56 +0800
Subject: [PATCH] update TSL2561 source code to fix bug

---
 project/modules/tsl2561.c |   88 ++++++++++++++++++++++++++------------------
 1 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/project/modules/tsl2561.c b/project/modules/tsl2561.c
index 4148d74..a004e41 100644
--- a/project/modules/tsl2561.c
+++ b/project/modules/tsl2561.c
@@ -10,7 +10,7 @@
  *      ChangeLog:  1, Release initial version on "10/08/23 17:52:00"
  *
  * Pin connection:
- *               STH20 Module            Raspberry Pi Board
+ *              TSL2561 Module           Raspberry Pi Board
  *                   VCC      <----->      #Pin1(3.3V)
  *                   SDA0     <----->      #Pin27(SDA, BCM GPIO0)
  *                   SCL0     <----->      #Pin28(SCL, BCM GPIO1)
@@ -20,7 +20,6 @@
  *                  dtoverlay=i2c0,pins_0_1
  *
  ********************************************************************************/
-
 
 #include <string.h>
 #include <stdio.h>
@@ -38,8 +37,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include "util_proc.h"
 #include "logger.h"
+#include "utils.h"
 #include "tsl2561.h"
 
 
@@ -146,20 +145,19 @@
 int tsl2561_get_lux(float *lux)
 {
     int                 i, fd;
-	int                 rv = 0;
+    int                 rv = 0;
     char               *dev = TSL2561_I2CDEV;
-    float               div = 0.0;
 
     unsigned char       reg_data[REG_COUNT];
-    unsigned char       buf;
-    int                 chn0_data = 0;
-    int                 chn1_data = 0;
+    int                 ch0_data = 0;
+    int                 ch1_data = 0;
+    float               ratio = 0.0;
 
-	if( !lux )
-	{
-		log_error("Invalid input arguments\n");
-		return -1;
-	}
+    if( !lux )
+    {
+        log_error("Invalid input arguments\n");
+        return -1;
+    }
 
     if( (fd=open(dev, O_RDWR)) < 0)
     {
@@ -174,36 +172,54 @@
     /* Read register Channel0 and channel1 data from register */
     for(i=0; i<REG_COUNT; i++)
     {
-        tsl2561_readreg(fd, regs_addr[i], &reg_data[i]);
+        rv = tsl2561_readreg(fd, regs_addr[i], &reg_data[i]);
+        if( rv < 0)
+        {
+            log_error("TSL2561 read register failed\n");
+            goto cleanup;
+        }
     }
 
-    chn0_data = reg_data[1]*256 + reg_data[0]; /* Channel0 = DATA0HIGH<<8 + DATA0LOW  */
-    chn1_data = reg_data[3]*256 + reg_data[2]; /* channel1 = DATA1HIGH<<8 +  DATA1LOW */
+    rv = 0;
+    ch0_data = (reg_data[1]<<8) + reg_data[0]; /* 通道0:可见光+红外 */
+    ch1_data = (reg_data[3]<<8) + reg_data[2]; /* 通道1:仅红外 */
 
-    if( chn0_data<=0 || chn1_data<0 )
-    {
-		rv = -2;
-        goto OUT;
+
+    /* 根据datasheet,饱和时光照强度很大 */
+    if (ch0_data == 0xFFFF || ch1_data == 0xFFFF) {
+        *lux = 50000.0;
+        goto cleanup;
     }
 
-    div = (float)chn1_data / (float)chn0_data;
-
-    if( div>0 && div<=0.5 )
-        *lux = 0.304*chn0_data-0.062*chn0_data*pow(div,1.4);
-
-    else if( div>0.5 && div<=0.61 )
-        *lux = 0.0224*chn0_data-0.031*chn1_data;
-
-    else if( div>0.61 && div<=0.8 )
-        *lux = 0.0128*chn0_data-0.0153*chn1_data;
-
-    else if( div>0.8 && div<=1.3 )
-        *lux = 0.00146*chn0_data-0.00112*chn1_data;
-
-    else if( div>1.3 )
+    if (ch0_data == 0) { /* 无光照 */
         *lux = 0.0;
+        goto cleanup;
+    }
 
-OUT:
+    ratio = (float)ch1_data / (float)ch0_data;
+
+    if (ratio >= 0 && ratio <= 0.50) {
+        *lux = 0.0304f * ch0_data - 0.062f * ch0_data * powf(ratio, 1.4f);
+    }
+    else if (ratio <= 0.61) {
+        *lux = 0.0224f * ch0_data - 0.031f * ch1_data;
+    }
+    else if (ratio <= 0.80) {
+        *lux = 0.0128f * ch0_data - 0.0153f * ch1_data;
+    }
+    else if (ratio <= 1.30) {
+        *lux = 0.00146f * ch0_data - 0.00112f * ch1_data;
+    }
+    else { /* 超出有效范围 */
+        *lux = 0.0;
+    }
+
+    if (*lux < 0) { /* 确保结果非负 */
+        *lux = 0.0;
+    }
+
+cleanup:
     tsl2561_power(fd, OFF);
+    close(fd);
     return rv;
 }

--
Gitblit v1.9.1