12
|
1 |
#include <stdio.h> |
|
2 |
#include <stdlib.h> |
|
3 |
#include <math.h> |
|
4 |
|
|
5 |
#include <sys/types.h> |
|
6 |
#include <sys/stat.h> |
|
7 |
#include <fcntl.h> |
|
8 |
#include <assert.h> |
|
9 |
#include <string.h> |
|
10 |
#include <limits.h> |
|
11 |
#include <unistd.h> |
|
12 |
#include <errno.h> |
|
13 |
|
|
14 |
#include "config.h" |
|
15 |
|
|
16 |
|
|
17 |
int catint(char *name) { |
|
18 |
FILE* f = fopen(name, "r"); |
|
19 |
int i = 0; |
|
20 |
if (fscanf(f, "%d", &i) != 1) |
|
21 |
i = -1; |
|
22 |
fclose(f); |
|
23 |
return i; |
|
24 |
} |
|
25 |
|
|
26 |
char *color_stop; |
|
27 |
char *color_red; |
|
28 |
char *color_orange; |
|
29 |
char *color_green; |
|
30 |
char *color_none; |
|
31 |
|
|
32 |
char *color(int low,int mid,int rev,int value) { |
|
33 |
if (value>mid) { |
|
34 |
return rev ? color_red : color_green; |
|
35 |
} else if (value>low) { |
|
36 |
return color_orange; |
|
37 |
} |
|
38 |
return rev ? color_green : color_red; |
|
39 |
} |
|
40 |
|
|
41 |
int main(int argc, char *argv[]) { |
|
42 |
if (argc<=2) { |
|
43 |
fprintf(stderr, "Usage: %s <term|pango> <sensor1> [sensor2] ...\n", argv[0]); |
|
44 |
fflush(stderr); |
|
45 |
return 1; |
|
46 |
} |
|
47 |
|
|
48 |
char *color; |
|
49 |
if (!strcmp(argv[1],"term")) { |
|
50 |
color_stop = "\x1b[0m"; |
|
51 |
color_red = "\x1b[1;31m"; |
|
52 |
color_green = "\x1b[1;32m"; |
|
53 |
color_orange = "\x1b[1;33m"; |
|
54 |
color_none = color_stop; |
|
55 |
} else if (!strcmp(argv[1],"pango")) { |
|
56 |
color_stop = "</span>"; |
|
57 |
color_red = "<span color='#f33'>"; |
|
58 |
color_green = "<span color='#0f0'>"; |
|
59 |
color_orange = "<span color='#fa0'>"; |
|
60 |
color_none = "<span>"; |
|
61 |
} else { |
|
62 |
color_stop = color_red = color_green = color_orange = color_none = ""; |
|
63 |
} |
|
64 |
|
|
65 |
int sleeptime = UPDATE_PERIOD; |
|
66 |
int alertflash = 1; |
|
67 |
while (1) { |
|
68 |
int i; |
|
69 |
int maxtemp = 0; |
|
70 |
for (i=2;i<argc;i++) { |
|
71 |
int temp = catint(argv[i]); |
|
72 |
if (temp > maxtemp) |
|
73 |
maxtemp = temp; |
|
74 |
} |
|
75 |
|
|
76 |
float ctemp = maxtemp/1000; |
|
77 |
|
|
78 |
if (ctemp <= GREEN_VALUE) |
|
79 |
color = color_green; |
|
80 |
else if (ctemp >= RED_VALUE) |
|
81 |
color = color_red; |
|
82 |
else if (ctemp >= ORANGE_VALUE) |
|
83 |
color = color_orange; |
|
84 |
else |
|
85 |
color = color_none; |
|
86 |
|
|
87 |
#ifdef FLASH_AFTER |
|
88 |
if (ctemp >= FLASH_AFTER) { |
|
89 |
sleeptime = 1; |
|
90 |
alertflash = !alertflash; |
|
91 |
} else { |
|
92 |
sleeptime = UPDATE_PERIOD; |
|
93 |
alertflash = 1; |
|
94 |
} |
|
95 |
#endif |
|
96 |
if (alertflash) { |
|
97 |
printf("%s%2.1f%s\n",color, ctemp, color_stop); |
|
98 |
} else { |
|
99 |
int len = snprintf(NULL, 0, "%2.1f", ctemp); |
|
100 |
for (i=0; i<len; i++) |
|
101 |
printf(" "); |
|
102 |
printf("\n"); |
|
103 |
} |
|
104 |
fflush(stdout); |
|
105 |
sleep(sleeptime); |
|
106 |
} |
|
107 |
} |