LingYun Studio embeded system framwork software, such as thirdparty build shell and lingyun library
guowenxue
2024-08-21 1e32814e0bc7ee1eb5b7f64b703f8bd03cba7805
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*********************************************************************************
 *      Copyright:  (C) 2024 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  led.c
 *    Description:  This file is used to control RGB 3-colors LED
 *
 * Pin connection:
 *               RGB Led Module           IGKBoard-IMX6ULL
 *                   R        <----->      #Pin36 (GPIO1_IO11)
 *                   G        <----->      #Pin38 (GPIO1_IO10)
 *                   B        <----->      #Pin40 (GPIO5_IO09)
 *                  GND       <----->      GND
 *
 ********************************************************************************/
 
#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 <signal.h>
#include <getopt.h>
#include <libgen.h>
 
#include "led.h"
 
int g_stop = 0;
 
static inline void msleep(unsigned long ms);
void sig_handler(int signum);
 
int main(int argc, char *argv[])
{
    int                 rv;
 
    if( (rv=init_led()) < 0 )
    {
        printf("initial leds gpio failure, rv=%d\n", rv);
        return 1;
    }
    printf("initial RGB Led gpios okay\n");
 
    signal(SIGINT,  sig_handler);
    signal(SIGTERM, sig_handler);
 
    while( !g_stop )
    {
        turn_led(LED_R, ON);
        msleep(DELAY);
        turn_led(LED_R, OFF);
        msleep(DELAY);
 
        toggle_led(LED_G);
        msleep(DELAY);
        toggle_led(LED_G);
        msleep(DELAY);
 
        blink_led(LED_B, DELAY);
    }
 
    term_led();
    return 0;
}
 
 
/*+---------------------------------+
 *| Leds API based on libgpiod v2.0 |
 *+---------------------------------+*/
 
static led_t leds_info[LED_CNT] =
{
    {"red",   0, 11, ACTIVE_HIGH, 0, NULL}, /* #Pin36, GPIO1_IO11 on chip0 line 11, active high */
    {"green", 0, 10, ACTIVE_HIGH, 0, NULL}, /* #Pin38, GPIO1_IO10 on chip0 line 10, active high */
    {"blue",  4, 9,  ACTIVE_HIGH, 0, NULL}, /* #Pin40, GPIO5_IO09 on chip4 line 09, active high */
};
 
/* Leds context */
static leds_t leds =
{
    .leds  = leds_info,
    .count = LED_CNT,
};
 
int init_led(void)
{
    led_t                       *led;
    int                          i, rv = 0;
    char                         chip_dev[32];
    struct gpiod_chip           *chip;      /* gpio chip */
    struct gpiod_line_settings  *settings;  /* gpio direction, bias, active_low, value */
    struct gpiod_line_config    *line_cfg;  /* gpio line */
    struct gpiod_request_config *req_cfg;   /* gpio consumer, it can be NULL */
 
    /* defined in libgpiod-2.0/lib/line-settings.c:
 
        struct gpiod_line_settings {
            enum gpiod_line_direction direction;
            enum gpiod_line_edge edge_detection;
            enum gpiod_line_drive drive;
            enum gpiod_line_bias bias;
            bool active_low;
            enum gpiod_line_clock event_clock;
            long debounce_period_us;
            enum gpiod_line_value output_value;
        };
     */
    settings = gpiod_line_settings_new();
    if (!settings)
    {
        printf("unable to allocate line settings\n");
        rv = -2;
        goto cleanup;
    }
 
    /* defined in libgpiod-2.0/lib/line-config.c
 
        struct gpiod_line_config {
            struct per_line_config line_configs[LINES_MAX];
            size_t num_configs;
            enum gpiod_line_value output_values[LINES_MAX];
            size_t num_output_values;
            struct settings_node *sref_list;
        };
    */
 
    line_cfg = gpiod_line_config_new();
    if (!line_cfg)
    {
        printf("unable to allocate the line config structure");
        rv = -2;
        goto cleanup;
    }
 
 
    /* defined in libgpiod-2.0/lib/request-config.c:
 
        struct gpiod_request_config {
            char consumer[GPIO_MAX_NAME_SIZE];
            size_t event_buffer_size;
        };
     */
    req_cfg = gpiod_request_config_new();
    if (!req_cfg)
    {
        printf("unable to allocate the request config structure");
        rv = -2;
        goto cleanup;
    }
 
    for(i=0; i<leds.count; i++)
    {
        led = &leds.leds[i];
 
        snprintf(chip_dev, sizeof(chip_dev), "/dev/gpiochip%d", led->chip_num);
        chip = gpiod_chip_open(chip_dev);
        if( !chip )
        {
            printf("open gpiochip failure, maybe you need running as root\n");
            rv = -3;
            goto cleanup;
        }
 
        /* Set as output direction, active low and default level as inactive */
        gpiod_line_settings_reset(settings);
        gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_OUTPUT);
        gpiod_line_settings_set_active_low(settings, led->active);
        gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_INACTIVE);
 
        /* set gpio line */
        gpiod_line_config_reset(line_cfg);
        gpiod_line_config_add_line_settings(line_cfg, &led->gpio_num, 1, settings);
 
        /* Can be NULL for default settings. */
        gpiod_request_config_set_consumer(req_cfg, led->name);
 
        /* Request a set of lines for exclusive usage. */
        led->request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
 
        gpiod_chip_close(chip);
        //printf("request %5s led[%d] for gpio output okay\n", led->name, led->gpio);
    }
 
cleanup:
 
    if( rv< 0 )
        term_led();
 
    if( line_cfg )
        gpiod_line_config_free(line_cfg);
 
    if( req_cfg )
        gpiod_request_config_free(req_cfg);
 
    if( settings )
        gpiod_line_settings_free(settings);
 
    return rv;
}
 
int term_led(void)
{
    int            i;
    led_t         *led;
 
    printf("terminate RGB Led gpios\n");
 
    for(i=0; i<leds.count; i++)
    {
        led = &leds.leds[i];
 
        if( led->request )
        {
            turn_led(i, OFF);
            gpiod_line_request_release(led->request);
        }
    }
 
    return 0;
}
 
int turn_led(int which, int cmd)
{
    led_t         *led;
    int            rv = 0;
    int            value = 0;
 
    if( which<0 || which>=leds.count )
    {
        printf("Invalid input arguments\n");
        return -1;
    }
 
    led = &leds.leds[which];
 
    value = OFF==cmd ? GPIOD_LINE_VALUE_INACTIVE : GPIOD_LINE_VALUE_ACTIVE;
    gpiod_line_request_set_value(led->request, led->gpio_num, value);
 
    led->status = OFF==cmd ? OFF : ON;
 
    return 0;
}
 
int toggle_led(int which)
{
    led_t         *led;
 
    if( which<0 || which>=leds.count )
    {
        printf("Invalid input arguments\n");
        return -1;
    }
 
    led = &leds.leds[which];
 
    return turn_led(which, !led->status);
}
 
void blink_led(int which, int interval)
{
    led_t         *led;
 
    turn_led(which, ON);
    msleep(interval);
 
    turn_led(which, OFF);
    msleep(interval);
}
 
/*+-------------------------------+
 *|      Misc functions API       |
 *+-------------------------------+*/
 
static inline void msleep(unsigned long ms)
{
    struct timespec cSleep;
    unsigned long ulTmp;
 
    cSleep.tv_sec = ms / 1000;
    if (cSleep.tv_sec == 0)
    {
        ulTmp = ms * 10000;
        cSleep.tv_nsec = ulTmp * 100;
    }
    else
    {
        cSleep.tv_nsec = 0;
    }
 
    nanosleep(&cSleep, 0);
 
    return ;
}
 
void sig_handler(int signum)
{
    switch( signum )
    {
        case SIGINT:
        case SIGTERM:
            g_stop = 1;
 
        default:
            break;
    }
 
    return ;
}