-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattery_status.cpp
More file actions
25 lines (25 loc) · 962 Bytes
/
battery_status.cpp
File metadata and controls
25 lines (25 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#define PATH_BATT_CHARGE_NOW "/sys/class/power_supply/BAT0/subsystem/BAT0/charge_now"
#define PATH_BATT_CHARGE_FULL "/sys/class/power_supply/BAT0/subsystem/BAT0/charge_full"
int getBattState(void)
{
int chargedPercent = 0;
FILE *battChargeNow;
FILE *battChargeFull;
long unsigned int battMax_mAh = 0;
long unsigned int battRemain_mAh = 0;
if (NULL == (battChargeNow = fopen(PATH_BATT_CHARGE_NOW, "r")))
{
fclose(battChargeNow);
return -1;
}
if (NULL == (battChargeFull = fopen(PATH_BATT_CHARGE_FULL, "r")))
{
fclose(battChargeNow);
fclose(battChargeFull);
return -1;
}
fscanf((FILE *)battChargeFull, "%lu", &battMax_mAh);
fscanf((FILE *)battChargeNow, "%lu", &battRemain_mAh);
chargedPercent = 100.00 * ((float)battRemain_mAh / (float)battMax_mAh);
return chargedPercent;
}