Add another battery status called 'idle'

On some systems, a battery's charging regulator may not charge a battery
even though it is not full. This might be the case because it was
configured to stop charging at a capacity threshold, or e.g. because
environmental conditions do not allow for charging the battery.

This commit adds this status (called 'idle') and adds support for
detecting the status on Linux.
This commit is contained in:
erbth 2022-03-20 17:20:20 +01:00
parent 30ea2d2da3
commit 3272abcfbe
5 changed files with 19 additions and 2 deletions

View File

@ -322,6 +322,7 @@ int main(int argc, char *argv[]) {
CFG_STR("status_bat", "BAT", CFGF_NONE), CFG_STR("status_bat", "BAT", CFGF_NONE),
CFG_STR("status_unk", "UNK", CFGF_NONE), CFG_STR("status_unk", "UNK", CFGF_NONE),
CFG_STR("status_full", "FULL", CFGF_NONE), CFG_STR("status_full", "FULL", CFGF_NONE),
CFG_STR("status_idle", "IDLE", CFGF_NONE),
CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE), CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE),
CFG_INT("low_threshold", 30, CFGF_NONE), CFG_INT("low_threshold", 30, CFGF_NONE),
CFG_STR("threshold_type", "time", CFGF_NONE), CFG_STR("threshold_type", "time", CFGF_NONE),
@ -742,6 +743,7 @@ int main(int argc, char *argv[]) {
.status_bat = cfg_getstr(sec, "status_bat"), .status_bat = cfg_getstr(sec, "status_bat"),
.status_unk = cfg_getstr(sec, "status_unk"), .status_unk = cfg_getstr(sec, "status_unk"),
.status_full = cfg_getstr(sec, "status_full"), .status_full = cfg_getstr(sec, "status_full"),
.status_idle = cfg_getstr(sec, "status_idle"),
.low_threshold = cfg_getint(sec, "low_threshold"), .low_threshold = cfg_getint(sec, "low_threshold"),
.threshold_type = cfg_getstr(sec, "threshold_type"), .threshold_type = cfg_getstr(sec, "threshold_type"),
.last_full_capacity = cfg_getbool(sec, "last_full_capacity"), .last_full_capacity = cfg_getbool(sec, "last_full_capacity"),

View File

@ -271,6 +271,7 @@ typedef struct {
const char *status_bat; const char *status_bat;
const char *status_unk; const char *status_unk;
const char *status_full; const char *status_full;
const char *status_idle;
int low_threshold; int low_threshold;
char *threshold_type; char *threshold_type;
bool last_full_capacity; bool last_full_capacity;

View File

@ -87,6 +87,7 @@ battery 0 {
status_bat = "🔋 BAT" status_bat = "🔋 BAT"
status_unk = "? UNK" status_unk = "? UNK"
status_full = "☻ FULL" status_full = "☻ FULL"
status_idle = "☻ IDLE"
path = "/sys/class/power_supply/BAT%d/uevent" path = "/sys/class/power_supply/BAT%d/uevent"
low_threshold = 10 low_threshold = 10
} }

View File

@ -49,6 +49,7 @@ typedef enum {
CS_DISCHARGING, CS_DISCHARGING,
CS_CHARGING, CS_CHARGING,
CS_FULL, CS_FULL,
CS_IDLE,
} charging_status_t; } charging_status_t;
/* A description of the state of one or more batteries. */ /* A description of the state of one or more batteries. */
@ -129,6 +130,13 @@ static void add_battery_info(struct battery_info *acc, const struct battery_info
acc->status = batt_info->status; acc->status = batt_info->status;
/* else: retain FULL, since it is more specific than UNKNOWN */ /* else: retain FULL, since it is more specific than UNKNOWN */
break; break;
case CS_IDLE:
if (batt_info->status != CS_UNKNOWN && batt_info->status != CS_FULL)
acc->status = batt_info->status;
/* else: retain IDLE, since it is more specific than UNKNOWN and is
* implied by CS_FULL though correct for all batteries */
break;
} }
acc->present_rate = abs(present_rate); acc->present_rate = abs(present_rate);
@ -187,8 +195,10 @@ static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *bat
batt_info->status = CS_CHARGING; batt_info->status = CS_CHARGING;
else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full")) else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
batt_info->status = CS_FULL; batt_info->status = CS_FULL;
else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging") || BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Not charging")) else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging"))
batt_info->status = CS_DISCHARGING; batt_info->status = CS_DISCHARGING;
else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Not charging"))
batt_info->status = CS_IDLE;
else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=")) else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
batt_info->status = CS_UNKNOWN; batt_info->status = CS_UNKNOWN;
else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") || else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") ||
@ -674,6 +684,9 @@ void print_battery_info(battery_info_ctx_t *ctx) {
case CS_FULL: case CS_FULL:
statusstr = ctx->status_full; statusstr = ctx->status_full;
break; break;
case CS_IDLE:
statusstr = ctx->status_idle;
break;
default: default:
statusstr = ctx->status_unk; statusstr = ctx->status_unk;
} }