logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

battery.c (988B)


  1. // Copyright 2025 QMK
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "battery_driver.h"
  4. #include "battery.h"
  5. #include "timer.h"
  6. #ifndef BATTERY_SAMPLE_INTERVAL
  7. # define BATTERY_SAMPLE_INTERVAL 30000
  8. #endif
  9. static uint8_t last_bat_level = 100;
  10. void battery_init(void) {
  11. battery_driver_init();
  12. last_bat_level = battery_driver_sample_percent();
  13. }
  14. __attribute__((weak)) void battery_percent_changed_user(uint8_t level) {}
  15. __attribute__((weak)) void battery_percent_changed_kb(uint8_t level) {}
  16. static void handle_percent_changed(void) {
  17. battery_percent_changed_user(last_bat_level);
  18. battery_percent_changed_kb(last_bat_level);
  19. }
  20. void battery_task(void) {
  21. static uint32_t bat_timer = 0;
  22. if (timer_elapsed32(bat_timer) > BATTERY_SAMPLE_INTERVAL) {
  23. last_bat_level = battery_driver_sample_percent();
  24. handle_percent_changed();
  25. bat_timer = timer_read32();
  26. }
  27. }
  28. uint8_t battery_get_percent(void) {
  29. return last_bat_level;
  30. }