From a34d65d7fdfbbaaa3e6484dfebe914378f478ca2 Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Thu, 30 Jan 2020 21:46:35 +0000 Subject: [PATCH] wasp: Add battery support --- TODO.md | 2 +- manifest/pinetime.py | 2 ++ wasp/drivers/battery.py | 36 ++++++++++++++++++++++++++++++++++++ wasp/drivers/signal.py | 23 +++++++++++++++++++++++ wasp/pinetime.py | 9 ++++++++- 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 wasp/drivers/battery.py create mode 100644 wasp/drivers/signal.py diff --git a/TODO.md b/TODO.md index 9def553..f6dfdcf 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/manifest/pinetime.py b/manifest/pinetime.py index f622878..7175d74 100644 --- a/manifest/pinetime.py +++ b/manifest/pinetime.py @@ -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' diff --git a/wasp/drivers/battery.py b/wasp/drivers/battery.py new file mode 100644 index 0000000..cc1eafb --- /dev/null +++ b/wasp/drivers/battery.py @@ -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 diff --git a/wasp/drivers/signal.py b/wasp/drivers/signal.py new file mode 100644 index 0000000..18ce8bf --- /dev/null +++ b/wasp/drivers/signal.py @@ -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) diff --git a/wasp/pinetime.py b/wasp/pinetime.py index 1a771aa..730eebc 100644 --- a/wasp/pinetime.py +++ b/wasp/pinetime.py @@ -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))