qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

battery.c (988B)


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