commit 24ba146026ec5a732ed7442b9ecf2e141b0642e3
parent b646036ca3c11745293c5c961145b5e8b0973846
Author: bsandro <email@bsandro.tech>
Date: Sat, 29 Apr 2023 16:21:49 +0300
hp omen (i5-7300HQ) battery capacity
Diffstat:
1 file changed, 19 insertions(+), 8 deletions(-)
diff --git a/dwmstatus.c b/dwmstatus.c
@@ -161,14 +161,16 @@ getbattery()
int mib[5];
struct sensor temp;
size_t len;
- float percentage = 0.0;
+ float max_charge = 1;
+ float cur_charge = 1;
+ float percentage = 0;
char charger_state = ' ';
mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
- mib[2] = 1; // cwfg0
- mib[3] = SENSOR_PERCENT;
- mib[4] = 0;
+ mib[2] = 4; // acpibat0
+ mib[3] = SENSOR_AMPHOUR;
+ mib[4] = 3; // amphour3 - remaining capacity
len = sizeof(temp);
@@ -176,17 +178,26 @@ getbattery()
if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
return smprintf("sysctl error: %s\n", strerror(errno));
} else {
- percentage = (float)temp.value / 1000.0;
+ cur_charge = (float)temp.value;
+ }
+
+ mib[4] = 4; // amphour4 - design capacity
+ if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
+ return smprintf("sysctl error: %s\n", strerror(errno));
+ } else {
+ max_charge = (float)temp.value;
}
+ percentage = (cur_charge/max_charge) * 100.0f;
+
// charger state
- mib[2] = 3;
- mib[3] = SENSOR_INDICATOR;
+ mib[3] = SENSOR_INTEGER;
+ mib[4] = 0;
if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
smprintf("sysctl error: %s\n", strerror(errno));
} else {
- charger_state = temp.value == 1 ? '+' : '-';
+ charger_state = temp.value == 2 ? '+' : '-';
}
return smprintf("%.1f%%%c", percentage, charger_state);