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 |   72 ++++++++++++++++++++++--------------
 1 files changed, 44 insertions(+), 28 deletions(-)

diff --git a/project/modules/tsl2561.c b/project/modules/tsl2561.c
index c23f61c..a004e41 100644
--- a/project/modules/tsl2561.c
+++ b/project/modules/tsl2561.c
@@ -37,8 +37,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include "util_proc.h"
 #include "logger.h"
+#include "utils.h"
 #include "tsl2561.h"
 
 
@@ -147,12 +147,11 @@
     int                 i, fd;
     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 )
     {
@@ -173,36 +172,53 @@
     /* 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