Refactor battery driver (#25550)

This commit is contained in:
Joel Challis 2025-08-17 01:14:48 +01:00 committed by GitHub
parent f29d8117bf
commit cc696a2ae8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 287 additions and 73 deletions

View file

@ -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" },

View file

@ -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. |
|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. |
## Functions
### Custom Driver {#custom-driver}
### `uint8_t battery_get_percent(void)` {#api-battery-get-percent}
A custom driver is expected to implement the following interface:
Sample battery level.
```c
void battery_driver_init(void) {
// Perform any initialisation here
}
#### 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.
uint8_t battery_driver_sample_percent(void) {
// Read and return current state here
return value;
}
```

55
docs/features/battery.md Normal file
View file

@ -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.

View file

@ -179,6 +179,32 @@ Configures the [Backlight](features/backlight) feature.
* `pins` <Badge type="info">Array: Pin</Badge>
* 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` <Badge type="info">Pin</Badge> <Badge>Required</Badge>
* The GPIO pin connected to the voltage divider.
* `reference_voltage` <Badge type="info">Number</Badge>
* The ADC reverence voltage, in millivolts.
* Default: `3300`
* `divider_r1` <Badge type="info">Number</Badge>
* The voltage divider resistance, in kOhm. Set to 0 to disable.
* Default: `100`
* `divider_r2` <Badge type="info">Number</Badge>
* The voltage divider resistance, in kOhm. Set to 0 to disable.
* Default: `100`
* `resolution` <Badge type="info">Number</Badge>
* The ADC resolution configured for the ADC Driver.
* Default: `10`
* `driver` <Badge type="info">String</Badge> <Badge>Required</Badge>
* The driver to use. Must be one of `adc`, `custom`, `vendor`.
* `sample_interval` <Badge type="info">Number</Badge>
* The delay between sampling the battery in milliseconds.
* Default: `30000` (30 s)
## Wireless/Bluetooth {#bluetooth}
Configures the [Wireless](features/wireless) feature.