-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiscreenctrl.c
executable file
·172 lines (146 loc) · 3.44 KB
/
piscreenctrl.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***
* Program: piscreenctrl
* Usage: piscreenctrl
* Description: run as daemon, listening on GPIO pin (wiringPi numbering)
* on high signal the command is executed, Ctrl+C to stop program
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <wiringPi.h>
#include <pthread.h>
#define LOOP_DELAY 2000
// CONSTANTS
#define DEBUG 0
#define TIMESTAMP_LENGTH 27
#define PIR_PIN 1
#define SIGNAL 1
#define DISPLAY_OFF 0
#define DISPLAY_ON 1
#define DEFAULT_TIMER_SEC 180
const char *CMDS[] = {
"/home/pi/bin/display-off.sh",
"/home/pi/bin/display-on.sh"
};
// GLOBAL VARIABLES
char timestamp[TIMESTAMP_LENGTH];
int timer = DEFAULT_TIMER_SEC;
// DECLARATIONS
void printUsage(void);
int init(void);
void exInt0_ISR(void);
void display_off(void);
void display_on(void);
// DEFINITIONS
void printUsage() {
fprintf(stderr, "Usage: piscreenctrl\n"
" Program listens on GPIO [PIR_PIN] for HIGH signal and executes turns on/off display.\n"
);
exit(EXIT_FAILURE);
}
/*
* initialization routine
*/
int init(void) {
if (wiringPiSetup()<0) {
fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno));
exit(EXIT_FAILURE);
return 1;
}
wiringPiISR(PIR_PIN, SIGNAL, &exInt0_ISR);
if (DEBUG) {
createTimestamp(timestamp);
fprintf(stdout,"[%s] [piscreenctrl] init() pir=%d signal=HIGH)\n", timestamp, PIR_PIN);
fflush(stdout);
}
return 0;
}
/*
* countdown runs in own thread, counts timer down by 1 per sec
*/
void *countdown(void *threadid) {
long tid;
tid = (long)threadid;
if (DEBUG) {
createTimestamp(timestamp);
fprintf(stdout,"[%s] [piscreenctrl] countdown thread started threadid=%d\n", timestamp, tid);
}
while (1) {
delay(1000); // 1000ms
timer = timer - 1;
timer = timer<0 ? 0 : timer;
}
pthread_exit(NULL);
}
void display_off(void) {
createTimestamp(timestamp);
fprintf(stdout, "[%s] display_off()\n", timestamp);
fflush(stdout);
system(CMDS[DISPLAY_OFF]);
}
void display_on(void) {
createTimestamp(timestamp);
fprintf(stdout, "[%s] display_on()\n", timestamp);
fflush(stdout);
system(CMDS[DISPLAY_ON]);
}
/*
* GPIO interrupt on PIR motion sensor rising to high
*/
void exInt0_ISR(void) {
if (DEBUG) {
createTimestamp(timestamp);
fprintf(stdout, "[%s] exInt0_ISR(%d): Motion detected. Reseting timer to %dsec\n", timestamp, PIR_PIN, DEFAULT_TIMER_SEC);
fflush(stdout);
}
timer = DEFAULT_TIMER_SEC;
return;
}
int createTimestamp(char* buffer) {
int millisec;
struct tm* tm_info;
struct timeval tv;
char buffer2[TIMESTAMP_LENGTH];
gettimeofday(&tv, NULL);
millisec = lrint(tv.tv_usec/1000.0);
if (millisec>=1000) {
millisec -=1000;
tv.tv_sec++;
}
tm_info = localtime(&tv.tv_sec);
strftime(buffer, TIMESTAMP_LENGTH, "%Y-%m-%d %H:%M:%S", tm_info);
sprintf(buffer, "%s.%d", buffer, millisec);
}
int main(int argc, char* argv[]) {
if (argc!=1) {
printUsage();
exit(1);
return 1;
}
init();
pthread_t *thread;
long threadid;
int rc;
rc = pthread_create(&thread, NULL, countdown, (void *)threadid);
if (rc){
fprintf(stderr,"ERROR; return code from pthread_create() is %d\n", rc);
return(1);
exit(1);
}
while (1) {
delay(LOOP_DELAY);
if (DEBUG) fprintf(stdout, "%d ",timer);
if (DEBUG) fflush(stdout);
if (timer==0) {
display_off();
while (!timer) {
delay(1000);
}
display_on();
}
}
exit(EXIT_SUCCESS);
return 0;
}