Browse Source

only send zmk_battery_state_changed on change

xmkb
JP Bonn 4 years ago committed by Pete Johanson
parent
commit
df4ec51f96
  1. 2
      app/include/zmk/events/battery_state_changed.h
  2. 12
      app/src/battery.c

2
app/include/zmk/events/battery_state_changed.h

@ -14,4 +14,6 @@ struct zmk_battery_state_changed { @@ -14,4 +14,6 @@ struct zmk_battery_state_changed {
uint8_t state_of_charge;
};
int32_t zmk_battery_state_of_charge();
ZMK_EVENT_DECLARE(zmk_battery_state_changed);

12
app/src/battery.c

@ -19,6 +19,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -19,6 +19,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
const struct device *battery;
static int32_t last_state_of_charge = 0;
int32_t zmk_battery_state_of_charge() { return last_state_of_charge; }
static int zmk_battery_update(const struct device *battery) {
struct sensor_value state_of_charge;
@ -36,6 +40,7 @@ static int zmk_battery_update(const struct device *battery) { @@ -36,6 +40,7 @@ static int zmk_battery_update(const struct device *battery) {
return rc;
}
if (last_state_of_charge != state_of_charge.val1) {
LOG_DBG("Setting BAS GATT battery level to %d.", state_of_charge.val1);
rc = bt_bas_set_battery_level(state_of_charge.val1);
@ -45,8 +50,13 @@ static int zmk_battery_update(const struct device *battery) { @@ -45,8 +50,13 @@ static int zmk_battery_update(const struct device *battery) {
return rc;
}
return ZMK_EVENT_RAISE(new_zmk_battery_state_changed(
rc = ZMK_EVENT_RAISE(new_zmk_battery_state_changed(
(struct zmk_battery_state_changed){.state_of_charge = state_of_charge.val1}));
last_state_of_charge = state_of_charge.val1;
}
return rc;
}
static void zmk_battery_work(struct k_work *work) {

Loading…
Cancel
Save