guowenxue
2021-04-23 7a0488541f68d1a9eb712a4eb5c6c36da150bc4a
update iotd and test infared ok
5 files modified
145 ■■■■ changed files
iotd/etc/iotd.conf 7 ●●●●● patch | view | raw | blame | history
iotd/hal/gpio.c 110 ●●●● patch | view | raw | blame | history
iotd/hal/gpio.h 6 ●●●● patch | view | raw | blame | history
iotd/lylib/util_time.h 9 ●●●●● patch | view | raw | blame | history
iotd/main.c 13 ●●●●● patch | view | raw | blame | history
iotd/etc/iotd.conf
@@ -22,8 +22,7 @@
[hardware]
# LED或继电器等GPIO输出控制, 格式: {名称:BCM编码:控制电平}
gpio_outpin={light_indoor:6:1},{light_livroomL:13:1},{light_livroomR:19:1},{light_hallway:26:1}
#gpio_outpin={led_red:6:1},{led_green:13:1},{led_blue:19:1}
gpio_outpin={light_indoor:6:1},{light_hallway:26:1}
# 红外探测到人后,继电器控制灯亮的时长
light_intval=15
@@ -65,13 +64,13 @@
[subsciber]
subTopic="$Sys/Studio/iotd/Downlink"
subTopic="$Sys/Studio/Downlink/iotd"
subQos=0
[publisher]
pubTopic="$Sys/Studio/iotd/Uplink"
pubTopic="$Sys/Studio/Uplink/"
pubQos=0
# Publisher上报传感器数据的周期,单位是秒
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 )
            gpiod_line_bulk_add(&bulk, s_gpio->input[i].line);
            num_lines ++;
        }
    }
    if( !num_lines )
            {
                //log_err("infrared gpiod line wait[%s] event failure!\n", s_gpio->input[i].name);
        log_err("No infrared detect gpiod lines found\n");
                return 0;
            }
            else if( rv == 0 )
    log_dbg("infrared start detect event now...\n");
    rv = gpiod_line_event_wait_bulk(&bulk, NULL, &event_bulk);
    if( rv <= 0 )
            {
                //log_dbg("infrared gpiod line[%s] wait event timeout\n", s_gpio->input[i].name);
        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;
            }
            /* 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") )
                {
                    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; 
}
iotd/hal/gpio.h
@@ -45,10 +45,14 @@
/* turn which light on/off */
extern void gpio_out(char *name, char *cmd);
/*thread work body to turn light $name on for some seconds */
void *light_on_worker(void *arg);
/* Return value: 0(LOW): Nobody detected, !0: Which infrared detect incoming */
#define FLAG_INFRARED_INDOOR          (1<<0)
#define FLAG_INFRARED_HALLWAY         (1<<1)
extern int infrared_detect(int interval);
extern int infrared_detect(void);
#endif   /* ----- #ifndef _GPIO_H_  ----- */
iotd/lylib/util_time.h
@@ -25,6 +25,15 @@
#include <sys/stat.h>
#include <sys/time.h>
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
#define container_of(ptr, type, member) ({      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})
typedef struct date_time_s
{
    int year;
iotd/main.c
@@ -194,26 +194,31 @@
                if( lux > hal_ctx->lux_threshold )
                {
                    log_nrml("Lux[%.3f] > Treshold[%.3f], don't need light on.\n", lux, hal_ctx->lux_threshold);
                    log_nrml("Lux[%.3f] > Treshold[%.3f], don't need light on and sleep now.\n", lux, hal_ctx->lux_threshold);
                    if( hal_ctx->gpio.light_intval > 0)
                        sleep( hal_ctx->gpio.light_intval );
                    else
                    sleep(30);
                    continue;
                }
            }
            rv = infrared_detect(250);
            rv = infrared_detect();
            if( rv & FLAG_INFRARED_INDOOR ) 
            {
                log_nrml("Someone incoming detected by indoor infrared\n");
                thread_start(NULL, light_on_worker, "indoor" );
            }
            if( rv & FLAG_INFRARED_HALLWAY ) 
            {
                log_nrml("Someone incoming detected by hallway infrared\n");
                thread_start(NULL, light_on_worker, "hallway" );
            }
        }
        msleep(500);
        msleep(100);
    }
}