guowenxue
2021-04-20 917354ea70f7c159643f193f1e4e315d92692830
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
/*********************************************************************************
 *      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 "lylib/logger.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)
{
    if( !gpio )
    {
        log_err("Invalid input arguments $gpio\n");
        return -1;
    }
 
    s_gpio = gpio;
}
 
void gpio_term(void)
{
    return ;
}
 
void gpio_out(char *name, int cmd)
{
    int              i;
    int              found = 0;
 
    for( i=0; i<s_gpio->outcnt; i++ )
    {
        if( !strncasecmp(s_gpio->output[i].name, name, strlen(name)))
        {
            found = 1;
            break;
        }
    }
 
    if( !found )
    {
        log_err("GPIO output for [%s] pin not found\n", name);
        return ;
    }
 
    if( OFF == cmd )
    {
        gpiod_line_set_value(s_gpio->output[i].lines, s_gpio->output[i].active_level);
    }
    else
    {
        gpiod_line_set_value(s_gpio->output[i].lines, !s_gpio->output[i].active_level);
    }
 
    return ;
}
 
 
#if 0
/* Return value: 1(HIGH): Sombody detected  0(LOW): Nobody detected */
int infrared_detect(void)
{
    struct gpiod_line_event  event;
 
    /* This function will block, must call it to setup */
    if( gpiod_line_event_wait(s_infrared_lines, NULL) < 0 )
    {
        //log_err("infrared gpiod line wait event failure!\n");
        return 0;
    }
 
    /* This function will block, must read to clear the event  */
    if (gpiod_line_event_read(s_infrared_lines, &event) < 0)
    {
        log_err("gpiod line event read failure!\n");
        return 0;
    }
 
    if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
        return 1;
    else
        return 0;
}
 
#endif