summaryrefslogtreecommitdiff
path: root/drivers/battery
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2025-08-17 01:14:48 +0100
committerGitHub <noreply@github.com>2025-08-17 01:14:48 +0100
commitcc696a2ae838a9639335ca8eb3cb3b794c06bc33 (patch)
tree901b54bda536acb5503c6cf924b0f30bca1a174e /drivers/battery
parentf29d8117bf877a4df1f88f40e0131f4465748540 (diff)
Refactor battery driver (#25550)
Diffstat (limited to 'drivers/battery')
-rw-r--r--drivers/battery/battery.c41
-rw-r--r--drivers/battery/battery.h46
-rw-r--r--drivers/battery/battery_adc.c25
3 files changed, 13 insertions, 99 deletions
diff --git a/drivers/battery/battery.c b/drivers/battery/battery.c
deleted file mode 100644
index faf3c5a214..0000000000
--- a/drivers/battery/battery.c
+++ /dev/null
@@ -1,41 +0,0 @@
1// Copyright 2025 QMK
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#include "battery_driver.h"
5#include "battery.h"
6#include "timer.h"
7
8#ifndef BATTERY_SAMPLE_INTERVAL
9# define BATTERY_SAMPLE_INTERVAL 30000
10#endif
11
12static uint8_t last_bat_level = 100;
13
14void battery_init(void) {
15 battery_driver_init();
16
17 last_bat_level = battery_driver_sample_percent();
18}
19
20__attribute__((weak)) void battery_percent_changed_user(uint8_t level) {}
21__attribute__((weak)) void battery_percent_changed_kb(uint8_t level) {}
22
23static void handle_percent_changed(void) {
24 battery_percent_changed_user(last_bat_level);
25 battery_percent_changed_kb(last_bat_level);
26}
27
28void battery_task(void) {
29 static uint32_t bat_timer = 0;
30 if (timer_elapsed32(bat_timer) > BATTERY_SAMPLE_INTERVAL) {
31 last_bat_level = battery_driver_sample_percent();
32
33 handle_percent_changed();
34
35 bat_timer = timer_read32();
36 }
37}
38
39uint8_t battery_get_percent(void) {
40 return last_bat_level;
41}
diff --git a/drivers/battery/battery.h b/drivers/battery/battery.h
deleted file mode 100644
index 0985723eaa..0000000000
--- a/drivers/battery/battery.h
+++ /dev/null
@@ -1,46 +0,0 @@
1// Copyright 2025 QMK
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <stdint.h>
7
8/**
9 * \file
10 *
11 * \defgroup battery Battery API
12 *
13 * \brief API to query battery status.
14 * \{
15 */
16
17/**
18 * \brief Initialize the battery driver.
19 */
20void battery_init(void);
21
22/**
23 * \brief Perform housekeeping tasks.
24 */
25void battery_task(void);
26
27/**
28 * \brief Sample battery level.
29 *
30 * \return The battery percentage, in the range 0-100.
31 */
32uint8_t battery_get_percent(void);
33
34/**
35 * \brief user hook called when battery level changed.
36 *
37 */
38void battery_percent_changed_user(uint8_t level);
39
40/**
41 * \brief keyboard hook called when battery level changed.
42 *
43 */
44void battery_percent_changed_kb(uint8_t level);
45
46/** \} */
diff --git a/drivers/battery/battery_adc.c b/drivers/battery/battery_adc.c
index cf0e69cb48..145265b5db 100644
--- a/drivers/battery/battery_adc.c
+++ b/drivers/battery/battery_adc.c
@@ -1,23 +1,24 @@
1// Copyright 2025 QMK 1// Copyright 2025 QMK
2// SPDX-License-Identifier: GPL-2.0-or-later 2// SPDX-License-Identifier: GPL-2.0-or-later
3 3
4#include "battery_driver.h"
4#include "analog.h" 5#include "analog.h"
5#include "gpio.h" 6#include "gpio.h"
6 7
7#ifndef BATTERY_PIN 8#ifndef BATTERY_ADC_PIN
8# error("BATTERY_PIN not configured!") 9# error("BATTERY_ADC_PIN not configured!")
9#endif 10#endif
10 11
11#ifndef BATTERY_REF_VOLTAGE_MV 12#ifndef BATTERY_ADC_REF_VOLTAGE_MV
12# define BATTERY_REF_VOLTAGE_MV 3300 13# define BATTERY_ADC_REF_VOLTAGE_MV 3300
13#endif 14#endif
14 15
15#ifndef BATTERY_VOLTAGE_DIVIDER_R1 16#ifndef BATTERY_ADC_VOLTAGE_DIVIDER_R1
16# define BATTERY_VOLTAGE_DIVIDER_R1 100 17# define BATTERY_VOLTAGE_DIVIDER_R1 100
17#endif 18#endif
18 19
19#ifndef BATTERY_VOLTAGE_DIVIDER_R2 20#ifndef BATTERY_ADC_VOLTAGE_DIVIDER_R2
20# define BATTERY_VOLTAGE_DIVIDER_R2 100 21# define BATTERY_ADC_VOLTAGE_DIVIDER_R2 100
21#endif 22#endif
22 23
23// TODO: infer from adc config? 24// TODO: infer from adc config?
@@ -26,16 +27,16 @@
26#endif 27#endif
27 28
28void battery_driver_init(void) { 29void battery_driver_init(void) {
29 gpio_set_pin_input(BATTERY_PIN); 30 gpio_set_pin_input(BATTERY_ADC_PIN);
30} 31}
31 32
32uint16_t battery_driver_get_mv(void) { 33uint16_t battery_driver_get_mv(void) {
33 uint32_t raw = analogReadPin(BATTERY_PIN); 34 uint32_t raw = analogReadPin(BATTERY_ADC_PIN);
34 35
35 uint32_t bat_mv = raw * BATTERY_REF_VOLTAGE_MV / (1 << BATTERY_ADC_RESOLUTION); 36 uint32_t bat_mv = raw * BATTERY_ADC_REF_VOLTAGE_MV / (1 << BATTERY_ADC_RESOLUTION);
36 37
37#if BATTERY_VOLTAGE_DIVIDER_R1 > 0 && BATTERY_VOLTAGE_DIVIDER_R2 > 0 38#if BATTERY_VOLTAGE_DIVIDER_R1 > 0 && BATTERY_ADC_VOLTAGE_DIVIDER_R2 > 0
38 bat_mv = bat_mv * (BATTERY_VOLTAGE_DIVIDER_R1 + BATTERY_VOLTAGE_DIVIDER_R2) / BATTERY_VOLTAGE_DIVIDER_R2; 39 bat_mv = bat_mv * (BATTERY_VOLTAGE_DIVIDER_R1 + BATTERY_ADC_VOLTAGE_DIVIDER_R2) / BATTERY_ADC_VOLTAGE_DIVIDER_R2;
39#endif 40#endif
40 41
41 return bat_mv; 42 return bat_mv;