/*********************************************************************************
|
* Copyright: (C) 2019 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: infrared.c
|
* Description: This file
|
*
|
* pi@raspberrypi:~ $ gpio readall #show BCM and wPi pinmap
|
*
|
* PIN #PIN BCM wPi
|
* VCC(left) ---- 2 ---- 5v ---- 5v
|
* I/O(mid) ---- 32 ---- 12 ---- 26
|
* GND(right) ---- 34 ---- 0v ---- 0v
|
*
|
* Version: 1.0.0(30/01/19)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "30/01/19 03:37:16"
|
*
|
********************************************************************************/
|
|
#include <stdio.h>
|
#include <unistd.h>
|
|
|
#ifdef CONFIG_USE_WIRINGPI
|
|
#include <wiringPi.h>
|
|
#define INFRARED_PIN 26
|
|
int main (int argc, char **argv)
|
{
|
wiringPiSetup();
|
pinMode(INFRARED_PIN, INPUT);
|
|
while(1)
|
{
|
printf("Infrared monitor: %s\n", digitalRead(INFRARED_PIN)? "Someone is closing!":"No one nearby!");
|
sleep(1);
|
}
|
|
|
return 0;
|
}
|
|
#else
|
|
#include <gpiod.h>
|
|
#define INFRARED_PIN 12
|
|
int main (int argc, char **argv)
|
{
|
struct gpiod_chip* chip;
|
struct gpiod_line* line;
|
int rv = 0;
|
struct gpiod_line_event event;
|
|
chip = gpiod_chip_open_by_name("gpiochip0");
|
if( !chip )
|
{
|
printf("gpiod open chip failure, maybe you need running as root\n");
|
return 1;
|
}
|
|
line = gpiod_chip_get_line(chip, INFRARED_PIN);
|
if( !line )
|
{
|
printf("gpiod get line[%d] failure\n", INFRARED_PIN);
|
rv = 2;
|
goto cleanup;
|
}
|
|
|
if( gpiod_line_request_rising_edge_events(line, "infrared") < 0 )
|
{
|
printf("gpiod request rising edge event failure\n");
|
rv = 3;
|
goto cleanup;
|
}
|
|
while(1)
|
{
|
/* This function will block, must call it to setup */
|
if( gpiod_line_event_wait(line, NULL) < 0 )
|
{
|
printf("gpiod line wait event failure!\n");
|
}
|
|
/* This function will block, must read to clear the event */
|
if (gpiod_line_event_read(line, &event) < 0)
|
{
|
printf("gpiod line wait event failure!\n");
|
}
|
|
if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
|
{
|
printf("Infrared monitor: Someone is closing!\n");
|
}
|
}
|
|
cleanup:
|
gpiod_chip_close(chip);
|
return rv;
|
}
|
|
|
#endif
|