-
Notifications
You must be signed in to change notification settings - Fork 1
/
battery-check.c
52 lines (41 loc) · 1.34 KB
/
battery-check.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* RP2040 Battery check
* Measures the voltage level on the VSYS pin of a Raspberry Pi Pico.
* Useful as a low-battery indicator.
* By Turi Scandurra – https://turiscandurra.com/circuits
* 2023.11.28 - v1.2.0
*/
#include "battery-check.h"
#include "hardware/adc.h"
uint16_t low_batt_threshold = LOW_BATT_THRESHOLD_DEFAULT;
Callbacks callbacks;
void battery_check_init(int delay_ms, void* repeat_callback, void* low_callback){
adc_gpio_init(PIN_BATT_LVL);
callbacks.repeat = repeat_callback;
callbacks.low = low_callback;
if (delay_ms > 0){
// Non-time-critical routine, run by timer
add_repeating_timer_ms(delay_ms, battery_check_task, &callbacks, &battery_check_timer);
}
}
static bool battery_check_task(repeating_timer_t *rt, void *user_data) {
uint16_t battery_mv = battery_check();
if(callbacks.repeat){
callbacks.repeat(battery_mv);
}
if(callbacks.low && battery_mv < low_batt_threshold){
callbacks.low(battery_mv);
}
return true;
}
void battery_set_threshold(uint16_t mv){
low_batt_threshold = mv;
}
void battery_check_stop(){
cancel_repeating_timer(&battery_check_timer);
}
uint16_t battery_check(){
adc_select_input(3); // VSYS sense input
// Coefficients from https://github.com/elehobica/pico_battery_op/
return adc_read() * 9875 / (1<<12) - 20;
}