From d0a9532e1425c6052ac7221c45433a48bf43ba50 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Sat, 09 Jan 2021 16:17:56 +0800
Subject: [PATCH] update led and infrared source code

---
 modules/c/led.c |   85 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 76 insertions(+), 9 deletions(-)

diff --git a/modules/c/led.c b/modules/c/led.c
index 8007bcd..2bf3df8 100644
--- a/modules/c/led.c
+++ b/modules/c/led.c
@@ -8,9 +8,9 @@
  *     pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap  
  *
  *                 LED      BCM           wPi 
- *                  B ---- GPIO.13  ---- GPIO.23
+ *                  G ---- GPIO.13  ---- GPIO.23
  *                  R ---- GPIO.19  ---- GPIO.24    
- *                  G ---- GPIO.26  ---- GPIO.25
+ *                  B ---- GPIO.26  ---- GPIO.25
  *                  I ---- GND      ----
  *                 
  *        Version:  1.0.0(2018/10/14)
@@ -28,9 +28,10 @@
 #include <time.h>
 #include <errno.h>
 
-#include <wiringPi.h>
 #include "led.h"
 
+#define msleep(x) usleep( 1000*(x) )
+#define DELAY     500
 
 int main(int argc, char *argv[])
 {
@@ -39,23 +40,26 @@
 	while(1)
 	{
 		turn_led(LED_R, ON);
-		sleep(1);
+		msleep(DELAY);
 		turn_led(LED_R, OFF);
-		sleep(1);
+		msleep(DELAY);
 
 		turn_led(LED_G, ON);
-		sleep(1);
+		msleep(DELAY);
 		turn_led(LED_G, OFF);
-		sleep(1);
+		msleep(DELAY);
 
 		turn_led(LED_B, ON);
-		sleep(1);
+		msleep(DELAY);
 		turn_led(LED_B, OFF);
-		sleep(1);
+		msleep(DELAY);
 	}
 
         return 0;
 }
+
+#ifdef CONFIG_USE_WIRINGPI
+#include <wiringPi.h>
 
 void init_led(void)
 {
@@ -83,4 +87,67 @@
 	return 0;
 }
 
+#else /* use libgpiod library */
 
+#include <gpiod.h>
+
+#if 1 /* Use libgpiod lowlevel API */
+
+struct gpiod_chip* chip;
+
+void init_led(void)
+{
+	chip = gpiod_chip_open_by_name("gpiochip0");
+	if( !chip )
+	{
+		printf("gpiod open chip failure, maybe you need running as root\n");
+		exit(1);
+	}
+}
+
+int turn_led(int which, int cmd)
+{
+	int                rv = 0;
+	struct gpiod_line* line;
+       
+	line = gpiod_chip_get_line(chip, led_gpio[which]);
+	if( !line )
+	{
+		rv = -2;
+	}
+       
+	gpiod_line_request_output(line, "led", 0);
+
+	if( OFF == cmd )
+	{
+		gpiod_line_set_value(line, 0);
+	}
+	else
+	{
+		gpiod_line_set_value(line, 1);
+	}
+}
+
+
+#else /* use libgpiod ctxless high level API */
+void init_led(void)
+{
+
+}
+
+void gpiod_ctxless_cb(void *data)
+{
+	sleep(1);
+}
+
+int turn_led(int which, int cmd)
+{
+	if( OFF == cmd )
+		gpiod_ctxless_set_value("gpiochip0", led_gpio[which], 0, false, "led", gpiod_ctxless_cb, NULL);
+	else
+		gpiod_ctxless_set_value("gpiochip0", led_gpio[which], 1, false, "led", gpiod_ctxless_cb, NULL);
+}
+#endif
+
+
+#endif

--
Gitblit v1.9.1