From c395bea78de70eb968c697e55c486d1d249f4b58 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Thu, 27 Nov 2025 21:44:58 +0800
Subject: [PATCH] Update lightd code for incoming detect

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

diff --git a/project/lightd/lightd.c b/project/lightd/lightd.c
index e3df1ec..9d5b03d 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>
@@ -142,42 +142,38 @@
     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 in every 10 minutes  */
+        if(check_timeout(&last_time, 600))
         {
-            log_warn("Someone incoming detected by %s infrared\n", rv&FLAG_INFRARED_INDOOR?"indoor":"hallway");
+            if( tsl2561_get_lux(&lux) < 0 )
+            {
+                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