From 50ecff29efb5afd4796ea192982bba10266947fe Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Sat, 11 Apr 2020 21:12:18 +0100 Subject: wasp: Automatically generate watch.py for PineTime This should ensure that main.py is always up to date. diff --git a/.gitignore b/.gitignore index 16efb0b..e6f30d3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ __pycache__ docs/build attic/ +wasp/boards/pinetime/watch.py diff --git a/Makefile b/Makefile index 82e6366..f0fd8ff 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ bootloader: softdevice: micropython/ports/nrf/drivers/bluetooth/download_ble_stack.sh -micropython: +micropython: wasp/boards/pinetime/watch.py $(MAKE) -C micropython/mpy-cross $(RM) micropython/ports/nrf/build-$(BOARD)-s132/frozen_content.c $(MAKE) -C micropython/ports/nrf \ @@ -39,6 +39,10 @@ micropython: --application micropython/ports/nrf/build-$(BOARD)-s132/firmware.hex \ micropython.zip +wasp/boards/pinetime/watch.py : wasp/boards/pinetime/watch.py.in + (cd wasp; ../tools/preprocess.py boards/pinetime/watch.py.in > \ + boards/pinetime/watch.py) + dfu: python3 -m nordicsemi dfu serial --package micropython.zip --port /dev/ttyACM0 diff --git a/tools/preprocess.py b/tools/preprocess.py new file mode 100755 index 0000000..4c43c2a --- /dev/null +++ b/tools/preprocess.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +# SPDX-License-Identifier: LGPL-3.0-or-later +# Copyright (C) 2020 Daniel Thompson +"""Quick and dirty macro processor. + +Currently the only support macro is #include! +""" + +import sys + +def preprocess(fname): + with open(fname) as f: + for ln in f.readlines(): + ln = ln.rstrip() + + macro = ln.lstrip() + if macro.startswith('#include'): + exec(macro[1:]) + else: + print(ln) + +def include(fname): + preprocess(fname) + +for arg in sys.argv[1:]: + preprocess(arg) diff --git a/wasp/boards/pinetime/watch.py b/wasp/boards/pinetime/watch.py deleted file mode 100644 index 1caeea5..0000000 --- a/wasp/boards/pinetime/watch.py +++ /dev/null @@ -1,99 +0,0 @@ -# SPDX-License-Identifier: LGPL-3.0-or-later -# Copyright (C) 2020 Daniel Thompson - -# Start measuring time (and feeding the watchdog) before *anything* else -from machine import RTCounter -from drivers.nrf_rtc import RTC -rtc = RTC(RTCounter(1, mode=RTCounter.PERIODIC)) -rtc.counter.start() - -import os -import time - -import draw565 - -from machine import I2C -from machine import Pin -#from machine import Signal -from machine import SPI - -from drivers.battery import Battery -from drivers.cst816s import CST816S -from drivers.signal import Signal -from drivers.st7789 import ST7789_SPI -from drivers.vibrator import Vibrator -from flash.flash_spi import FLASH - -class Backlight(object): - lo = Pin("BL_LO", Pin.OUT, value=0) - mid = Pin("BL_MID", Pin.OUT, value=1) - hi = Pin("BL_HI", Pin.OUT, value=1) - - def __init__(self, level=1): - self.set(level) - - def set(self, level): - hi = 1 - mid = 1 - lo = 1 - - if level >= 3: - hi = 0 - elif level == 2: - mid = 0 - elif level == 1: - lo = 0 - - self.hi(hi) - self.mid(mid) - self.lo(lo) - -# Setup the display (and manage the backlight) -backlight = Backlight(0) -spi = SPI(0) -spi.init(polarity=1, phase=1, baudrate=8000000) -display = ST7789_SPI(240, 240, spi, - cs=Pin("DISP_CS", Pin.OUT), - dc=Pin("DISP_DC", Pin.OUT), - res=Pin("DISP_RST", Pin.OUT)) -drawable = draw565.Draw565(display) - -# Setup the last few bits and pieces -battery = Battery( - Pin('BATTERY', Pin.IN), - Signal(Pin('CHARGING', Pin.IN), invert=True), - Signal(Pin('USB_PWR', Pin.IN), invert=True)) -button = Pin('BUTTON', Pin.IN) -i2c = I2C(1, scl='I2C_SCL', sda='I2C_SDA') -touch = CST816S(i2c) -vibrator = Vibrator(Pin('MOTOR', Pin.OUT, value=0), active_low=True) - -# Release flash from deep power-down -nor_cs = Pin('NOR_CS', Pin.OUT, value=1) -nor_cs(0) -spi.write('\xAB') -nor_cs(1) - -# Mount the filesystem -flash = FLASH(spi, (Pin('NOR_CS', Pin.OUT, value=1),)) -try: - os.mount(flash, '/flash') -except AttributeError: - # Format the filesystem (and provide a default version of main.py) - os.VfsLfs2.mkfs(flash) - os.mount(flash,'/flash') - with open('/flash/main.py', 'w') as f: - f.write('''\ -import manager -wasp = manager.Manager(watch) -wasp.run() -''') - -# Only change directory if the button is not pressed (this will -# allow us access to fix any problems with main.py)! -if not button.value(): - os.chdir('/flash') - backlight.set(1) -else: - display.poweroff() - diff --git a/wasp/boards/pinetime/watch.py.in b/wasp/boards/pinetime/watch.py.in new file mode 100644 index 0000000..46fc6f6 --- /dev/null +++ b/wasp/boards/pinetime/watch.py.in @@ -0,0 +1,97 @@ +# SPDX-License-Identifier: LGPL-3.0-or-later +# Copyright (C) 2020 Daniel Thompson + +# Start measuring time (and feeding the watchdog) before *anything* else +from machine import RTCounter +from drivers.nrf_rtc import RTC +rtc = RTC(RTCounter(1, mode=RTCounter.PERIODIC)) +rtc.counter.start() + +import os +import time + +import draw565 + +from machine import I2C +from machine import Pin +#from machine import Signal +from machine import SPI + +from drivers.battery import Battery +from drivers.cst816s import CST816S +from drivers.signal import Signal +from drivers.st7789 import ST7789_SPI +from drivers.vibrator import Vibrator +from flash.flash_spi import FLASH + +class Backlight(object): + lo = Pin("BL_LO", Pin.OUT, value=0) + mid = Pin("BL_MID", Pin.OUT, value=1) + hi = Pin("BL_HI", Pin.OUT, value=1) + + def __init__(self, level=1): + self.set(level) + + def set(self, level): + hi = 1 + mid = 1 + lo = 1 + + if level >= 3: + hi = 0 + elif level == 2: + mid = 0 + elif level == 1: + lo = 0 + + self.hi(hi) + self.mid(mid) + self.lo(lo) + +# Setup the display (and manage the backlight) +backlight = Backlight(0) +spi = SPI(0) +spi.init(polarity=1, phase=1, baudrate=8000000) +display = ST7789_SPI(240, 240, spi, + cs=Pin("DISP_CS", Pin.OUT), + dc=Pin("DISP_DC", Pin.OUT), + res=Pin("DISP_RST", Pin.OUT)) +drawable = draw565.Draw565(display) + +# Setup the last few bits and pieces +battery = Battery( + Pin('BATTERY', Pin.IN), + Signal(Pin('CHARGING', Pin.IN), invert=True), + Signal(Pin('USB_PWR', Pin.IN), invert=True)) +button = Pin('BUTTON', Pin.IN) +i2c = I2C(1, scl='I2C_SCL', sda='I2C_SDA') +touch = CST816S(i2c) +vibrator = Vibrator(Pin('MOTOR', Pin.OUT, value=0), active_low=True) + +# Release flash from deep power-down +nor_cs = Pin('NOR_CS', Pin.OUT, value=1) +nor_cs(0) +spi.write('\xAB') +nor_cs(1) + +# Mount the filesystem +flash = FLASH(spi, (Pin('NOR_CS', Pin.OUT, value=1),)) +try: + os.mount(flash, '/flash') +except AttributeError: + # Format the filesystem (and provide a default version of main.py) + os.VfsLfs2.mkfs(flash) + os.mount(flash,'/flash') + with open('/flash/main.py', 'w') as f: + f.write('''\ +#include('main.py') +''') + +# Only change directory if the button is not pressed (this will +# allow us access to fix any problems with main.py)! +if not button.value(): + os.chdir('/flash') + backlight.set(1) +else: + display.poweroff() + -- cgit v0.10.2