guowenxue
2021-11-03 f696e95a01ee103605a9ed5fd23de58847ab9abc
modules/c/led.c
@@ -1,21 +1,21 @@
/*********************************************************************************
 *      Copyright:  (C) 2018 LingYun IoT System Studio
 *      Copyright:  (C) 2021 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  led.c
 *    Description:  This file is used to control RGB 3-colours LED
 *    Description:  This file is used to control RGB 3-colors LED
 *
 *     pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap
 *     pi@raspberrypi:~ $ gpio readall     #show RPi pin map
 *
 *                 LED      BCM           wPi
 *                  B ---- GPIO.13  ---- GPIO.23
 *                  R ---- GPIO.19  ---- GPIO.24
 *                  G ---- GPIO.26  ---- GPIO.25
 *                  I ---- GND      ----
 *                 LED     #PIN        BCM
 *                  I ----  39  ----   GND
 *                  G ----  37  ----   26
 *                  R ----  35  ----   19
 *                  B ----  33  ----   13
 *                 
 *        Version:  1.0.0(2018/10/14)
 *        Version:  1.0.0(2012/11/03)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2018/10/14 12:13:26"
 *      ChangeLog:  1, Release initial version on "2021/11/03 12:13:26"
 *                 
 ********************************************************************************/
@@ -28,9 +28,28 @@
#include <time.h>
#include <errno.h>
#include <wiringPi.h>
#include "led.h"
#include <gpiod.h>
#define msleep(x) usleep( 1000*(x) )
#define DELAY     500
#define ON        1
#define OFF       0
/* Three LEDs code */
enum
{
        LED_R = 0,
        LED_G,
        LED_B,
        LED_MAX,
};
/* RGB 3-color LED BCM pin number */
static int led_gpio[LED_MAX]= { 19, 26, 13 };
void init_led(void);
int turn_led(int which, int cmd);
int main(int argc, char *argv[])
{
@@ -39,48 +58,76 @@
   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;
}
#if 1 /* Use libgpiod lowlevel API */
struct gpiod_chip* chip;
void init_led(void)
{
   int         i;
          wiringPiSetup();
   for(i=0; i<LED_MAX; i++)
   chip = gpiod_chip_open_by_name("gpiochip0");
   if( !chip )
   {
             pinMode( led_gpio[i], OUTPUT );
      printf("gpiod open chip failure, maybe you need running as root\n");
      exit(1);
   }
}
int turn_led(int which, int cmd)
{
   if( which<0 || which>=LED_MAX )
      return -1;
   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 )
             digitalWrite (led_gpio[which], LOW);
   {
      gpiod_line_set_value(line, 0);
   }
   else
             digitalWrite (led_gpio[which], HIGH);
   {
      gpiod_line_set_value(line, 1);
   }
   return 0;
   msleep(DELAY);
}
#else /* use libgpiod ctxless high level API */
void init_led(void)
{
}
static 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