Files
MSE-PI-E2EEDA-Plein-de-eeee…/nodes/src/battery_percent.c
2026-06-04 21:01:43 +02:00

43 lines
1.2 KiB
C

#include "battery_percent.h"
/**
* Taken from sample/board/nordic/battery/src/main.c
* A discharge curve specific to the power source.
*/
static const struct battery_level_point levels[] = {
/* "Curve" here eyeballed from captured data for the [Adafruit
* 3.7v 2000 mAh](https://www.adafruit.com/product/2011) LIPO
* under full load that started with a charge of 3.96 V and
* dropped about linearly to 3.58 V over 15 hours. It then
* dropped rapidly to 3.10 V over one hour, at which point it
* stopped transmitting.
*
* Based on eyeball comparisons we'll say that 15/16 of life
* goes between 3.95 and 3.55 V, and 1/16 goes between 3.55 V
* and 3.1 V.
*/
{ 10000, 3950 },
{ 625, 3550 },
{ 0, 3100 },
};
enum error_code battery_init(){
enum error_code ret = init_failed;
if(0 == battery_measure_enable(true)){
ret = success;
}else{}
return ret;
}
enum error_code battery_get_value(int* holder){
enum error_code ret = read_failed;
// battery_voltage in [mV]
int battery_voltage = battery_sample();
if(battery_voltage >= 0){
*holder = (battery_level_pptt(battery_voltage, levels)/100);
ret = success;
}else{}
return ret;
}