update led and infrared source code
| | |
| | | * 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" |
| | |
| | | |
| | | #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) |
| | | { |
| | |
| | | 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 |
| | |
| | | #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[]) |
| | |
| | | return 0; |
| | | } |
| | | |
| | | #ifdef CONFIG_USE_WIRINGPI |
| | | #include <wiringPi.h> |
| | | |
| | | void init_led(void) |
| | | { |
| | | int i; |
| | |
| | | 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 |
| | |
| | | * 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> |
| | |
| | | #define OFF 0 |
| | | #define ON 1 |
| | | |
| | | |
| | | /* Three LEDs code */ |
| | | enum |
| | | { |
| | |
| | | |
| | | /* 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); |
| | |
| | | CFLAGS+=-I${PWD} |
| | | #CFLAGS+=-Wall -Werror |
| | | |
| | | # default use libgpiod library |
| | | #CFLAGS+=-DCONFIG_USE_WIRINGPI |
| | | |
| | | LDFLAGS+=-lwiringPi |
| | | LDFLAGS+=-lgpiod |
| | | LDFLAGS+=-lpthread |
| | | LDFLAGS+=-lm |
| | | |