diff options
author | JP Bonn <jp-keyword-bitbucket.d58644@jpbonn.com> | 2021-03-04 18:50:32 -0700 |
---|---|---|
committer | Pete Johanson <peter@peterjohanson.com> | 2021-03-10 18:07:18 -0500 |
commit | df4ec51f964f5647bb46fb2b721370064073bb54 (patch) | |
tree | 8c937d21e34987b74ff2a2ae68802e0bbb72d99f /app/src | |
parent | d956193251739026e1d52f83a45648cb4b6de110 (diff) |
only send zmk_battery_state_changed on change
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/battery.c | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/app/src/battery.c b/app/src/battery.c index 5a7c57b..8caa024 100644 --- a/app/src/battery.c +++ b/app/src/battery.c @@ -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,17 +40,23 @@ static int zmk_battery_update(const struct device *battery) { return rc; } - LOG_DBG("Setting BAS GATT battery level to %d.", state_of_charge.val1); + 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); + rc = bt_bas_set_battery_level(state_of_charge.val1); - 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; + } + + 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 ZMK_EVENT_RAISE(new_zmk_battery_state_changed( - (struct zmk_battery_state_changed){.state_of_charge = state_of_charge.val1})); + return rc; } static void zmk_battery_work(struct k_work *work) { |