guowenxue
2021-04-22 4ff5fe054d3e9db5edf1eb8e9114729b5f7918a3
update iotd for infrared detect
4 files modified
103 ■■■■ changed files
iotd/conf/conf.c 5 ●●●●● patch | view | raw | blame | history
iotd/hal/gpio.c 70 ●●●●● patch | view | raw | blame | history
iotd/hal/gpio.h 5 ●●●● patch | view | raw | blame | history
iotd/main.c 23 ●●●● patch | view | raw | blame | history
iotd/conf/conf.c
@@ -72,6 +72,11 @@
                if( 3 == rv) 
                {
                    log_nrml("parser GPIO input[%s] BCM[%d] active[%d]\n", gpio->input[cnt].name, gpio->input[cnt].pin, gpio->input[cnt].active_level);
                    if( strstr(gpio->input[cnt].name, "infrared") )
                    {
                        log_nrml("parser GPIO enable infrared detect\n");
                        gpio->infrared_enable = 1;
                    }
                    cnt++;
                    gpio->incnt = cnt;
                }
iotd/hal/gpio.c
@@ -22,6 +22,7 @@
int gpio_init(gpio_t *gpio)
{
    int                 i;
    int                 rv;
    gpio_info_t        *gpioinfo;
    s_gpio = gpio;
@@ -59,8 +60,14 @@
            return -2;
        }
        gpiod_line_request_output(gpio->output[i].line, gpio->output[i].name, !gpio->output[i].active_level);
        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);
        }
    }
@@ -75,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");
        }
    }
}
@@ -146,41 +158,55 @@
    return ;
}
#define MS_NANOSEC  (1*1000*1000)
/* 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;
    int                      rv = 0;
    int                      res = 0;
    struct gpiod_line_event  event;
    int                      event_type;
    struct timespec          ts = { 0, interval*MS_NANOSEC }; /* second and nanoseconds,  */
    for(i=0; i<s_gpio->incnt; i++)
    {
        if( strstr(s_gpio->input[i].name, "infrared"))
        {
            log_dbg("Start detect '%s' on pin[#%d]\n", s_gpio->input[i].name, s_gpio->input[i].pin);
            /* This function will block, must call it to setup */
            rv = gpiod_line_event_wait(s_gpio->input[i].line, NULL) ;
            rv = gpiod_line_event_wait(s_gpio->input[i].line, &ts);
            if( rv< 0 )
            {
                log_err("infrared gpiod line wait event failure!\n");
                //log_err("infrared gpiod line wait[%s] event failure!\n", s_gpio->input[i].name);
                return 0;
            }
            log_err("infrared gpiod line wait event rv=%d!\n", rv);
            /* This function will block, must read to clear the event  */
            if (gpiod_line_event_read(s_gpio->input[i].line, &event) < 0)
            else if( rv == 0 )
            {
                log_err("gpiod line event read failure!\n");
                return 0;
                //log_dbg("infrared gpiod line[%s] wait event timeout\n", s_gpio->input[i].name);
                continue;
            }
            if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
                return 1;
            else
                return 0;
            /* 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;
        }
    }
        }
    }
    return res;
}
iotd/hal/gpio.h
@@ -36,6 +36,7 @@
    gpio_info_t          input[GPIO_MAXIN];   /* GPIO input pins */
    int                  incnt;               /* GPIO input numbers */
    int                  infrared_enable;     /* infrared enable or not */
} gpio_t; 
extern int gpio_init(gpio_t *gpio);
@@ -45,7 +46,9 @@
extern void gpio_out(char *name, char *cmd);
/* Return value: 0(LOW): Nobody detected, !0: Which infrared detect incoming */
extern int infrared_detect(void);
#define FLAG_INFRARED_INDOOR          (1<<0)
#define FLAG_INFRARED_HALLWAY         (1<<1)
extern int infrared_detect(int interval);
#endif   /* ----- #ifndef _GPIO_H_  ----- */
iotd/main.c
@@ -176,15 +176,21 @@
    iotd_ctx_t            *ctx = (iotd_ctx_t *)args;
    hal_ctx_t             *hal_ctx;
    float                  lux = 0.0;
    int                    rv;
    hal_ctx = &ctx->hal_ctx;
    log_dbg("infrared configured [%d], lux configured [%d]\n", hal_ctx->gpio.infrared_enable, hal_ctx->lux_enable);
    while( ! g_signal.stop )
    {
        if( hal_ctx->gpio.infrared_enable )
    { 
        if( hal_ctx->lux_enable )
        {
            lux = tsl2561_get_lux(); 
            log_dbg("TSL2561 get Lux[%.3f].\n", lux);
                log_dbg("TSL2561 get Lux[%.3f] Treshold[%.3f].\n", lux, hal_ctx->lux_threshold);
            if( lux > hal_ctx->lux_threshold )
            {
@@ -194,13 +200,20 @@
            }
        }
        log_nrml("start infrared monitor detect...\n");
        if( infrared_detect() )
            rv = infrared_detect(250);
            if( rv & FLAG_INFRARED_INDOOR )
        {
            log_nrml("Someone incoming detected by infrared\n");
                log_nrml("Someone incoming detected by indoor infrared\n");
        }
        sleep(1);
            if( rv & FLAG_INFRARED_HALLWAY )
            {
                log_nrml("Someone incoming detected by hallway infrared\n");
            }
        }
        msleep(500);
    }
}