feat(battery)!: Add chosen node for battery

battery.c now uses the zmk,battery chosen node to select a battery
sensor. Using the node labeled "BATTERY" is maintained for backwards
compatibility but is now deprecated. Custom boards should switch to
using the chosen node.

# Conflicts:
#	app/boards/arm/bluemicro840/bluemicro840_v1.dts
#	app/boards/arm/nice60/nice60.dts
#	app/boards/arm/nrfmicro/nrfmicro_13.dts

# Conflicts:
#	app/boards/arm/bluemicro840/bluemicro840_v1.dts
This commit is contained in:
Joel Spadin
2021-07-17 17:49:37 -05:00
committed by Pete Johanson
parent d08463e483
commit 388e345c28
10 changed files with 40 additions and 12 deletions

View File

@@ -5,6 +5,7 @@
*/
#include <device.h>
#include <devicetree.h>
#include <init.h>
#include <kernel.h>
#include <drivers/sensor.h>
@@ -18,12 +19,16 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/battery.h>
#include <zmk/events/battery_state_changed.h>
const struct device *battery;
static uint8_t last_state_of_charge = 0;
uint8_t zmk_battery_state_of_charge() { return last_state_of_charge; }
#if DT_HAS_CHOSEN(zmk_battery)
static const struct device *const battery = DEVICE_DT_GET(DT_CHOSEN(zmk_battery));
#else
static const struct device *battery;
#endif
static int zmk_battery_update(const struct device *battery) {
struct sensor_value state_of_charge;
@@ -75,10 +80,18 @@ static void zmk_battery_timer(struct k_timer *timer) { k_work_submit(&battery_wo
K_TIMER_DEFINE(battery_timer, zmk_battery_timer, NULL);
static int zmk_battery_init(const struct device *_arg) {
#if !DT_HAS_CHOSEN(zmk_battery)
battery = device_get_binding("BATTERY");
if (battery == NULL) {
LOG_DBG("No battery device labelled BATTERY found.");
return -ENODEV;
}
LOG_WRN("Finding battery device labeled BATTERY is deprecated. Use zmk,battery chosen node.");
#endif
if (!device_is_ready(battery)) {
LOG_ERR("Battery device \"%s\" is not ready", battery->name);
return -ENODEV;
}