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 authored and petejohanson committed Nov 27, 2023
1 parent 84b9335 commit 6276e97
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 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 @@ -84,6 +86,10 @@ static void zmk_battery_timer(struct k_timer *timer) {

K_TIMER_DEFINE(battery_timer, zmk_battery_timer, NULL);

static void zmk_battery_start_reporting() {
k_timer_start(&battery_timer, K_NO_WAIT, K_SECONDS(CONFIG_ZMK_BATTERY_REPORT_INTERVAL));
}

static int zmk_battery_init(const struct device *_arg) {
#if !DT_HAS_CHOSEN(zmk_battery)
battery = device_get_binding("BATTERY");
Expand All @@ -100,9 +106,30 @@ static int zmk_battery_init(const struct device *_arg) {
return -ENODEV;
}

k_timer_start(&battery_timer, K_NO_WAIT, K_SECONDS(CONFIG_ZMK_BATTERY_REPORT_INTERVAL));

zmk_battery_start_reporting();
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:
zmk_battery_start_reporting();
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 6276e97

Please sign in to comment.