From b124a747dd6097a7ca6f111da46067b7d86abe5b Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Mon, 3 Feb 2020 22:29:57 +0000 Subject: [PATCH] wasp: simulator: Add battery level simulation In order to get best test coverage the act of reading the battery results in the battery either charging or discharging. --- wasp/boards/simulator/watch.py | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/wasp/boards/simulator/watch.py b/wasp/boards/simulator/watch.py index e25d03c..75bd232 100644 --- a/wasp/boards/simulator/watch.py +++ b/wasp/boards/simulator/watch.py @@ -29,6 +29,40 @@ class Display(ST7789_SPI): super().__init__(240, 240, spi, cs=cs, dc=dc, res=rst) +class Battery(object): + def __init__(self): + self.voltage = 3.9 + self.step = -0.01 + self.powered = False + + def charging(self): + self.voltage_mv() + return self.powered + + def power(self): + self.voltage_mv() + return self.powered + + def voltage_mv(self): + if self.voltage > 4: + self.step = -0.005 + self.powered = False + elif self.voltage < 3.4: + self.step = 0.01 + self.powered = True + self.voltage += self.step + + return int(self.voltage * 1000) + + def level(self): + mv = self.voltage_mv() + level = ((19 * mv) // 100) - 660 + if level > 100: + return 100 + if level < 0: + return 0 + return level + class RTC(object): def __init__(self): self.uptime = 0 @@ -49,6 +83,7 @@ class RTC(object): display = Display() backlight = Backlight() +battery = Battery() rtc = RTC() vibrator = Vibrator(Pin('MOTOR', Pin.OUT, value=0), active_low=True) button = Pin('BUTTON', Pin.IN, quiet=True)