Add power consumption

This commit is contained in:
Przemek Grondek 2022-10-14 00:05:19 +02:00
parent 13663512a4
commit cd487dcf76

View File

@ -100,7 +100,11 @@ readfile(char *base, char *file)
memset(line, 0, sizeof(line));
path = smprintf("%s/%s", base, file);
if (file != NULL)
path = smprintf("%s/%s", base, file);
else {
path = smprintf("%s", base);
}
fd = fopen(path, "r");
free(path);
if (fd == NULL)
@ -194,6 +198,26 @@ getBrightness(char *base) {
return smprintf("%s %d%% ", status, percentage);
}
char *
getPowerConsumption(char *base) {
char *co;
long usageRead;
float usage;
co = readfile(base, NULL);
if (co == NULL)
return smprintf("");
sscanf(co, "%ld", &usageRead);
free(co);
if (usage == 0)
return smprintf("");
usage = usageRead/ 1e6;
return smprintf("%.2lf W ", usage);
}
char *
gettemperature(char *base, char *sensor)
@ -213,15 +237,17 @@ void update() {
char *t0;
char *bat;
char *brightness;
char *powerConsumption;
avgs = loadavg();
tmbln = mktimes("%H:%M %d.%m.%Y", tzwarsaw);
t0 = gettemperature("/sys/class//hwmon/hwmon1", "temp1_input");
bat = getbattery("/sys/class/power_supply/BAT0");
brightness = getBrightness("/sys/class/backlight/amdgpu_bl0");
powerConsumption = getPowerConsumption("/sys/class/power_supply/BAT0/power_now");
status = smprintf("%s%s%s%s%s",
bat, brightness, t0, avgs, tmbln);
status = smprintf("%s%s%s%s%s%s",
powerConsumption, bat, brightness, t0, avgs, tmbln);
setstatus(status);
free(t0);