summaryrefslogtreecommitdiff
path: root/wasp/drivers/battery.py
blob: cc1eafbea00d79fd516dbb983cdfcdd603805314 (plain)
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
# Generic lithium ion battery driver

from machine import Pin, ADC

class Battery(object):
    def __init__(self, battery, charging, power=None):
        self._battery = ADC(battery)
        self._charging = charging
        self._power = power

    def charging(self):
        return self._charging.value() 

    def power(self):
        if self._power:
            return self._power.value()
        return self._charging.value()

    def voltage_mv(self):
        # Assumes a 50/50 voltage divider and a 3.3v power supply
        raw = self._battery.read_u16()
        return (2 * 3300 * raw) // 65535

    def level(self):
        # This is a trivial battery level estimation approach. It is assumes
        # the discharge from 4v to 3.5v is roughly linear and 4v is 100% and
        # that 3.5v is 5%. Below 3.5v the voltage will start to drop pretty
        # sharply to we will drop from 5% to 0% pretty fast... but we'll
        # live with that for now.
        mv = self.voltage_mv()
        level = ((19 * mv) // 100) - 660
        if level > 100:
            return 100
        if level < 0:
            return 0
        return level