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 | 68 ++++++++++++++++++++++ modules/c/makefile | 4 + modules/c/infrared.c | 77 +++++++++++++++++++++++++ modules/c/led.h | 21 ++++-- 4 files changed, 158 insertions(+), 12 deletions(-) diff --git a/modules/c/infrared.c b/modules/c/infrared.c index 3ebbb99..25cd18a 100644 --- a/modules/c/infrared.c +++ b/modules/c/infrared.c @@ -5,6 +5,13 @@ * Filename: infrared.c * Description: This file * + * pi@raspberrypi:~ $ gpio readall #show BCM and wPi pinmap + * + * PIN #PIN BCM wPi + * VCC(left) ---- 2 ---- 5v ---- 5v + * I/O(mid) ---- 32 ---- 12 ---- 26 + * GND(right) ---- 34 ---- 0v ---- 0v + * * Version: 1.0.0(30/01/19) * Author: Guo Wenxue <guowenxue@gmail.com> * ChangeLog: 1, Release initial version on "30/01/19 03:37:16" @@ -13,10 +20,13 @@ #include <stdio.h> #include <unistd.h> + + +#ifdef CONFIG_USE_WIRINGPI + #include <wiringPi.h> -/*Monitor RPi I/O Pin connected to PIN#38, and wPi pin number is 28 */ -#define INFRARED_PIN 28 +#define INFRARED_PIN 26 int main (int argc, char **argv) { @@ -33,3 +43,66 @@ return 0; } +#else + +#include <gpiod.h> + +#define INFRARED_PIN 12 + +int main (int argc, char **argv) +{ + struct gpiod_chip* chip; + struct gpiod_line* line; + int rv = 0; + struct gpiod_line_event event; + + chip = gpiod_chip_open_by_name("gpiochip0"); + if( !chip ) + { + printf("gpiod open chip failure, maybe you need running as root\n"); + return 1; + } + + line = gpiod_chip_get_line(chip, INFRARED_PIN); + if( !line ) + { + printf("gpiod get line[%d] failure\n", INFRARED_PIN); + rv = 2; + goto cleanup; + } + + + if( gpiod_line_request_rising_edge_events(line, "infrared") < 0 ) + { + printf("gpiod request rising edge event failure\n"); + rv = 3; + goto cleanup; + } + + while(1) + { + /* This function will block, must call it to setup */ + 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 */ + 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"); + } + } + +cleanup: + gpiod_chip_close(chip); + return rv; +} + + +#endif diff --git a/modules/c/led.c b/modules/c/led.c index 959edd8..2bf3df8 100644 --- a/modules/c/led.c +++ b/modules/c/led.c @@ -28,11 +28,9 @@ #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[]) @@ -60,6 +58,9 @@ return 0; } +#ifdef CONFIG_USE_WIRINGPI +#include <wiringPi.h> + void init_led(void) { int i; @@ -86,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 diff --git a/modules/c/led.h b/modules/c/led.h index dfc411c..125d80d 100644 --- a/modules/c/led.h +++ b/modules/c/led.h @@ -5,13 +5,13 @@ * Filename: led.h * Description: This file is used to control RGB 3-colours LED * - * pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap + * pi@raspberrypi:~ $ gpio readall #show BCM and wPi pinmap * - * LED BCM wPi - * G ---- GPIO.13 ---- GPIO.23 - * R ---- GPIO.19 ---- GPIO.24 - * B ---- GPIO.26 ---- GPIO.25 - * I ---- GND ---- + * LED #PIN BCM wPi + * I ---- 9 ---- GND ---- GND + * G ---- 11 ---- 17 ---- 0 + * R ---- 13 ---- 27 ---- 2 + * B ---- 15 ---- 22 ---- 3 * * Version: 1.0.0(2018/10/14) * Author: Guo Wenxue <guowenxue@gmail.com> @@ -25,6 +25,7 @@ #define OFF 0 #define ON 1 + /* Three LEDs code */ enum { @@ -36,8 +37,12 @@ /* 3 LEDs WiringPi GPIO port */ - /* LED_R LED_G LED_B */ -static int led_gpio[LED_MAX]= { 24, 23, 25 }; + +#ifdef CONFIG_USE_WIRINGPI +static int led_gpio[LED_MAX]= { 2, 0, 3 }; +#else /* use libgpiod library */ +static int led_gpio[LED_MAX]= { 27, 17, 22 }; +#endif void init_led(void); diff --git a/modules/c/makefile b/modules/c/makefile index 9eaad45..240ce22 100644 --- a/modules/c/makefile +++ b/modules/c/makefile @@ -18,7 +18,11 @@ CFLAGS+=-I${PWD} #CFLAGS+=-Wall -Werror +# default use libgpiod library +#CFLAGS+=-DCONFIG_USE_WIRINGPI + LDFLAGS+=-lwiringPi +LDFLAGS+=-lgpiod LDFLAGS+=-lpthread LDFLAGS+=-lm -- Gitblit v1.9.1