From 24345339421493cdacdbaae0248c3928ea9404c2 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Tue, 18 Nov 2025 12:41:40 +0800
Subject: [PATCH] update proc.h and utils.h

---
 project/modules/tsl2561.c |   31 +++++++++++++++++--------------
 1 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/project/modules/tsl2561.c b/project/modules/tsl2561.c
index 4148d74..7bb19d7 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,7 +37,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include "util_proc.h"
+#include "utils.h"
 #include "logger.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;
 
-	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