From 7a0488541f68d1a9eb712a4eb5c6c36da150bc4a Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Fri, 23 Apr 2021 22:43:13 +0800 Subject: [PATCH] update iotd and test infared ok --- iotd/hal/gpio.c | 165 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 135 insertions(+), 30 deletions(-) diff --git a/iotd/hal/gpio.c b/iotd/hal/gpio.c index bd050b1..73340a6 100644 --- a/iotd/hal/gpio.c +++ b/iotd/hal/gpio.c @@ -12,6 +12,7 @@ ********************************************************************************/ #include "lylib/logger.h" +#include "lylib/util_proc.h" #include "gpio.h" #define RPI_GPIONAME "gpiochip0" @@ -22,11 +23,10 @@ int gpio_init(gpio_t *gpio) { int i; + int rv; gpio_info_t *gpioinfo; - s_gpio = gpio; - if( !gpio ) { @@ -40,7 +40,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 +61,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 +83,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 +127,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 +147,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); } @@ -150,30 +160,125 @@ } -#if 0 -/* Return value: 1(HIGH): Sombody detected 0(LOW): Nobody detected */ -int infrared_detect(void) +void *light_on_worker(void *arg) { - struct gpiod_line_event event; + int i; + int found = 0; + char *name = (char *)arg; + gpio_info_t *gpiout; - /* This function will block, must call it to setup */ - if( gpiod_line_event_wait(s_infrared_lines, NULL) < 0 ) + if( !name ) { - //log_err("infrared gpiod line wait event failure!\n"); - return 0; + log_err("Invalid input arugment\n"); + return NULL; } - /* 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( s_gpio->light_intval <= 0 ) + { + log_err("Invalid light_intval value[%d] in configure file\n", s_gpio->light_intval); + return NULL; } - if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE) - return 1; - else - return 0; + for(i=0; i<s_gpio->outcnt; i++) + { + if( strstr(s_gpio->output[i].name, name) ) + { + gpiout = &(s_gpio->output[i]); + found = 1; + } + } + + if( !found ) + { + log_err("Light '%s' not found\n", name); + return NULL; + } + + log_nrml("Trun on %s for [%d] seconds\n", gpiout->name, s_gpio->light_intval); + + gpio_out(gpiout->name, "on"); + sleep(s_gpio->light_intval); + gpio_out(gpiout->name, "off"); + + return NULL; } -#endif +/* Return value: 0(LOW): Nobody detected, !0: indoor or hallway detected */ +int infrared_detect(void) +{ + int i; + int rv = 0; + int res = 0; + + struct gpiod_line_event ev; + struct gpiod_line_bulk bulk, event_bulk; + struct gpiod_line *line; + + int num_lines = 0; + + + 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_err("No infrared detect gpiod lines found\n"); + return 0; + } + + log_dbg("infrared start detect event now...\n"); + rv = gpiod_line_event_wait_bulk(&bulk, NULL, &event_bulk); + if( rv <= 0 ) + { + log_err("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_err("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_dbg("infrared '%s' detect get wrong power level\n", s_gpio->input[i].name); + continue; + } + + if( strstr(s_gpio->input[i].name, "indoor") ) + { + log_dbg("indoor infrared gpiod wait get event.\n", s_gpio->input[i].name); + res |= FLAG_INFRARED_INDOOR; + } + else if( strstr(s_gpio->input[i].name, "hallway") ) + { + log_dbg("hallway infrared gpiod wait get event.\n", s_gpio->input[i].name); + res |= FLAG_INFRARED_HALLWAY; + } + } + } + } + + return res; +} + + -- Gitblit v1.9.1