-
Notifications
You must be signed in to change notification settings - Fork 0
/
door.c
76 lines (63 loc) · 1.56 KB
/
door.c
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
/**
*
* Requires the bcm2835 gpio library found at
* http://www.airspayce.com/mikem/bcm2835/index.html
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <bcm2835.h>
void on_high();
void on_low();
#define PIN_OUT RPI_V2_GPIO_P1_11
#define PIN_PIN RPI_V2_GPIO_P1_15
int run;
//Check counter runs from 0-255
//on each 0 the changed script is run regardless of status
//this means that its run every ~2 min
uint8_t counter;
uint8_t status; //Current status of door
#define SLEEP_TIME 500
int main(int argc, char** argv){
if (!bcm2835_init())
return 1;
//Set PIN_OUT to be output
bcm2835_gpio_fsel(PIN_OUT, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(PIN_OUT, HIGH);
// Set PIN_IN to be input with pulldown
bcm2835_gpio_fsel(PIN_IN, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_set_pud(PIN_IN, BCM2835_GPIO_PUD_DOWN);
run = 1;
counter = 0;
status = LOW;
while(run){
//Check if door is open or closed
uint8_t value = bcm2835_gpio_lev(PIN_IN);
if(value == HIGH)
on_high();
else
on_low();
//Sleep
counter++;
delay(SLEEP_TIME);
}
return 1;
}
/** Action to perform when input is high(Door closed) */
void on_high(){
if(status == LOW || counter == 0){
counter = 0;
status = HIGH;
//Call script, easier to add new separate functionality in script.
system("doorchanged 1");
}
}
/** Action to perform when input is low(Door open) */
void on_low(){
if(status == HIGH || counter == 0){
counter = 0;
status = LOW;
//Call script, easier to add new separate functionality in script.
system("doorchanged 0");
}
}