From 0fd2ff9f9aca1f30d6b4620f90802223cc221fe0 Mon Sep 17 00:00:00 2001
From: GuoWenxue <“guowenxue@gmail.com”>
Date: Fri, 22 Apr 2022 09:17:24 +0800
Subject: [PATCH] update socket.c for listen socket

---
 modules/c/relay.c |   34 ++++++++++++++++------------------
 1 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/modules/c/relay.c b/modules/c/relay.c
index 3e360b7..392b3c5 100644
--- a/modules/c/relay.c
+++ b/modules/c/relay.c
@@ -14,11 +14,17 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <libgen.h>
-
 #include <string.h>
 
-#include <wiringPi.h>
-#include "relay.h"
+#include <gpiod.h>
+
+#define ON              1
+#define OFF             0
+
+/* Relay    #Pin(29)    GPIO05(BCM 5)  */
+#define relay_pin       5
+
+int turn_relay(int cmd);
 
 int main (int argc, char **argv)
 {
@@ -27,9 +33,6 @@
         printf("Usage: %s [on/off]\n", basename(argv[0]));
         return -1;
     }
-
-    wiringPiSetup();
-    relay_init();
 
     if( !strstr(argv[1], "on") || !strstr(argv[1], "on") )
     {
@@ -43,21 +46,16 @@
     return 0;
 }
 
-void relay_init(void)
+static void gpiod_ctxless_cb(void *data)
 {
-    pinMode(RELAY_PIN, OUTPUT);
+        sleep(3);
 }
 
-
-void turn_relay(int cmd)
+int turn_relay(int cmd)
 {
-    if( OFF == cmd )
-    {
-        digitalWrite ( RELAY_PIN, HIGH );
-    }
-    else
-    {
-        digitalWrite ( RELAY_PIN, LOW );
-    }
+        if( OFF == cmd )
+                gpiod_ctxless_set_value("gpiochip0", relay_pin, 0, false, "relay", gpiod_ctxless_cb, NULL);
+        else
+                gpiod_ctxless_set_value("gpiochip0", relay_pin, 1, false, "relay", gpiod_ctxless_cb, NULL);
 }
 

--
Gitblit v1.9.1