diff options
| author | Joel Challis | 2025-08-17 02:14:48 +0200 |
|---|---|---|
| committer | GitHub | 2025-08-17 02:14:48 +0200 |
| commit | cc696a2ae838a9639335ca8eb3cb3b794c06bc33 (patch) | |
| tree | 901b54bda536acb5503c6cf924b0f30bca1a174e /drivers | |
| parent | f29d8117bf877a4df1f88f40e0131f4465748540 (diff) | |
Refactor battery driver (#25550)
Diffstat (limited to 'drivers')
| -rw-r--r-- | drivers/battery/battery.c | 41 | ||||
| -rw-r--r-- | drivers/battery/battery.h | 46 | ||||
| -rw-r--r-- | drivers/battery/battery_adc.c | 25 |
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 @@ -// Copyright 2025 QMK -// SPDX-License-Identifier: GPL-2.0-or-later - -#include "battery_driver.h" -#include "battery.h" -#include "timer.h" - -#ifndef BATTERY_SAMPLE_INTERVAL -# define BATTERY_SAMPLE_INTERVAL 30000 -#endif - -static uint8_t last_bat_level = 100; - -void battery_init(void) { - battery_driver_init(); - - last_bat_level = battery_driver_sample_percent(); -} - -__attribute__((weak)) void battery_percent_changed_user(uint8_t level) {} -__attribute__((weak)) void battery_percent_changed_kb(uint8_t level) {} - -static void handle_percent_changed(void) { - battery_percent_changed_user(last_bat_level); - battery_percent_changed_kb(last_bat_level); -} - -void battery_task(void) { - static uint32_t bat_timer = 0; - if (timer_elapsed32(bat_timer) > BATTERY_SAMPLE_INTERVAL) { - last_bat_level = battery_driver_sample_percent(); - - handle_percent_changed(); - - bat_timer = timer_read32(); - } -} - -uint8_t battery_get_percent(void) { - return last_bat_level; -} 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 @@ -// Copyright 2025 QMK -// SPDX-License-Identifier: GPL-2.0-or-later - -#pragma once - -#include <stdint.h> - -/** - * \file - * - * \defgroup battery Battery API - * - * \brief API to query battery status. - * \{ - */ - -/** - * \brief Initialize the battery driver. - */ -void battery_init(void); - -/** - * \brief Perform housekeeping tasks. - */ -void battery_task(void); - -/** - * \brief Sample battery level. - * - * \return The battery percentage, in the range 0-100. - */ -uint8_t battery_get_percent(void); - -/** - * \brief user hook called when battery level changed. - * - */ -void battery_percent_changed_user(uint8_t level); - -/** - * \brief keyboard hook called when battery level changed. - * - */ -void battery_percent_changed_kb(uint8_t level); - -/** \} */ 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 @@ // Copyright 2025 QMK // SPDX-License-Identifier: GPL-2.0-or-later +#include "battery_driver.h" #include "analog.h" #include "gpio.h" -#ifndef BATTERY_PIN -# error("BATTERY_PIN not configured!") +#ifndef BATTERY_ADC_PIN +# error("BATTERY_ADC_PIN not configured!") #endif -#ifndef BATTERY_REF_VOLTAGE_MV -# define BATTERY_REF_VOLTAGE_MV 3300 +#ifndef BATTERY_ADC_REF_VOLTAGE_MV +# define BATTERY_ADC_REF_VOLTAGE_MV 3300 #endif -#ifndef BATTERY_VOLTAGE_DIVIDER_R1 +#ifndef BATTERY_ADC_VOLTAGE_DIVIDER_R1 # define BATTERY_VOLTAGE_DIVIDER_R1 100 #endif -#ifndef BATTERY_VOLTAGE_DIVIDER_R2 -# define BATTERY_VOLTAGE_DIVIDER_R2 100 +#ifndef BATTERY_ADC_VOLTAGE_DIVIDER_R2 +# define BATTERY_ADC_VOLTAGE_DIVIDER_R2 100 #endif // TODO: infer from adc config? @@ -26,16 +27,16 @@ #endif void battery_driver_init(void) { - gpio_set_pin_input(BATTERY_PIN); + gpio_set_pin_input(BATTERY_ADC_PIN); } uint16_t battery_driver_get_mv(void) { - uint32_t raw = analogReadPin(BATTERY_PIN); + uint32_t raw = analogReadPin(BATTERY_ADC_PIN); - uint32_t bat_mv = raw * BATTERY_REF_VOLTAGE_MV / (1 << BATTERY_ADC_RESOLUTION); + uint32_t bat_mv = raw * BATTERY_ADC_REF_VOLTAGE_MV / (1 << BATTERY_ADC_RESOLUTION); -#if BATTERY_VOLTAGE_DIVIDER_R1 > 0 && BATTERY_VOLTAGE_DIVIDER_R2 > 0 - bat_mv = bat_mv * (BATTERY_VOLTAGE_DIVIDER_R1 + BATTERY_VOLTAGE_DIVIDER_R2) / BATTERY_VOLTAGE_DIVIDER_R2; +#if BATTERY_VOLTAGE_DIVIDER_R1 > 0 && BATTERY_ADC_VOLTAGE_DIVIDER_R2 > 0 + bat_mv = bat_mv * (BATTERY_VOLTAGE_DIVIDER_R1 + BATTERY_ADC_VOLTAGE_DIVIDER_R2) / BATTERY_ADC_VOLTAGE_DIVIDER_R2; #endif return bat_mv; |