Skip to content

Commit

Permalink
feat(ble): Only update BAS when active
Browse files Browse the repository at this point in the history
As a further mitigation against spurious wakeups without having to disable the battery service entirely, the battery percentage can only be updated when the board is active i.e. when in use, which infers the computer is also in use
  • Loading branch information
ReFil committed Nov 15, 2023
1 parent d7d9eed commit 1b2e214
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions app/src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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/activity.h>
#include <zmk/workqueue.h>

static uint8_t last_state_of_charge = 0;
Expand Down Expand Up @@ -52,13 +53,15 @@ static int zmk_battery_update(const struct device *battery) {
if (last_state_of_charge != state_of_charge.val1) {
last_state_of_charge = state_of_charge.val1;
#if IS_ENABLED(CONFIG_BT_BAS)
LOG_DBG("Setting BAS GATT battery level to %d.", last_state_of_charge);
if (zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE) {
LOG_DBG("Setting BAS GATT battery level to %d.", last_state_of_charge);

rc = bt_bas_set_battery_level(last_state_of_charge);
rc = bt_bas_set_battery_level(last_state_of_charge);

if (rc != 0) {
LOG_WRN("Failed to set BAS GATT battery level (err %d)", rc);
return rc;
if (rc != 0) {
LOG_WRN("Failed to set BAS GATT battery level (err %d)", rc);
return rc;
}
}
#endif
rc = ZMK_EVENT_RAISE(new_zmk_battery_state_changed(
Expand Down

0 comments on commit 1b2e214

Please sign in to comment.