From 262d93c76cca638a832cf21cc44393df31bc7940 Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Fri, 31 Jan 2020 19:36:55 +0000 Subject: [PATCH] wasp: simulator: First steps towards a simulator Currently this just traces SPI activity from the ST7789 driver but its a good baseline to start building up test functions from. --- Makefile | 5 +++ wasp/boards/simulator/machine.py | 54 ++++++++++++++++++++++++++++ wasp/boards/simulator/micropython.py | 5 +++ wasp/boards/simulator/watch.py | 26 ++++++++++++++ wasp/drivers/st7789.py | 2 ++ 5 files changed, 92 insertions(+) create mode 100644 wasp/boards/simulator/machine.py create mode 100644 wasp/boards/simulator/micropython.py create mode 100644 wasp/boards/simulator/watch.py diff --git a/Makefile b/Makefile index 6cc89a3..c990d14 100644 --- a/Makefile +++ b/Makefile @@ -53,5 +53,10 @@ debug: -ex "attach 1" \ -ex "load" +sim: + PYTHONDONTWRITEBYTECODE=1 \ + PYTHONPATH=$(PWD)/wasp/boards/simulator:$(PWD)/wasp \ + python3 -i wasp/boot.py + .PHONY: bootloader micropython diff --git a/wasp/boards/simulator/machine.py b/wasp/boards/simulator/machine.py new file mode 100644 index 0000000..2c93770 --- /dev/null +++ b/wasp/boards/simulator/machine.py @@ -0,0 +1,54 @@ +class Tracer(object): + def __init__(self, *args, **kwargs): + print(f'{self.__class__.__name__}.__init__{args} {kwargs}') + + def __getattr__(self, name): + if name.upper() == name: + return name + return lambda *args, **kwargs: print(f'{self.__class__.__name__}.{name}{args} {kwargs}') + +class ADC(Tracer): + pass + +class Pin(object): + IN = 'IN' + OUT = 'OUT' + + def __init__(self, id, direction, value=1): + self._id = id + self._value = 0 + + def init(self, d, value): + self.value(value) + + def on(self): + self.value(1) + + def off(self): + self.value(0) + + def value(self, v=None): + if v is None: + return self._value + if v: + print(self._id + ": on") + self._value = False + else: + print(self._id + ": off") + self._value = True + + def __call__(self, v=None): + self.value(v) + +class PWM(Tracer): + FREQ_16MHZ = 'FREQ_16MHZ' + +class SPI(object): + def __init__(self, id): + self._id = id + + def init(self, baudrate=1000000, polarity=0, phase=0, bits=8, sck=None, mosi=None, miso=None): + pass + + def write(self, buf): + print("Sending data: " + str(buf)) diff --git a/wasp/boards/simulator/micropython.py b/wasp/boards/simulator/micropython.py new file mode 100644 index 0000000..a5740f1 --- /dev/null +++ b/wasp/boards/simulator/micropython.py @@ -0,0 +1,5 @@ +def const(x): + return x + +def native(x): + return x diff --git a/wasp/boards/simulator/watch.py b/wasp/boards/simulator/watch.py new file mode 100644 index 0000000..c6d695f --- /dev/null +++ b/wasp/boards/simulator/watch.py @@ -0,0 +1,26 @@ +import time +def sleep_ms(ms): + time.sleep(ms / 1000) +time.sleep_ms = sleep_ms + +from machine import Pin +from machine import SPI + +from drivers.st7789 import ST7789_SPI +from drivers.vibrator import Vibrator + +class Display(ST7789_SPI): + def __init__(self): + spi = SPI(0) + # Mode 3, maximum clock speed! + spi.init(polarity=1, phase=1, baudrate=8000000) + + # Configure the display + cs = Pin("DISP_CS", Pin.OUT) + dc = Pin("DISP_DC", Pin.OUT) + rst = Pin("DISP_RST", Pin.OUT) + + super().__init__(240, 240, spi, cs=cs, dc=dc, res=rst) + +display = Display() +vibrator = Vibrator(Pin('MOTOR', Pin.OUT, value=0), active_low=True) diff --git a/wasp/drivers/st7789.py b/wasp/drivers/st7789.py index cea14d7..9236790 100644 --- a/wasp/drivers/st7789.py +++ b/wasp/drivers/st7789.py @@ -1,5 +1,7 @@ # MicroPython ST7789 display driver, currently only has an SPI interface +import micropython + from micropython import const from time import sleep_ms