Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BAS GATT reporting #306

Merged
merged 4 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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)
Expand Down
2 changes: 1 addition & 1 deletion app/boards/arm/bluemicro840/bluemicro840_v1.dts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

vbatt {
compatible = "zmk,battery-voltage-divider";
label = "VOLTAGE_DIVIDER";
label = "BATTERY";
io-channels = <&adc 7>;
output-ohms = <2000000>;
full-ohms = <(2000000 + 806000)>;
Expand Down
2 changes: 1 addition & 1 deletion app/boards/arm/nice_nano/nice_nano.dts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

vbatt {
compatible = "zmk,battery-voltage-divider";
label = "VOLTAGE_DIVIDER";
label = "BATTERY";
io-channels = <&adc 2>;
output-ohms = <2000000>;
full-ohms = <(2000000 + 806000)>;
Expand Down
2 changes: 1 addition & 1 deletion app/boards/arm/nrfmicro/nrfmicro_13.dts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

vbatt {
compatible = "zmk,battery-voltage-divider";
label = "VOLTAGE_DIVIDER";
label = "BATTERY";
io-channels = <&adc 2>;
output-ohms = <2000000>;
full-ohms = <(2000000 + 820000)>;
Expand Down
4 changes: 3 additions & 1 deletion app/drivers/zephyr/battery_voltage_divider.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ static int bvd_sample_fetch(struct device *dev, enum sensor_channel chan) {
struct adc_sequence *as = &drv_data->as;

// Make sure selected channel is supported
if (chan != SENSOR_CHAN_GAUGE_VOLTAGE && chan != SENSOR_CHAN_GAUGE_STATE_OF_CHARGE) {
if (chan != SENSOR_CHAN_GAUGE_VOLTAGE && chan != SENSOR_CHAN_GAUGE_STATE_OF_CHARGE &&
chan != SENSOR_CHAN_ALL) {
LOG_DBG("Selected channel is not supported: %d.", chan);
return -ENOTSUP;
}

Expand Down
77 changes: 77 additions & 0 deletions app/src/battery.c
Original file line number Diff line number Diff line change
@@ -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) {
Nicell marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Nicell marked this conversation as resolved.
Show resolved Hide resolved

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);