From e66340112f34b9ddcf18a151c794b76bc140b00a Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Sun, 14 Oct 2018 19:22:10 +0800 Subject: [PATCH] Add GPIO LED program --- modules/c/led.c | 86 ++++++++++++++++++++++++++++ modules/c/led.h | 48 ++++++++++++++++ 2 files changed, 134 insertions(+), 0 deletions(-) diff --git a/modules/c/led.c b/modules/c/led.c new file mode 100644 index 0000000..8007bcd --- /dev/null +++ b/modules/c/led.c @@ -0,0 +1,86 @@ +/********************************************************************************* + * Copyright: (C) 2018 LingYun IoT System Studio + * All rights reserved. + * + * Filename: led.c + * Description: This file is used to control RGB 3-colours LED + * + * pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap + * + * LED BCM wPi + * B ---- GPIO.13 ---- GPIO.23 + * R ---- GPIO.19 ---- GPIO.24 + * G ---- GPIO.26 ---- GPIO.25 + * I ---- GND ---- + * + * Version: 1.0.0(2018/10/14) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2018/10/14 12:13:26" + * + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <time.h> +#include <errno.h> + +#include <wiringPi.h> +#include "led.h" + + +int main(int argc, char *argv[]) +{ + init_led(); + + while(1) + { + turn_led(LED_R, ON); + sleep(1); + turn_led(LED_R, OFF); + sleep(1); + + turn_led(LED_G, ON); + sleep(1); + turn_led(LED_G, OFF); + sleep(1); + + turn_led(LED_B, ON); + sleep(1); + turn_led(LED_B, OFF); + sleep(1); + } + + return 0; +} + +void init_led(void) +{ + int i; + + wiringPiSetup(); + + for(i=0; i<LED_MAX; i++) + { + pinMode( led_gpio[i], OUTPUT ); + } +} + +int turn_led(int which, int cmd) +{ + if( which<0 || which>=LED_MAX ) + return -1; + + + if( OFF == cmd ) + digitalWrite (led_gpio[which], LOW); + else + digitalWrite (led_gpio[which], HIGH); + + return 0; +} + + diff --git a/modules/c/led.h b/modules/c/led.h new file mode 100644 index 0000000..582d845 --- /dev/null +++ b/modules/c/led.h @@ -0,0 +1,48 @@ +/********************************************************************************* + * Copyright: (C) 2018 LingYun IoT System Studio + * All rights reserved. + * + * Filename: led.c + * Description: This file is used to control RGB 3-colours LED + * + * pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap + * + * LED BCM wPi + * B ---- GPIO.13 ---- GPIO.23 + * R ---- GPIO.19 ---- GPIO.24 + * G ---- GPIO.26 ---- GPIO.25 + * I ---- GND ---- + * + * Version: 1.0.0(2018/10/14) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2018/10/14 12:13:26" + * + ********************************************************************************/ + +#ifndef __LED_H +#define __LED_H + +#define OFF 0 +#define ON 1 + +/* Three LEDs code */ +enum +{ + LED_R = 0, + LED_G, + LED_B, + LED_MAX, +}; + +/* 3 LEDs WiringPi GPIO port */ + + /* LED_R LED_G LED_B */ +static int led_gpio[LED_MAX]= { 24, 25, 23 }; + + +void init_led(void); +int turn_led(int which, int cmd); + + +#endif + -- Gitblit v1.9.1