From 4ff5fe054d3e9db5edf1eb8e9114729b5f7918a3 Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Thu, 22 Apr 2021 03:18:37 +0800 Subject: [PATCH] update iotd for infrared detect --- iotd/hal/gpio.c | 95 ++++++++++++++++++++++++++++++++--------------- 1 files changed, 64 insertions(+), 31 deletions(-) diff --git a/iotd/hal/gpio.c b/iotd/hal/gpio.c index bd050b1..72dd269 100644 --- a/iotd/hal/gpio.c +++ b/iotd/hal/gpio.c @@ -22,11 +22,10 @@ int gpio_init(gpio_t *gpio) { int i; + int rv; gpio_info_t *gpioinfo; - s_gpio = gpio; - if( !gpio ) { @@ -40,7 +39,6 @@ log_warn("WARNNING: No GPIO pins configured\n"); return 0; } - /* gpiod open chip */ s_chip = gpiod_chip_open_by_name(RPI_GPIONAME); @@ -62,8 +60,14 @@ return -2; } - gpiod_line_request_output(gpio->output[i].line, gpio->output[i].name, !gpio->output[i].active_level); - log_nrml("gpiod request '%s' pin[#%d] output ok\n", gpio->output[i].name, gpio->output[i].pin); + if( gpiod_line_request_output(gpio->output[i].line, gpio->output[i].name, !gpio->output[i].active_level) < 0 ) + { + log_err("gpiod request '%s' pin[#%d] output failure: %s\n", gpio->output[i].name, gpio->output[i].pin, strerror(errno)); + } + else + { + log_nrml("gpiod request '%s' pin[#%d] output ok\n", gpio->output[i].name, gpio->output[i].pin); + } } @@ -78,14 +82,19 @@ } if( gpio->output[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 ) { - gpiod_line_request_rising_edge_events(gpio->input[i].line, gpio->input[i].name); - log_nrml("gpiod request '%s' pin[#%d] for rising edge event ok\n", gpio->input[i].name, gpio->input[i].pin); + log_err("gpiod request '%s' pin[#%d] event edge [%s] failure: %s\n", + gpio->input[i].name, gpio->input[i].pin, gpio->output[i].active_level?"rising":"falling", strerror(errno)); } else { - gpiod_line_request_falling_edge_events(gpio->input[i].line, gpio->input[i].name); - log_nrml("gpiod request '%s' pin[#%d] for falling edge event ok\n", gpio->input[i].name, gpio->input[i].pin); + log_nrml("gpiod request '%s' pin[#%d] event edge [%s] ok\n", + gpio->input[i].name, gpio->input[i].pin, gpio->output[i].active_level?"rising":"falling"); } } } @@ -117,7 +126,7 @@ gpiod_chip_close(s_chip); } -void gpio_out(char *name, int cmd) +void gpio_out(char *name, char *cmd) { int i; int found = 0; @@ -137,11 +146,11 @@ return ; } - if( OFF == cmd ) + if( strstr(cmd, "on") ) { gpiod_line_set_value(s_gpio->output[i].line, s_gpio->output[i].active_level); } - else + else if( strstr(cmd, "off") ) { gpiod_line_set_value(s_gpio->output[i].line, !s_gpio->output[i].active_level); } @@ -149,31 +158,55 @@ return ; } +#define MS_NANOSEC (1*1000*1000) -#if 0 -/* Return value: 1(HIGH): Sombody detected 0(LOW): Nobody detected */ -int infrared_detect(void) +/* Return value: 0(LOW): Nobody detected, !0: indoor or hallway detected */ +int infrared_detect(int interval) { + int i; + int rv = 0; + int res = 0; struct gpiod_line_event event; + int event_type; + struct timespec ts = { 0, interval*MS_NANOSEC }; /* second and nanoseconds, */ - /* This function will block, must call it to setup */ - if( gpiod_line_event_wait(s_infrared_lines, NULL) < 0 ) + for(i=0; i<s_gpio->incnt; i++) { - //log_err("infrared gpiod line wait event failure!\n"); - return 0; + if( strstr(s_gpio->input[i].name, "infrared")) + { + /* This function will block, must call it to setup */ + rv = gpiod_line_event_wait(s_gpio->input[i].line, &ts); + if( rv < 0 ) + { + //log_err("infrared gpiod line wait[%s] event failure!\n", s_gpio->input[i].name); + return 0; + } + else if( rv == 0 ) + { + //log_dbg("infrared gpiod line[%s] wait event timeout\n", s_gpio->input[i].name); + continue; + } + + /* Must read to clear the event */ + rv = gpiod_line_event_read(s_gpio->input[i].line, &event); + + event_type = s_gpio->input[i].active_level? GPIOD_LINE_EVENT_RISING_EDGE : GPIOD_LINE_EVENT_FALLING_EDGE; + + if ( event.event_type == event_type ) + { + if( strstr(s_gpio->input[i].name, "indoor") ) + { + res |= FLAG_INFRARED_INDOOR; + } + else if( strstr(s_gpio->input[i].name, "hallway") ) + { + res |= FLAG_INFRARED_HALLWAY; + } + } + + } } - /* 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; + return res; } -#endif -- Gitblit v1.9.1