Skip to content

Commit

Permalink
feat(ble): Only update BAS when active
Browse files Browse the repository at this point in the history
Subscribes to the activity changing event, will stop the battery work timer when in idle or deep sleep, restart when board goes active
  • Loading branch information
ReFil committed Nov 16, 2023
1 parent d7d9eed commit e3f8010
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event_manager.h>
#include <zmk/battery.h>
#include <zmk/events/battery_state_changed.h>
#include <zmk/events/activity_state_changed.h>
#include <zmk/activity.h>
#include <zmk/workqueue.h>

static uint8_t last_state_of_charge = 0;
Expand Down Expand Up @@ -105,4 +107,26 @@ static int zmk_battery_init(const struct device *_arg) {
return 0;
}

static int battery_event_listener(const zmk_event_t *eh) {

if (as_zmk_activity_state_changed(eh)) {
switch (zmk_activity_get_state()) {
case ZMK_ACTIVITY_ACTIVE:
k_timer_start(&battery_timer, K_NO_WAIT, K_SECONDS(CONFIG_ZMK_BATTERY_REPORT_INTERVAL));
return 0;
case ZMK_ACTIVITY_IDLE:
case ZMK_ACTIVITY_SLEEP:
k_timer_stop(&battery_timer);
return 0;
default:
break;
}
}
return -ENOTSUP;
}

ZMK_LISTENER(battery, battery_event_listener);

ZMK_SUBSCRIPTION(battery, zmk_activity_state_changed);

SYS_INIT(zmk_battery_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);

0 comments on commit e3f8010

Please sign in to comment.