From 89e917d502e6a6f1042643296507336b660c17d9 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Fri, 20 Dec 2019 13:57:17 +1030 Subject: added cpu temp output support C and F, see comments in config.h. you need to set the right thermal zone directory. --- binstatus.c | 2 +- components.c | 54 ++++++++++++++++++++++++++++++++++++++++++------------ components.h | 3 ++- config.h | 4 ++++ 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/binstatus.c b/binstatus.c index 6211c07..de4a25d 100644 --- a/binstatus.c +++ b/binstatus.c @@ -42,7 +42,7 @@ main(void) } XStoreName(dsp, DefaultRootWindow(dsp), status); XFlush(dsp); - sleep(1); + sleep(SLEEPTIME); } XCloseDisplay(dsp); return 0; diff --git a/components.c b/components.c index 8244432..4611999 100644 --- a/components.c +++ b/components.c @@ -24,12 +24,12 @@ dectobin(int dec) return (dec % 2) + 10 * dectobin(dec / 2); } -char *eat(char *food, size_t len) +char *eatnonascii(char *food, size_t len) { - if (len == 0) return food; - while (food[len-1] == '\n') + if (len <= 0) return food; + while (food[len-1] < 32 || food[len-1] > 126) { - food[len-1] = '\0'; + food[--len] = '\0'; } return food; } @@ -73,14 +73,14 @@ char *currenttime(char *store, size_t size, int flag) } if (flag & BINARYTIME) { - snprintf(store, size, "%05d:%05d %s ", dectobin(time[0]), + snprintf(store, size, "%05d:%05d%s ", dectobin(time[0]), dectobin(time[1]), meridiem); } /* military time is the default, so we need not do anything */ else { - snprintf(store, size, "%02d:%02d %s ", time[0], time[1], + snprintf(store, size, "%02d:%02d%s ", time[0], time[1], meridiem); } return store; @@ -113,18 +113,48 @@ char *charging(char *store, size_t size, int flag) return "error "; } fgets(cap, 15, state); - eat(cap, strlen(cap)); + eatnonascii(cap, strlen(cap)); /* my thinkpad says "Unknown" when charged ... */ - if (strcmp(cap, "Unknown") == 0) strcpy(cap, "⚡F"); - if (strcmp(cap, "Charged") == 0) strcpy(cap, "⚡F"); - if (strcmp(cap, "Charging") == 0) strcpy(cap, "⚡C"); - if (strcmp(cap, "Discharging") == 0) strcpy(cap, "⚡D"); + if (strcmp(cap, "Unknown") == 0) strcpy(cap, "F"); + if (strcmp(cap, "Charged") == 0) strcpy(cap, "F"); + if (strcmp(cap, "Charging") == 0) strcpy(cap, "C"); + if (strcmp(cap, "Discharging") == 0) strcpy(cap, "D"); fclose(state); snprintf(store, size, "%s ", cap); return store; } -char *remainingtime(char *store, size_t size, int flag) +char *volume(char *store, size_t size, int flag) { + // TODO + return store; +} + +char *cputemp(char *store, size_t size, int flag) +{ + FILE *cpufile; + char temp[15]; + int t; + cpufile = fopen(TEMPFILE, "r"); + if (!cpufile) + { + fprintf(stderr, "cannot read CPU temp\n"); + return "error "; + } + fgets(temp, 4, cpufile); + eatnonascii(temp, strlen(temp)); + if (temp[2] == '0') + temp[2] = '\0'; + fclose(cpufile); + + t = atoi(temp); + + if (flag & FARENHEIT) + { + t = (t*9)/5+32; + snprintf(store, size, "%d°F ", t); + } + else + snprintf(store, size, "%d°C ", t); return store; } diff --git a/components.h b/components.h index 8af712e..c9f5e54 100644 --- a/components.h +++ b/components.h @@ -25,6 +25,7 @@ char *currenttime(char *store, size_t size, int flag); char *battery(char *store, size_t size, int flag); char *charging(char *store, size_t size, int flag); -char *remainingtime(char *store, size_t size, int flag); +char *volume(char *store, size_t size, int flag); +char *cputemp(char *store, size_t size, int flag); #endif diff --git a/config.h b/config.h index 099aa22..4db0606 100644 --- a/config.h +++ b/config.h @@ -16,6 +16,8 @@ #ifndef CONFIG_H #define CONFIG_H +#define SLEEPTIME 5 // number of seconds to sleep before each update +#define TEMPFILE "/sys/class/thermal/thermal_zone0/temp" #define MAXLENGTH 256 enum flag @@ -25,6 +27,7 @@ enum flag BINARYTIME = 1<<2, MILITARYTIME = 1<<3, SHOWMERIDIEM = 1<<4, + FARENHEIT = 1<<5, }; struct component @@ -42,6 +45,7 @@ static const struct component components[] ={ {currenttime, NORMALTIME|SHOWMERIDIEM}, {battery, NONE}, {charging, NONE}, + {cputemp, NONE}, }; #endif -- cgit v1.2.3