added battery.h, last_state_of_charge to uint8_t
This commit is contained in:
parent
b8a8ecd59d
commit
c643f1cd96
3 changed files with 15 additions and 7 deletions
9
app/include/zmk/battery.h
Normal file
9
app/include/zmk/battery.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
/*
|
||||
* Copyright (c) 2021 The ZMK Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
uint8_t zmk_battery_state_of_charge();
|
|
@ -14,6 +14,4 @@ struct zmk_battery_state_changed {
|
|||
uint8_t state_of_charge;
|
||||
};
|
||||
|
||||
int32_t zmk_battery_state_of_charge();
|
||||
|
||||
ZMK_EVENT_DECLARE(zmk_battery_state_changed);
|
|
@ -15,13 +15,14 @@
|
|||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
#include <zmk/event_manager.h>
|
||||
#include <zmk/battery.h>
|
||||
#include <zmk/events/battery_state_changed.h>
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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(
|
||||
(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;
|
||||
|
|
Loading…
Reference in a new issue