From 2e84abfd073ec3047c7b3295c47f109007f3bd80 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 02 Feb 2026 16:34:56 +0800
Subject: [PATCH] Update lightd code to get lux value without timeout

---
 project/lightd/lightd.c |   62 +++++++++++++-----------------
 1 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/project/lightd/lightd.c b/project/lightd/lightd.c
index 22ce0cb..a025598 100644
--- a/project/lightd/lightd.c
+++ b/project/lightd/lightd.c
@@ -2,8 +2,8 @@
  *      Copyright:  (C) 2019 LingYun IoT System Studio
  *                  All rights reserved.
  *
- *       Filename:  main.c
- *    Description:  This file
+ *       Filename:  lightd.c
+ *    Description:  This file is infrared detect auto light program on RaspberryPi
  *
  *        Version:  1.0.0(29/01/19)
  *         Author:  Guo Wenxue <guowenxue@gmail.com>
@@ -23,7 +23,7 @@
 #include <cjson/cJSON.h>
 
 #include "logger.h"
-#include "util_proc.h"
+#include "proc.h"
 #include "config.h"
 #include "tsl2561.h"
 #include "ds18b20.h"
@@ -60,7 +60,6 @@
 
     int                rv;
     float              lux = 0.0;
-    time_t             last_time = 0;
     iotd_ctx_t         ctx;
     hwinfo_t          *hwinfo = &ctx.hwinfo;
 
@@ -142,42 +141,35 @@
     log_info("Auto light control thread start\n");
     while( ! g_signal.stop )
     {
-        /* TSL2561 update lux in every 5 minutes  */
-        if(check_timeout(&last_time, 300))
+        /* Block detect human incoming */
+        rv = infrared_detect();
+        if( rv == 0 )
+            goto nextloop;
+
+        log_warn("Someone incoming detected by %s infrared\n", rv&FLAG_INFRARED_INDOOR?"indoor":"hallway");
+
+        /* TSL2561 sensor is not present, do not turn on the light */
+        if( !hwinfo->tsl2561 )
         {
-            /* The TSL2561 sensor is not present, do not turn on the light */
-            if( !hwinfo->tsl2561 )
-            {
-                log_error("TSL2561 is not present, do not turn on the light\n");
-                lux = hwinfo->lux_threshold + 100.0;
-                goto nextloop;
-            }
-
-            /* The TSL2561 sensor failed, do not turn on the light */
-            if( tsl2561_get_lux(&lux) < 0 )
-            {
-                log_error("TSL2561 sample failed, do not turn on the light\n");
-                lux = hwinfo->lux_threshold + 100.0;
-                goto nextloop;
-            }
-
-            if( lux > hwinfo->lux_threshold )
-                log_info("Lux[%.3f] > Treshold[%.3f], don't auto light\n", lux, hwinfo->lux_threshold);
-            else
-                log_info("Lux[%.3f] <= Treshold[%.3f], need auto light\n", lux, hwinfo->lux_threshold);
-        }
-
-        /* It's bright enough, no lighting is needed */
-        if( lux > hwinfo->lux_threshold )
-        {
+            log_error("TSL2561 is not present, do not turn on the light\n");
             goto nextloop;
         }
 
-        /* It's not bright enough, lighting is necessary */
-        rv = infrared_detect();
-        if( rv != 0 )
+        /* TSL2561 update lux value now */
+        if( tsl2561_get_lux(&lux) < 0 )
         {
-            log_warn("Someone incoming detected by %s infrared\n", rv&FLAG_INFRARED_INDOOR?"indoor":"hallway");
+            log_error("TSL2561 sample failed, do not turn on the light\n");
+            goto nextloop;
+        }
+
+        /* Check need auto light or not */
+        if( lux > hwinfo->lux_threshold )
+        {
+            log_info("Lux[%.3f] > Treshold[%.3f], don't auto light\n", lux, hwinfo->lux_threshold);
+        }
+        else
+        {
+            log_info("Lux[%.3f] <= Treshold[%.3f], need auto light\n", lux, hwinfo->lux_threshold);
 
             turn_light("indoor", ON);
             turn_light("hallway", ON);

--
Gitblit v1.9.1