RaspberrPi project source code
guowenxue
7 hours ago 7be9084b27e3385f27f7ac5d0e946f4dcaa94d1e
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
/*********************************************************************************
 *      Copyright:  (C) 2019 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  gpio.c
 *    Description:  This file is GPIO input/output functions
 *
 *        Version:  1.0.0(2019年06月24日)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2019年06月24日 23时46分47秒"
 *
 ********************************************************************************/
 
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
 
#include "logger.h"
#include "util_proc.h"
#include "gpio.h"
 
#define RPI_GPIONAME        "gpiochip0"
#include <errno.h>
 
#include "logger.h"
#include "util_proc.h"
#include "gpio.h"
 
#define RPI_GPIONAME        "gpiochip0"
 
static struct gpiod_chip    *s_chip;
static gpio_t               *s_gpio = NULL;
 
int gpio_init(gpio_t *gpio)
{
    int                 i;
    int                 rv;
 
    s_gpio = gpio;
    if( !gpio )
    {
        log_error("Invalid input arguments $gpio\n");
        return -1;
    }
 
    if( !gpio->incnt && !gpio->outcnt )
    {
        log_warn("WARNNING: No GPIO pins configured\n");
        return 0;
    }
 
    /*  gpiod open chip */
    s_chip = gpiod_chip_open_by_name(RPI_GPIONAME);
    if( !s_chip )
    {
        log_error("gpiod open chip failure, maybe you need running as root\n");
        return -2;
    }
    log_info("gpiod initialise open chip ok\n");
 
    /*  gpiod request all output pins */
    for(i=0; i<gpio->outcnt; i++)
    {
        gpio->output[i].line = gpiod_chip_get_line(s_chip, gpio->output[i].pin);
        if( !gpio->output[i].line )
        {
            log_error("gpiod get line for '%s' pin[#%d] failure\n", gpio->output[i].name, gpio->output[i].pin );
            rv = -3;
            goto failed;
        }
 
        if( gpiod_line_request_output(gpio->output[i].line, gpio->output[i].name, !gpio->output[i].active_level) < 0 )
        {
            log_error("gpiod request '%s' pin[#%d] output failure: %s\n", gpio->output[i].name, gpio->output[i].pin, strerror(errno));
            rv = -3;
            goto failed;
        }
        else
        {
            log_info("gpiod request '%s' pin[#%d] output ok\n", gpio->output[i].name, gpio->output[i].pin);
        }
    }
 
    /*  gpiod request all input pins */
    for(i=0; i<gpio->incnt; i++)
    {
        gpio->input[i].line = gpiod_chip_get_line(s_chip, gpio->input[i].pin);
        if( !gpio->input[i].line )
        {
            log_error("gpiod get line for '%s' pin[#%d] failure\n", gpio->input[i].name, gpio->input[i].pin );
            rv = -4;
            goto failed;
        }
 
        if( gpio->input[i].active_level )
            rv = gpiod_line_request_rising_edge_events(gpio->input[i].line, gpio->input[i].name) ;
        else
            rv = gpiod_line_request_falling_edge_events(gpio->input[i].line, gpio->input[i].name) ;
 
        if( rv < 0 )
        {
            log_error("gpiod request '%s' pin[#%d] event edge [%s] failure: %s\n",
                    gpio->input[i].name, gpio->input[i].pin, gpio->input[i].active_level?"rising":"falling", strerror(errno));
            rv = -4;
            goto failed;
        }
        else
        {
            log_info("gpiod request '%s' pin[#%d] event edge [%s] ok\n",
                    gpio->input[i].name, gpio->input[i].pin, gpio->input[i].active_level?"rising":"falling");
        }
    }
 
    return 0;
 
failed:
    gpiod_chip_close(s_chip);
    return rv;
}
 
 
void gpio_term(void)
{
    int              i;
 
    log_info("start teriminated GPIO\n");
 
    if( !s_gpio->incnt && !s_gpio->outcnt )
    {
        return ;
    }
 
    for(i=0; i<s_gpio->outcnt; i++)
    {
        gpiod_line_release(s_gpio->output[i].line);
    }
 
    for(i=0; i<s_gpio->incnt; i++)
    {
        gpiod_line_release(s_gpio->input[i].line);
    }
 
    gpiod_chip_close(s_chip);
}
 
/* Turn light ON or OFF */
void turn_light(char *name, int status)
{
    int              i;
    int              found = 0;
    int              value;
 
    for( i=0; i<s_gpio->outcnt; i++ )
    {
        if( strstr(s_gpio->output[i].name, name) )
        {
            found = 1;
            break;
        }
    }
 
    if( !found )
    {
        log_error("GPIO output for '%s' pin not found\n", name);
        return ;
    }
 
    log_info("Turn %s light %s\n", name, status?"on":"off");
    value = status ? s_gpio->output[i].active_level : !s_gpio->output[i].active_level;
    gpiod_line_set_value(s_gpio->output[i].line, value);
 
    return ;
}
 
/* Return value: 0(LOW): Nobody detected, !0: indoor or hallway detected */
int infrared_detect(void)
{
    int                      i;
    int                      rv = 0;
    int                      result = 0;
    int                      num_lines = 0;
 
    struct gpiod_line_event  ev;
    struct gpiod_line_bulk   bulk, event_bulk;
    struct gpiod_line       *line;
 
    gpiod_line_bulk_init(&bulk);
 
    for(i=0; i<s_gpio->incnt; i++)
    {
        if( strstr(s_gpio->input[i].name, "infrared"))
        {
            gpiod_line_bulk_add(&bulk, s_gpio->input[i].line);
            num_lines ++;
        }
    }
 
    if( !num_lines )
    {
        log_error("No infrared detect gpiod lines found\n");
        return 0;
    }
 
    log_debug("infrared start detect event(blocked) now...\n");
    rv = gpiod_line_event_wait_bulk(&bulk, NULL, &event_bulk);
    if( rv <= 0 )
    {
        log_error("infrared gpiod line wait[%s] event failure!\n", s_gpio->input[i].name);
        return 0;
    }
 
    for (i=0; i<num_lines; i++)
    {
        line = gpiod_line_bulk_get_line(&event_bulk, 0);
 
        /* must call this function to  clear the event  */
        rv = gpiod_line_event_read(line, &ev);
        if( rv < 0)
        {
            log_error("gpiod_line_event_read get failure\n");
            break;
        }
 
        for(i=0; i<s_gpio->incnt; i++)
        {
            if(line == s_gpio->input[i].line)
            {
                if( s_gpio->input[i].active_level != gpiod_line_get_value(line) )
                {
                    log_debug("infrared '%s' detect get wrong power level\n", s_gpio->input[i].name);
                    continue;
                }
 
                if( strstr(s_gpio->input[i].name, "indoor") )
                {
                    log_debug("indoor infrared gpiod wait get event.\n", s_gpio->input[i].name);
                    result |= FLAG_INFRARED_INDOOR;
                }
                else if( strstr(s_gpio->input[i].name, "hallway") )
                {
                    log_debug("hallway infrared gpiod wait get event.\n", s_gpio->input[i].name);
                    result |= FLAG_INFRARED_HALLWAY;
                }
            }
        }
    }
 
    return result;
}