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

@ -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;
}

View file

@ -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);
/** \} */

View file

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