From cc696a2ae838a9639335ca8eb3cb3b794c06bc33 Mon Sep 17 00:00:00 2001
From: Joel Challis
Date: Sun, 17 Aug 2025 01:14:48 +0100
Subject: Refactor battery driver (#25550)
---
docs/_sidebar.json | 1 +
docs/drivers/battery.md | 69 ++++++++++++++++-----------------------------
docs/features/battery.md | 55 ++++++++++++++++++++++++++++++++++++
docs/reference_info_json.md | 26 +++++++++++++++++
4 files changed, 106 insertions(+), 45 deletions(-)
create mode 100644 docs/features/battery.md
(limited to 'docs')
diff --git a/docs/_sidebar.json b/docs/_sidebar.json
index ee4709a650..eec345b788 100644
--- a/docs/_sidebar.json
+++ b/docs/_sidebar.json
@@ -175,6 +175,7 @@
]
},
{ "text": "Audio", "link": "/features/audio" },
+ { "text": "Battery", "link": "/features/battery" },
{ "text": "Bootmagic", "link": "/features/bootmagic" },
{ "text": "Converters", "link": "/feature_converters" },
{ "text": "Custom Matrix", "link": "/custom_matrix" },
diff --git a/docs/drivers/battery.md b/docs/drivers/battery.md
index e482ffc8b6..ae07668cc0 100644
--- a/docs/drivers/battery.md
+++ b/docs/drivers/battery.md
@@ -1,6 +1,6 @@
# Battery Driver
-This driver provides support for sampling battery level.
+This driver provides support for directly sampling battery level.
## Usage
@@ -10,21 +10,17 @@ To use this driver, add the following to your `rules.mk`:
BATTERY_DRIVER_REQUIRED = yes
```
-## Basic Configuration {#basic-configuration}
-
-Add the following to your `config.h`:
-
-|Define |Default |Description |
-|--------------------------|--------|--------------------------------------------------|
-|`BATTERY_SAMPLE_INTERVAL` |`30000` |The time between battery samples in milliseconds. |
+::::info Note
+This is already configured for you if you are using the [Battery](../features/battery) feature.
+::::
## Driver Configuration {#driver-configuration}
-Driver selection can be configured in `rules.mk` as `BATTERY_DRIVER`. Valid values are `adc` (default), `vendor`, or `custom`. See below for information on individual drivers.
+Driver selection can be configured in `rules.mk` as `BATTERY_DRIVER`. Valid values are `adc`, `vendor`, or `custom`. See below for information on individual drivers.
### ADC Driver {#adc-driver}
-This is the default battery driver. The default configuration assumes the battery is connected to a ADC capable pin through a voltage divider.
+The default configuration assumes the battery is connected to a ADC capable pin through a voltage divider.
```make
BATTERY_DRIVER = adc
@@ -32,42 +28,25 @@ BATTERY_DRIVER = adc
The following `#define`s apply only to the `adc` driver:
-|Define |Default |Description |
-|-----------------------------|--------------|--------------------------------------------------------------|
-|`BATTERY_PIN` |*Not defined* |The GPIO pin connected to the voltage divider. |
-|`BATTERY_REF_VOLTAGE_MV` |`3300` |The ADC reverence voltage, in millivolts. |
-|`BATTERY_VOLTAGE_DIVIDER_R1` |`100` |The voltage divider resistance, in kOhm. Set to 0 to disable. |
-|`BATTERY_VOLTAGE_DIVIDER_R2` |`100` |The voltage divider resistance, in kOhm. Set to 0 to disable. |
-|`BATTERY_ADC_RESOLUTION` |`10` |The ADC resolution configured for the ADC Driver. |
-
-## Functions
-
-### `uint8_t battery_get_percent(void)` {#api-battery-get-percent}
-
-Sample battery level.
-
-#### Return Value {#api-battery-get-percent-return}
-
-The battery percentage, in the range 0-100.
+|Define |Default |Description |
+|---------------------------------|--------------|--------------------------------------------------------------|
+|`BATTERY_ADC_PIN` |*Not defined* |The GPIO pin connected to the voltage divider. |
+|`BATTERY_ADC_REF_VOLTAGE_MV` |`3300` |The ADC reverence voltage, in millivolts. |
+|`BATTERY_ADC_VOLTAGE_DIVIDER_R1` |`100` |The voltage divider resistance, in kOhm. Set to 0 to disable. |
+|`BATTERY_ADC_VOLTAGE_DIVIDER_R2` |`100` |The voltage divider resistance, in kOhm. Set to 0 to disable. |
+|`BATTERY_ADC_RESOLUTION` |`10` |The ADC resolution configured for the ADC Driver. |
-## Callbacks
+### Custom Driver {#custom-driver}
-### `void battery_percent_changed_user(uint8_t level)` {#api-battery-percent-changed-user}
+A custom driver is expected to implement the following interface:
-User hook called when battery level changed.
+```c
+void battery_driver_init(void) {
+ // Perform any initialisation here
+}
-### Arguments {#api-battery-percent-changed-user-arguments}
-
- - `uint8_t level`
- The battery percentage, in the range 0-100.
-
----
-
-### `void battery_percent_changed_kb(uint8_t level)` {#api-battery-percent-changed-kb}
-
-Keyboard hook called when battery level changed.
-
-### Arguments {#api-battery-percent-changed-kb-arguments}
-
- - `uint8_t level`
- The battery percentage, in the range 0-100.
+uint8_t battery_driver_sample_percent(void) {
+ // Read and return current state here
+ return value;
+}
+```
diff --git a/docs/features/battery.md b/docs/features/battery.md
new file mode 100644
index 0000000000..f5c725efb9
--- /dev/null
+++ b/docs/features/battery.md
@@ -0,0 +1,55 @@
+# Battery
+
+This feature provides the high level abstraction for sampling battery level.
+
+## Usage
+
+To use this driver, add the following to your `rules.mk`:
+
+```make
+BATTERY_ENABLE = yes
+```
+
+## Basic Configuration {#basic-configuration}
+
+Add the following to your `config.h`:
+
+|Define |Default |Description |
+|--------------------------|--------|--------------------------------------------------|
+|`BATTERY_SAMPLE_INTERVAL` |`30000` |The time between battery samples in milliseconds. |
+
+## Driver Configuration {#driver-configuration}
+
+See the [Battery Driver](../drivers/battery) documentation for more information.
+
+## Functions
+
+### `uint8_t battery_get_percent(void)` {#api-battery-get-percent}
+
+Sample battery level.
+
+#### Return Value {#api-battery-get-percent-return}
+
+The battery percentage, in the range 0-100.
+
+## Callbacks
+
+### `void battery_percent_changed_user(uint8_t level)` {#api-battery-percent-changed-user}
+
+User hook called when battery level changed.
+
+### Arguments {#api-battery-percent-changed-user-arguments}
+
+ - `uint8_t level`
+ The battery percentage, in the range 0-100.
+
+---
+
+### `void battery_percent_changed_kb(uint8_t level)` {#api-battery-percent-changed-kb}
+
+Keyboard hook called when battery level changed.
+
+### Arguments {#api-battery-percent-changed-kb-arguments}
+
+ - `uint8_t level`
+ The battery percentage, in the range 0-100.
diff --git a/docs/reference_info_json.md b/docs/reference_info_json.md
index cf22317613..84377ef36c 100644
--- a/docs/reference_info_json.md
+++ b/docs/reference_info_json.md
@@ -179,6 +179,32 @@ Configures the [Backlight](features/backlight) feature.
* `pins` Array: Pin
* A list of GPIO pins connected to the backlight LEDs (`software` and `timer` drivers only).
+## Battery
+
+Configures the [Battery](features/battery) feature.
+
+* `battery`
+ * `adc`
+ * `pin` Pin Required
+ * The GPIO pin connected to the voltage divider.
+ * `reference_voltage` Number
+ * The ADC reverence voltage, in millivolts.
+ * Default: `3300`
+ * `divider_r1` Number
+ * The voltage divider resistance, in kOhm. Set to 0 to disable.
+ * Default: `100`
+ * `divider_r2` Number
+ * The voltage divider resistance, in kOhm. Set to 0 to disable.
+ * Default: `100`
+ * `resolution` Number
+ * The ADC resolution configured for the ADC Driver.
+ * Default: `10`
+ * `driver` String Required
+ * The driver to use. Must be one of `adc`, `custom`, `vendor`.
+ * `sample_interval` Number
+ * The delay between sampling the battery in milliseconds.
+ * Default: `30000` (30 s)
+
## Wireless/Bluetooth {#bluetooth}
Configures the [Wireless](features/wireless) feature.
--
cgit v1.2.3