feat(battery): Add BAS GATT reporting
This commit is contained in:
parent
74fa113d88
commit
953de71646
2 changed files with 78 additions and 0 deletions
|
@ -54,6 +54,7 @@ endif()
|
|||
target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c)
|
||||
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/behaviors/behavior_bt.c)
|
||||
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c)
|
||||
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/battery.c)
|
||||
target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split_listener.c)
|
||||
target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c)
|
||||
target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL app PRIVATE src/split/bluetooth/central.c)
|
||||
|
|
77
app/src/battery.c
Normal file
77
app/src/battery.c
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright (c) 2020 The ZMK Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <device.h>
|
||||
#include <init.h>
|
||||
#include <kernel.h>
|
||||
#include <drivers/sensor.h>
|
||||
#include <bluetooth/services/bas.h>
|
||||
|
||||
#include <logging/log.h>
|
||||
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
struct device *battery;
|
||||
|
||||
static int zmk_battery_update(struct device *battery) {
|
||||
struct sensor_value state_of_charge;
|
||||
|
||||
int rc = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_STATE_OF_CHARGE);
|
||||
|
||||
if (rc != 0) {
|
||||
LOG_DBG("Failed to fetch battery values: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STATE_OF_CHARGE, &state_of_charge);
|
||||
|
||||
if (rc != 0) {
|
||||
LOG_DBG("Failed to get battery state of charge: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
LOG_DBG("Setting BAS GATT battery level to %d.", state_of_charge.val1);
|
||||
|
||||
return bt_gatt_bas_set_battery_level(state_of_charge.val1);
|
||||
}
|
||||
|
||||
static void zmk_battery_work(struct k_work *work) {
|
||||
int rc = zmk_battery_update(battery);
|
||||
|
||||
if (rc != 0) {
|
||||
LOG_DBG("Failed to update battery value: %d.", rc);
|
||||
}
|
||||
}
|
||||
|
||||
K_WORK_DEFINE(battery_work, zmk_battery_work);
|
||||
|
||||
static void zmk_battery_timer(struct k_timer *timer) { k_work_submit(&battery_work); }
|
||||
|
||||
K_TIMER_DEFINE(battery_timer, zmk_battery_timer, NULL);
|
||||
|
||||
static int zmk_battery_init(struct device *_arg) {
|
||||
battery = device_get_binding("BATTERY");
|
||||
|
||||
if (battery) {
|
||||
LOG_DBG("Found battery reporting device.");
|
||||
} else {
|
||||
LOG_DBG("No battery device labelled BATTERY found.");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
int rc = zmk_battery_update(battery);
|
||||
|
||||
if (rc != 0) {
|
||||
LOG_DBG("Failed to update battery value: %d.", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
k_timer_start(&battery_timer, K_MINUTES(1), K_MINUTES(1));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(zmk_battery_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
|
Loading…
Reference in a new issue