1
0
Fork 0

wasp: Add battery support

pull/5/head
Daniel Thompson 2020-01-30 21:46:35 +00:00
parent ef5d805c51
commit a34d65d7fd
5 changed files with 70 additions and 2 deletions

View File

@ -36,5 +36,5 @@ The TODO list helps keep track on progress towards that goal. It is not
- [ ] Optimized RLE inner loops
* [X] Backlight driver
* [ ] Button driver (interrupt based)
* [ ] Battery/charger driver
* [X] Battery/charger driver
* [ ] Simple clock and battery level application

View File

@ -2,6 +2,8 @@ freeze('$(MPY_DIR)/../wasp',
(
'boot.py',
'demo.py',
'drivers/battery.py',
'drivers/signal.py',
'drivers/st7789.py',
'logo.py',
'pinetime.py'

View File

@ -0,0 +1,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

View File

@ -0,0 +1,23 @@
class Signal(object):
'''Simplified Signal class
Note: The normal C implementation isn't working for the NRF port
'''
def __init__(self, pin, invert=False):
self.pin = pin
self.invert = invert
def __call__(self, v=None):
return self.value(v)
def value(self, v=None):
if v == None:
return self.invert ^ self.pin.value()
self.pin.value(self.invert ^ bool(v))
def on(self):
self.value(1)
def off(self):
self.value(0)

View File

@ -1,6 +1,9 @@
from machine import Pin
#from machine import Signal
from machine import SPI
from drivers.battery import Battery
from drivers.signal import Signal
from drivers.st7789 import ST7789_SPI
class Display(ST7789_SPI):
@ -42,5 +45,9 @@ class Backlight(object):
backlight = Backlight(0)
display = Display()
backlight.set(1)
battery = Battery(
Pin('BATTERY', Pin.IN),
Signal(Pin('CHARGING', Pin.IN), invert=True),
Signal(Pin('USB_PWR', Pin.IN), invert=True))