/*********************************************************************************
|
* Copyright: (C) 2019 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: gpio.c
|
* Description: This file is GPIO input/output functions
|
*
|
* Version: 1.0.0(2019年06月24日)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "2019年06月24日 23时46分47秒"
|
*
|
********************************************************************************/
|
|
#include "lylib/logger.h"
|
#include "gpio.h"
|
|
#define RPI_GPIONAME "gpiochip0"
|
|
static struct gpiod_chip *s_chip;
|
static gpio_t *s_gpio = NULL;
|
|
int gpio_init(gpio_t *gpio)
|
{
|
if( !gpio )
|
{
|
log_err("Invalid input arguments $gpio\n");
|
return -1;
|
}
|
|
s_gpio = gpio;
|
}
|
|
void gpio_term(void)
|
{
|
return ;
|
}
|
|
void gpio_out(char *name, int cmd)
|
{
|
int i;
|
int found = 0;
|
|
for( i=0; i<s_gpio->outcnt; i++ )
|
{
|
if( !strncasecmp(s_gpio->output[i].name, name, strlen(name)))
|
{
|
found = 1;
|
break;
|
}
|
}
|
|
if( !found )
|
{
|
log_err("GPIO output for [%s] pin not found\n", name);
|
return ;
|
}
|
|
if( OFF == cmd )
|
{
|
gpiod_line_set_value(s_gpio->output[i].lines, s_gpio->output[i].active_level);
|
}
|
else
|
{
|
gpiod_line_set_value(s_gpio->output[i].lines, !s_gpio->output[i].active_level);
|
}
|
|
return ;
|
}
|
|
|
#if 0
|
/* Return value: 1(HIGH): Sombody detected 0(LOW): Nobody detected */
|
int infrared_detect(void)
|
{
|
struct gpiod_line_event event;
|
|
/* This function will block, must call it to setup */
|
if( gpiod_line_event_wait(s_infrared_lines, NULL) < 0 )
|
{
|
//log_err("infrared gpiod line wait event failure!\n");
|
return 0;
|
}
|
|
/* 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;
|
}
|
|
#endif
|