guowenxue
2021-04-23 7a0488541f68d1a9eb712a4eb5c6c36da150bc4a
iotd/hal/gpio.c
@@ -12,6 +12,7 @@
 ********************************************************************************/
#include "lylib/logger.h"
#include "lylib/util_proc.h"
#include "gpio.h"
#define RPI_GPIONAME        "gpiochip0"
@@ -158,55 +159,126 @@
    return ;
}
#define MS_NANOSEC  (1*1000*1000)
void *light_on_worker(void *arg)
{
    int                      i;
    int                      found = 0;
    char                    *name = (char *)arg;
    gpio_info_t             *gpiout;
    if( !name )
    {
        log_err("Invalid input arugment\n");
        return NULL;
    }
    if( s_gpio->light_intval <= 0 )
    {
        log_err("Invalid light_intval value[%d] in configure file\n", s_gpio->light_intval);
        return NULL;
    }
    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;
}
/* Return value: 0(LOW): Nobody detected, !0: indoor or hallway detected */
int infrared_detect(int interval)
int infrared_detect(void)
{
    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,  */
    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"))
        {
            /* 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;
            }
            gpiod_line_bulk_add(&bulk, s_gpio->input[i].line);
            num_lines ++;
        }
    }
            /* Must read to clear the event */
            rv = gpiod_line_event_read(s_gpio->input[i].line, &event);
    if( !num_lines )
    {
        log_err("No infrared detect gpiod lines found\n");
        return 0;
    }
            event_type = s_gpio->input[i].active_level? GPIOD_LINE_EVENT_RISING_EDGE : GPIOD_LINE_EVENT_FALLING_EDGE;
    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;
    }
            if ( event.event_type == event_type )
    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;
    return res;
}