finalized support for optional laptop battery level

This commit is contained in:
shockrahwow 2019-09-02 03:04:32 -07:00
parent 217889d082
commit 8f5152206b
2 changed files with 9 additions and 3 deletions

View File

@ -7,6 +7,9 @@ laptop:
# compiling with LAPTOP=1
gcc -D LAPTOP=1 status.c -o $(output) $(flags)
run:
./statline
clean:
rm -f statline

View File

@ -17,6 +17,7 @@
#define BASIC_MSG "/comfy/ o clock | "
#define TIME_FORMAT "%I:%M%p - %d %b %Y"
// Time in seconds
#define TIME_DELAY 5
#define MAXSTR 1024
@ -62,6 +63,7 @@ ram_usage(void)
#ifdef LAPTOP
#define BATTERY_STATUS_BUFFER 50
#define REDUCER 1000
static char*
battery_percentage(void)
{
@ -72,17 +74,18 @@ battery_percentage(void)
if(file_max) {
fgets(buf, BATTERY_STATUS_BUFFER, file_max);
fclose(file_max);
total = atoi(buf);
total = atoi(buf)/REDUCER;
}
FILE* file_curr = fopen("/sys/class/power_supply/BAT0/energy_now", "r");
if(file_curr) {
fgets(buf, BATTERY_STATUS_BUFFER, file_max);
fclose(file_curr);
total = atoi(buf);
curr = atoi(buf)/REDUCER;
}
snprintf(buf, BATTERY_STATUS_BUFFER, "Battery: %.2f", (float)(curr/total));
float level = (float)curr / (float)total;
snprintf(buf, BATTERY_STATUS_BUFFER, "Battery: %.2f%%", level*100);
return buf;
}
#endif