Browse Source

added battery.h, last_state_of_charge to uint8_t

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

9
app/include/zmk/battery.h

@ -0,0 +1,9 @@
/*
* Copyright (c) 2021 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once
uint8_t zmk_battery_state_of_charge();

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

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

11
app/src/battery.c

@ -15,13 +15,14 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event_manager.h> #include <zmk/event_manager.h>
#include <zmk/battery.h>
#include <zmk/events/battery_state_changed.h> #include <zmk/events/battery_state_changed.h>
const struct device *battery; const struct device *battery;
static int32_t last_state_of_charge = 0; static uint8_t last_state_of_charge = 0;
int32_t zmk_battery_state_of_charge() { return last_state_of_charge; } uint8_t zmk_battery_state_of_charge() { return last_state_of_charge; }
static int zmk_battery_update(const struct device *battery) { static int zmk_battery_update(const struct device *battery) {
struct sensor_value state_of_charge; struct sensor_value state_of_charge;
@ -43,9 +44,9 @@ static int zmk_battery_update(const struct device *battery) {
if (last_state_of_charge != state_of_charge.val1) { if (last_state_of_charge != state_of_charge.val1) {
last_state_of_charge = state_of_charge.val1; last_state_of_charge = state_of_charge.val1;
LOG_DBG("Setting BAS GATT battery level to %d.", state_of_charge.val1); LOG_DBG("Setting BAS GATT battery level to %d.", last_state_of_charge);
rc = bt_bas_set_battery_level(state_of_charge.val1); rc = bt_bas_set_battery_level(last_state_of_charge);
if (rc != 0) { if (rc != 0) {
LOG_WRN("Failed to set BAS GATT battery level (err %d)", rc); LOG_WRN("Failed to set BAS GATT battery level (err %d)", rc);
@ -53,7 +54,7 @@ static int zmk_battery_update(const struct device *battery) {
} }
rc = 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})); (struct zmk_battery_state_changed){.state_of_charge = last_state_of_charge}));
} }
return rc; return rc;

Loading…
Cancel
Save