guowenxue
2023-07-10 5e9d03d507aad324a803eb8795e0eed6fb671761
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
/*********************************************************************************
 *      Copyright:  (C) 2019 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  hal.c
 *    Description:  This file is RPi HAL(Hardware Abstract Layer) initial 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 "hal.h"
 
#define RPI_GPIONAME        "gpiochip0"
 
static struct gpiod_chip    *s_chip; 
static struct gpiod_line    *s_light_lines[LIGHT_MAX]; /* relay GPIO lines */
static struct gpiod_line    *s_infrared_lines;  /* infrared GPIO lines */
static int                   s_light_cnt;
 
int hal_init(hal_ctx_t *ctx)
{
    int             i;
 
    if(!ctx)
    {
    log_err("Invalid input arguments\n");
    return -1;
    }
 
    /* current configure light counts */
    s_light_cnt = ctx->light_cnt;
 
    /* gpiod open chip */
    s_chip = gpiod_chip_open_by_name(RPI_GPIONAME);
    if( !s_chip )
    {
    log_err("gpiod open chip failure, maybe you need running as root\n");
    return -2;
    }
    log_nrml("gpiod initialise open chip ok\n");
 
    /* gpiod get gpio lines and request relay pin as output */
    for(i=0; i<ctx->light_cnt; i++)
    {
    s_light_lines[i] = gpiod_chip_get_line(s_chip, ctx->light_pins[i]);
    if( !s_light_lines[i] )
    {
        log_err("gpiod get line for pin[%d] failure\n", ctx->light_pins[i]);
        return -2;
    }
 
    gpiod_line_request_output(s_light_lines[i], "lightd", RELAY_INACTVLEVEL);
    }
    log_nrml("gpiod initialise request relay pins output ok\n");
 
 
    /* gpiod get gpio lines and request infrared pin as input */
    if( ctx->infrared_pin )
    {
    s_infrared_lines = gpiod_chip_get_line(s_chip, ctx->infrared_pin);
    gpiod_line_request_rising_edge_events(s_infrared_lines, "infrared");
    log_err("infrared GPIO input lines request for rising edge event ok\n");
    }
 
    if( tsl2561_init()< 0 )
    {
    log_err("LUX sensor TSL2561 initialise failure\n");
    return -2;
    }
 
 
    return 0;
}
 
 
void hal_term(hal_ctx_t *ctx)
{
    int             i;
 
    if(!ctx)
    {
    log_err("Invalid input arguments\n");
    return ;
    }
 
    for(i=0; i<ctx->light_cnt; i++)
    {
    gpiod_line_release(s_light_lines[i]);
    }
 
    gpiod_line_release(s_infrared_lines);
 
    gpiod_chip_close(s_chip);
 
    return ;
}
 
void turn_light(int which, int cmd)
{
    if( which >= s_light_cnt )
    {
    log_err("light[%d] not support in configure file\n", which);
    return ;
    }
 
    if( OFF == cmd )
    {
    gpiod_line_set_value(s_light_lines[which], RELAY_INACTVLEVEL);
    }
    else
    {
    gpiod_line_set_value(s_light_lines[which], RELAY_ACTVLEVEL);
    }
 
    return ;
}
 
 
/* 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;
}