guowenxue
2022-04-09 e4a3dc95bdbb1371a7104fc9cf9c850bcdd3f883
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*********************************************************************************
 *      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(indoor) ----  16  ----   23     ----   4
 *        I/O(hallway) ----  18  ----   24     ----   5
 *          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>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
 
//#define CONFIG_USE_WIRINGPI
 
#ifdef CONFIG_USE_WIRINGPI
 
#include <wiringPi.h>
 
int main (int argc, char **argv)
{
    int           wPiPin;
 
    if( argc != 2 )
    {
        printf("%s [wPi PinNum]\n", argv[0]);
        printf("Exampe indoor : %s 4\n", argv[0]);
        printf("Exampe hallway: %s 5\n", argv[0]);
        return 0;
    }
 
    wPiPin = atoi(argv[1]);
    printf("Start infrared detect on wPi Pin[#%d]\n", wPiPin);
 
    wiringPiSetup();
    pinMode(wPiPin, INPUT);
 
    while(1)
    {
        printf("Infrared monitor: %s\n", digitalRead(wPiPin)? "Someone is closing!":"No one nearby!");
        sleep(1);
    }
 
    return 0;
}
 
#else
 
#include <gpiod.h>
 
int main (int argc, char **argv)
{
    struct gpiod_chip*       chip;
    struct gpiod_line*       line;
    int                      rv = 0;
    struct gpiod_line_event  event;
    int                      bcmPin;
 
    if( argc != 2 )
    {
        printf("%s [BCM PinNum]\n", argv[0]);
        printf("Exampe indoor : %s 23\n", argv[0]);
        printf("Exampe hallway: %s 24\n", argv[0]);
        return 0;
    }
 
    bcmPin = atoi(argv[1]);
    printf("Start infrared detect on BCM Pin[#%d]\n", bcmPin);
 
    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, bcmPin);
    if( !line )
    {
        printf("gpiod get line[%d] failure\n", bcmPin);
        rv = 2;
        goto cleanup;
    }
 
    if( gpiod_line_request_input(line, "infrared") < 0 )
    {
        printf("gpiod request input [#%d] failure: %s\n", bcmPin, strerror(errno) );
        rv = 3;
        goto cleanup;
    }
 
    while(1)
    {
        /* This function will block, must call it to setup */
        printf("Infrared monitor: %s\n", gpiod_line_get_value(line) ? "Someone is closing!":"No one nearby!");
        sleep(1);
    }
 
cleanup:
    gpiod_chip_close(chip);
    return rv;
}
 
 
#endif