From 3ab85464ff92c57a33945a32a8a00df5a9abb684 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 21 Nov 2024 20:32:46 -0600 Subject: add status symbols for not charging, full, and unknown status --- lowbat.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'lowbat.c') diff --git a/lowbat.c b/lowbat.c index 572873a..b5fb665 100644 --- a/lowbat.c +++ b/lowbat.c @@ -21,6 +21,7 @@ static char batteries[MAX_BATTERIES][MAX_PSU_NAME_LEN]; static unsigned int num_batteries; static FILE *fp; static char prop_path[MAX_PROP_PATH_LEN]; +static enum StatusSymbols { UNKNOWN = '?', CHARGING = '+', DISCHARGING = '-', NOT_CHARGING = '=', FULL = '=' }; // functions void load_installed_batteries() { @@ -111,33 +112,44 @@ int get_total_capacity() { return energy_now * 100 / energy_full; } -int is_discharging() { - char status[12]; // valid values: "Unknown", "Charging", "Discharging", "Not charging", "Full" +char get_status() { + char status[MAX_BATTERIES]; int i; if (!num_batteries) - return 0; + return UNKNOWN; + /* store first letter of each battery's status + * valid values: "Unknown", "Charging", "Discharging", "Not charging", "Full" */ + // return discharging if any batteries are discharging for (i = 0; i < num_batteries; i++) { - read_prop(batteries[i], "status", "%s", &status); - if (strcmp(status, "Discharging") != 0) - return 0; + read_prop(batteries[i], "status", "%c", &status[i]); + if (status[i] == 'D') + return DISCHARGING; } - - return 1; + // return charging if any batteries are charging + if (strchr(status, 'C') != NULL) + return CHARGING; + // return not charging if any batteries are not charging + if (strchr(status, 'F') != NULL || strchr(status, 'N') != NULL) + return NOT_CHARGING; + + // return unknown otherwise + return UNKNOWN; } int main() { - int discharging, capacity, old_discharging = -1, old_capacity = -1; + char status, old_status = -1; + int capacity, old_capacity = -1; int warning_level = 0; // 1 if warned already, 2 if warned with critical warning char msg[MAX_MSG_LEN]; while (1) { load_installed_batteries(); - discharging = is_discharging(); + status = get_status(); capacity = get_total_capacity(); - if (discharging) { + if (status == DISCHARGING) { if (capacity <= 5 && warning_level < 2) { snprintf(msg, MAX_MSG_LEN, "%d%% remains", capacity); notifysend("Critical Low Battery Warning", msg, NOTIFY_URGENCY_CRITICAL); @@ -153,11 +165,11 @@ int main() { warning_level = 0; // clear conditions } - if (discharging != old_discharging || capacity != old_capacity) { - printf("%s%d%%\n", discharging ? "-" : "+", capacity); + if (status != old_status || capacity != old_capacity) { + printf("%c%d%%\n", status, capacity); fflush(stdout); } - old_discharging = discharging; + old_status = status; old_capacity = capacity; sleep(PERIOD); } -- cgit v1.2.3