guowenxue
2021-08-17 0a9eeb590f106d7bdf76dc391da311d22dac1c6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*********************************************************************************
 *      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 
 *                  G ---- GPIO.13  ---- GPIO.23
 *                  R ---- GPIO.19  ---- GPIO.24    
 *                  B ---- 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 "led.h"
 
#define msleep(x) usleep( 1000*(x) )
#define DELAY     500
 
int main(int argc, char *argv[])
{
    init_led();
 
    while(1)
    {
        turn_led(LED_R, ON);
        msleep(DELAY);
        turn_led(LED_R, OFF);
        msleep(DELAY);
 
        turn_led(LED_G, ON);
        msleep(DELAY);
        turn_led(LED_G, OFF);
        msleep(DELAY);
 
        turn_led(LED_B, ON);
        msleep(DELAY);
        turn_led(LED_B, OFF);
        msleep(DELAY);
    }
 
        return 0;
}
 
#ifdef CONFIG_USE_WIRINGPI
#include <wiringPi.h>
 
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;
}
 
#else /* use libgpiod library */
 
#include <gpiod.h>
 
#if 0 /* 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