-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfartd.c
253 lines (200 loc) · 5.74 KB
/
fartd.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*-
* Copyright (c) 2016, 2017, Dennis Koegel
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* for reference,
* see https://www.raspberrypi.org/learning/fart-detector/worksheet/
* and README.md
*/
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <libgpio.h>
const int sleep_calibrate = 0;
const int sleep_main = 500000;
const char *statusfile_path = "/var/run/fartd_metrics";
int statusfile_enabled = 1;
#define LEVEL_GREEN 6
#define LEVEL_YELLOW 10
#define LEVEL_RED 14
#define LEVEL_UNKNOWN 16
const int resistorpin[4] = /* R1, R2, R3, R4 */ {17, 18, 22, 23};
const int detectpin = 4;
const int ledred = 21;
const int ledyellow = 20;
const int ledgreen = 16;
gpio_handle_t gpio;
char *datestr;
time_t now;
int is_daemon = 1;
void dacset (int dacval) {
int i;
for (i = 0; i < 4; i++) {
if (dacval & (1 << i)) {
/* mode OUTPUT: enabled, "siphon" current */
gpio_pin_output(gpio, resistorpin[i]);
gpio_pin_low(gpio, resistorpin[i]);
} else {
/* mode INPUT: disabled */
gpio_pin_input(gpio, resistorpin[i]);
}
}
}
int calibrate() {
int i;
gpio_value_t val;
/*
* 'i' can go to 16, because it could be the case that
* even with all resistors activated, it's STILL not
* enough to bring the sensor voltage below the detection
* threshold (to bring detectpin to LOW)
*/
for (i = 0; i<16; i++) {
if (!is_daemon)
printf(" %2d", i);
dacset(i);
if (sleep_calibrate)
usleep(sleep_calibrate);
val = gpio_pin_get(gpio, detectpin);
if (val == GPIO_PIN_LOW) {
if (!is_daemon)
printf(".\n");
return i;
}
}
if (!is_daemon)
printf(" OMGWTF.\n");
return 16;
}
void statusfile (int air, const char *airstr) {
FILE *f;
if (!statusfile_enabled)
return;
f = fopen(statusfile_path, "w");
if (f == NULL)
err(EX_CANTCREAT, "Opening statusfile %s", statusfile_path);
/*
* we don't handle the case of (air == LEVEL_UNKNOWN) here, because for
* graphing purposes, LEVEL_UNKNOWN (16) still is more useful than NaN
*/
fprintf(f, "air_quality_value %d\n", air);
fclose(f);
}
void setled(int air) {
gpio_pin_set(gpio, ledgreen, (air >= LEVEL_GREEN ? GPIO_PIN_HIGH : GPIO_PIN_LOW));
gpio_pin_set(gpio, ledyellow, (air >= LEVEL_YELLOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW));
/* red: special case: blinking for UNKNOWN is handled in main */
if (air >= LEVEL_RED)
gpio_pin_high(gpio, ledred);
else if (air < LEVEL_RED)
gpio_pin_low(gpio, ledred);
}
char *airtostr(int air) {
char *ret;
if (air == LEVEL_UNKNOWN)
asprintf(&ret, "<beyond measurement>");
else if (air >= LEVEL_RED)
asprintf(&ret, "%d (RED)", air);
else if (air >= LEVEL_YELLOW)
asprintf(&ret, "%d (yellow)", air);
else if (air >= LEVEL_GREEN)
asprintf(&ret, "%d (green)", air);
else
asprintf(&ret, "%d (-)", air);
return ret;
}
int main (int argc, char **argv) {
int air;
char *airstr;
int prevair = 0;
char *prevairstr;
if (argc == 2 && strcmp(argv[1], "-d") == 0)
is_daemon = 0;
gpio = gpio_open(0);
if (gpio == GPIO_INVALID_HANDLE)
err(69, "gpio_open() failed");
gpio_pin_input(gpio, detectpin);
gpio_pin_output(gpio, ledgreen);
gpio_pin_output(gpio, ledyellow);
gpio_pin_output(gpio, ledred);
if (!is_daemon)
setbuf(stdout, NULL);
else {
switch (fork()) {
case -1: err(69, "fork()");
case 0: break;
default: exit(0);
}
openlog("fartd", LOG_NDELAY, LOG_DAEMON);
/*
* if daemon, turn on all LEDs on at startup
* for 2 * main cycle time, to say hello
*/
setled(15);
usleep(sleep_main * 2);
}
if (!is_daemon)
printf("initial value: ");
prevair = air = calibrate();
statusfile(air, airstr);
if (is_daemon) {
airstr = airtostr(air);
syslog(LOG_NOTICE, "Starting -- initial air quality: %s", airstr);
free(airstr);
}
setled(air);
for (;;) {
usleep(sleep_main);
if (!is_daemon) {
time(&now);
datestr = ctime(&now);
datestr[strlen(datestr) - 1] = '\0';
printf("%s: ", datestr);
}
air = calibrate();
/* red alert: special case: blink for maximum value */
if (air == LEVEL_UNKNOWN)
gpio_pin_toggle(gpio, ledred);
if (prevair == air)
/* value unchanged - do nothing */
continue;
airstr = airtostr(air);
prevairstr = airtostr(prevair);
syslog(LOG_NOTICE, "Air quality changed: %s -> %s",
prevairstr, airstr);
statusfile(air, airstr);
free(airstr);
free(prevairstr);
setled(air);
prevair = air;
}
return 0;
}