status-temp/temp.c
changeset 12 ae09261c22e3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/status-temp/temp.c	Wed Dec 10 20:30:53 2014 +0400
@@ -0,0 +1,107 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <string.h>
+#include <limits.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "config.h"
+
+
+int catint(char *name) {
+    FILE* f = fopen(name, "r");
+    int i = 0;
+    if (fscanf(f, "%d", &i) != 1)
+        i = -1;
+    fclose(f);
+    return i;
+}
+
+char *color_stop;
+char *color_red;
+char *color_orange;
+char *color_green;
+char *color_none;
+
+char *color(int low,int mid,int rev,int value) {
+    if (value>mid) {
+        return rev ? color_red : color_green;
+    } else if (value>low) {
+        return color_orange;
+    }
+    return rev ? color_green : color_red;
+}
+
+int main(int argc, char *argv[]) {
+    if (argc<=2) {
+        fprintf(stderr, "Usage: %s <term|pango> <sensor1> [sensor2] ...\n", argv[0]);
+        fflush(stderr);
+        return 1;
+    }
+
+    char *color;
+    if (!strcmp(argv[1],"term")) {
+        color_stop = "\x1b[0m";
+        color_red = "\x1b[1;31m";
+        color_green = "\x1b[1;32m";
+        color_orange = "\x1b[1;33m";
+        color_none = color_stop;
+    } else if (!strcmp(argv[1],"pango")) {
+        color_stop = "</span>";
+        color_red = "<span color='#f33'>";
+        color_green = "<span color='#0f0'>";
+        color_orange = "<span color='#fa0'>";
+        color_none = "<span>";
+    } else {
+        color_stop = color_red = color_green = color_orange = color_none = "";
+    }
+
+    int sleeptime = UPDATE_PERIOD;
+    int alertflash = 1;
+    while (1) {
+        int i;
+        int maxtemp = 0;
+        for (i=2;i<argc;i++) {
+            int temp = catint(argv[i]);
+            if (temp > maxtemp)
+                maxtemp = temp;
+        }
+
+        float ctemp = maxtemp/1000;
+
+        if (ctemp <= GREEN_VALUE)
+            color = color_green;
+        else if (ctemp >= RED_VALUE)
+            color = color_red;
+        else if (ctemp >= ORANGE_VALUE)
+            color = color_orange;
+        else
+            color = color_none;
+
+#ifdef FLASH_AFTER
+        if (ctemp >= FLASH_AFTER) {
+            sleeptime = 1;
+            alertflash = !alertflash;
+        } else {
+            sleeptime = UPDATE_PERIOD;
+            alertflash = 1;
+        }
+#endif
+        if (alertflash) {
+            printf("%s%2.1f%s\n",color, ctemp, color_stop);
+        } else {
+            int len = snprintf(NULL, 0, "%2.1f", ctemp);
+            for (i=0; i<len; i++)
+                printf(" ");
+            printf("\n");
+        }
+        fflush(stdout);
+        sleep(sleeptime);
+    }
+}