guowenxue
2018-10-14 e66340112f34b9ddcf18a151c794b76bc140b00a
Add GPIO LED program
2 files added
134 ■■■■■ changed files
modules/c/led.c 86 ●●●●● patch | view | raw | blame | history
modules/c/led.h 48 ●●●●● patch | view | raw | blame | history
modules/c/led.c
New file
@@ -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;
}
modules/c/led.h
New file
@@ -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