From 47579e3790b1ee5a1ad2ce35ea6028cd9ddf6ea0 Mon Sep 17 00:00:00 2001
From: GuoWenxue <“guowenxue@gmail.com”>
Date: Fri, 29 Apr 2022 09:23:27 +0800
Subject: [PATCH] update client_main.c to use packet_segmented_pack

---
 modules/c/infrared.c |   65 +++++++++++++++++---------------
 1 files changed, 34 insertions(+), 31 deletions(-)

diff --git a/modules/c/infrared.c b/modules/c/infrared.c
index 868638d..c6f13fe 100644
--- a/modules/c/infrared.c
+++ b/modules/c/infrared.c
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
 //#define CONFIG_USE_WIRINGPI
@@ -30,17 +31,27 @@
 
 #include <wiringPi.h>
 
-#define INFRARED_PIN           4
-//#define INFRARED_PIN           5
-
 int main (int argc, char **argv)
 {
+    int           wPiPin;
+
+    if( argc != 2 )
+    {
+        printf("%s [wPi PinNum]\n", argv[0]);
+        printf("Exampe indoor : %s 4\n", argv[0]);
+        printf("Exampe hallway: %s 5\n", argv[0]);
+        return 0;
+    }
+
+    wPiPin = atoi(argv[1]);
+    printf("Start infrared detect on wPi Pin[#%d]\n", wPiPin);
+
     wiringPiSetup();
-    pinMode(INFRARED_PIN, INPUT);
+    pinMode(wPiPin, INPUT);
 
     while(1)
     {
-        printf("Infrared monitor: %s\n", digitalRead(INFRARED_PIN)? "Someone is closing!":"No one nearby!");
+        printf("Infrared monitor: %s\n", digitalRead(wPiPin)? "Someone is closing!":"No one nearby!");
         sleep(1);
     }
 
@@ -51,15 +62,24 @@
 
 #include <gpiod.h>
 
-#define INFRARED_PIN           23
-//#define INFRARED_PIN           24
-
 int main (int argc, char **argv)
 {
     struct gpiod_chip*       chip;
     struct gpiod_line*       line;
     int                      rv = 0;
     struct gpiod_line_event  event;
+    int                      bcmPin;
+
+    if( argc != 2 )
+    {
+        printf("%s [BCM PinNum]\n", argv[0]);
+        printf("Exampe indoor : %s 23\n", argv[0]);
+        printf("Exampe hallway: %s 24\n", argv[0]);
+        return 0;
+    }
+
+    bcmPin = atoi(argv[1]);
+    printf("Start infrared detect on BCM Pin[#%d]\n", bcmPin);
 
     chip = gpiod_chip_open_by_name("gpiochip0");
     if( !chip )
@@ -68,19 +88,17 @@
         return 1;
     }
 
-    line = gpiod_chip_get_line(chip, INFRARED_PIN);
+    line = gpiod_chip_get_line(chip, bcmPin);
     if( !line )
     {
-        printf("gpiod get line[%d] failure\n", INFRARED_PIN);
+        printf("gpiod get line[%d] failure\n", bcmPin);
         rv = 2;
         goto cleanup;
     }
 
-
-    //if( gpiod_line_request_rising_edge_events(line, "infrared") < 0 )
-    if( gpiod_line_request_falling_edge_events(line, "infrared") < 0 )
+    if( gpiod_line_request_input(line, "infrared") < 0 )
     {
-        printf("gpiod request rising edge event failure: %s\n", strerror(errno));
+        printf("gpiod request input [#%d] failure: %s\n", bcmPin, strerror(errno) );
         rv = 3;
         goto cleanup;
     }
@@ -88,23 +106,8 @@
     while(1)
     {
         /* This function will block, must call it to setup */
-        printf("Start gpiod_line_event_wait...\n");
-        if( gpiod_line_event_wait(line, NULL) < 0 )
-        {
-            printf("gpiod line wait event failure!\n");
-        }
-
-        /* This function will block, must read to clear the event  */
-        printf("Start gpiod_line_event_read...\n");
-        if (gpiod_line_event_read(line, &event) < 0)
-        {
-            printf("gpiod line wait event failure!\n");
-        }
-
-        if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
-        {
-            printf("Infrared monitor: Someone is closing!\n");
-        }
+        printf("Infrared monitor: %s\n", gpiod_line_get_value(line) ? "Someone is closing!":"No one nearby!");
+        sleep(1);
     }
 
 cleanup:

--
Gitblit v1.9.1