1package battery
2
3import (
4 "machine"
5)
6
7var adc machine.ADC
8
9func Init(bPin machine.Pin) {
10 adc = machine.ADC{
11 Pin: bPin,
12 }
13 adc.Configure(machine.ADCConfig{})
14}
15
16func GetVoltage() float64 {
17 return float64(adc.Get()) * 2 * 3.6 / 65535 // double-100K resistor divider
18}
19
20func GetPercentage() (bool, float64) {
21 ratio := (GetVoltage() - 3.6) / 0.6
22 if ratio > 1.5 {
23 return true, ratio * 100
24 }
25 return false, ratio * 100 // Non-linear, fix this later.
26}