From 3ef6cf39749c00366b12aef9f023aac3bda16f2d Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 17 Nov 2025 23:39:25 +0800
Subject: [PATCH] update modules for tsl2561 and ds18b20

---
 project/modules/tsl2561.c |   29 ++++++++++++++++-------------
 1 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/project/modules/tsl2561.c b/project/modules/tsl2561.c
index 4148d74..b912433 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>
@@ -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;
 
-	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,7 +172,9 @@
     /* 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)
+            goto failed;
     }
 
     chn0_data = reg_data[1]*256 + reg_data[0]; /* Channel0 = DATA0HIGH<<8 + DATA0LOW  */
@@ -182,8 +182,8 @@
 
     if( chn0_data<=0 || chn1_data<0 )
     {
-		rv = -2;
-        goto OUT;
+        rv = -2;
+        goto cleanup;
     }
 
     div = (float)chn1_data / (float)chn0_data;
@@ -203,7 +203,10 @@
     else if( div>1.3 )
         *lux = 0.0;
 
-OUT:
+cleanup:
     tsl2561_power(fd, OFF);
+
+failed:
+    close(fd);
     return rv;
 }

--
Gitblit v1.9.1