2020-03-22 15:40:18 +00:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-06-30 23:04:01 +01:00
|
|
|
def nop():
|
|
|
|
pass
|
|
|
|
schedule = nop
|
|
|
|
def _callback(obj):
|
|
|
|
schedule()
|
|
|
|
|
2020-02-09 19:49:52 +00:00
|
|
|
# Start measuring time (and feeding the watchdog) before *anything* else
|
2020-02-03 19:12:04 +00:00
|
|
|
from machine import RTCounter
|
2020-02-09 19:49:52 +00:00
|
|
|
from drivers.nrf_rtc import RTC
|
2020-06-30 23:04:01 +01:00
|
|
|
rtc = RTC(RTCounter(1, mode=RTCounter.PERIODIC, period=1, callback=_callback))
|
2020-02-09 19:49:52 +00:00
|
|
|
rtc.counter.start()
|
|
|
|
|
2020-12-12 21:35:45 +00:00
|
|
|
import gc
|
2020-02-09 19:49:52 +00:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2020-03-08 23:16:30 +00:00
|
|
|
import draw565
|
|
|
|
|
2020-03-07 11:48:17 +00:00
|
|
|
from machine import I2C
|
2020-02-09 19:49:52 +00:00
|
|
|
from machine import Pin
|
2020-01-30 21:46:35 +00:00
|
|
|
#from machine import Signal
|
2020-01-21 22:10:50 +00:00
|
|
|
from machine import SPI
|
|
|
|
|
2020-01-30 21:46:35 +00:00
|
|
|
from drivers.battery import Battery
|
2020-06-09 21:29:00 +01:00
|
|
|
from drivers.bma421 import BMA421
|
2020-03-07 11:48:17 +00:00
|
|
|
from drivers.cst816s import CST816S
|
2020-06-22 22:22:40 +01:00
|
|
|
from drivers.hrs3300 import HRS3300
|
2020-01-30 21:46:35 +00:00
|
|
|
from drivers.signal import Signal
|
2020-01-23 18:59:43 +00:00
|
|
|
from drivers.st7789 import ST7789_SPI
|
2020-01-31 19:24:33 +00:00
|
|
|
from drivers.vibrator import Vibrator
|
2020-02-09 19:49:52 +00:00
|
|
|
from flash.flash_spi import FLASH
|
2020-01-29 17:30:57 +00:00
|
|
|
|
2020-07-24 17:04:46 +01:00
|
|
|
from ubluepy import uart_connected as connected
|
|
|
|
|
2020-01-29 17:30:57 +00:00
|
|
|
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)
|
|
|
|
|
2020-02-09 19:49:52 +00:00
|
|
|
# Setup the display (and manage the backlight)
|
2020-01-29 17:30:57 +00:00
|
|
|
backlight = Backlight(0)
|
2020-02-09 19:49:52 +00:00
|
|
|
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))
|
2020-03-08 23:16:30 +00:00
|
|
|
drawable = draw565.Draw565(display)
|
2020-02-01 13:42:11 +00:00
|
|
|
|
2020-06-10 08:52:46 +01:00
|
|
|
def boot_msg(s):
|
|
|
|
drawable.string(s, 0, 108, width=240)
|
|
|
|
if safe_mode:
|
|
|
|
time.sleep_ms(500)
|
|
|
|
|
|
|
|
safe_mode = False
|
|
|
|
boot_msg("Init button")
|
2020-02-03 19:12:04 +00:00
|
|
|
button = Pin('BUTTON', Pin.IN)
|
2020-06-10 08:52:46 +01:00
|
|
|
safe_mode = button.value()
|
|
|
|
if safe_mode:
|
|
|
|
backlight.set(1)
|
|
|
|
time.sleep(1)
|
|
|
|
|
2020-02-09 19:49:52 +00:00
|
|
|
try:
|
2020-06-10 08:52:46 +01:00
|
|
|
# Setup the last few bits and pieces
|
|
|
|
boot_msg("Init hardware")
|
|
|
|
battery = Battery(
|
|
|
|
Pin('BATTERY', Pin.IN),
|
|
|
|
Signal(Pin('CHARGING', Pin.IN), invert=True),
|
|
|
|
Signal(Pin('USB_PWR', Pin.IN), invert=True))
|
|
|
|
i2c = I2C(1, scl='I2C_SCL', sda='I2C_SDA')
|
|
|
|
accel = BMA421(i2c)
|
2020-06-22 22:22:40 +01:00
|
|
|
hrs = HRS3300(i2c)
|
2020-06-30 23:04:01 +01:00
|
|
|
touch = CST816S(i2c,
|
|
|
|
Pin('TP_INT', Pin.IN), Pin('TP_RST', Pin.OUT, value=0),
|
|
|
|
_callback)
|
2020-06-10 08:52:46 +01:00
|
|
|
vibrator = Vibrator(Pin('MOTOR', Pin.OUT, value=0), active_low=True)
|
|
|
|
|
|
|
|
# Mount the filesystem
|
|
|
|
boot_msg("Init SPINOR")
|
2020-10-10 11:30:32 +01:00
|
|
|
flash = FLASH(spi, (Pin('NOR_CS', Pin.OUT, value=1),))
|
2020-06-10 08:52:46 +01:00
|
|
|
try:
|
|
|
|
boot_msg("Mount FS")
|
|
|
|
os.mount(flash, '/flash')
|
2021-08-31 21:21:21 +01:00
|
|
|
except (AttributeError, OSError) as e:
|
2020-06-10 08:52:46 +01:00
|
|
|
# Format the filesystem (and provide a default version of main.py)
|
|
|
|
boot_msg("Format FS")
|
|
|
|
os.VfsLfs2.mkfs(flash)
|
|
|
|
boot_msg("Retry mount FS")
|
|
|
|
os.mount(flash,'/flash')
|
|
|
|
boot_msg("Write main.py")
|
|
|
|
with open('/flash/main.py', 'w') as f:
|
|
|
|
f.write('''\
|
2020-04-11 21:12:18 +01:00
|
|
|
#include('main.py')
|
2020-02-09 19:49:52 +00:00
|
|
|
''')
|
|
|
|
|
2020-06-10 08:52:46 +01:00
|
|
|
# Only change directory if the button is not pressed (this will
|
|
|
|
# allow us access to fix any problems with main.py)!
|
|
|
|
if not safe_mode:
|
|
|
|
boot_msg("Enter /flash")
|
|
|
|
os.chdir('/flash')
|
2020-11-14 20:40:16 +00:00
|
|
|
boot_msg("main.py")
|
2020-06-10 08:52:46 +01:00
|
|
|
else:
|
|
|
|
boot_msg("Safe mode")
|
|
|
|
except:
|
|
|
|
drawable.string("FAILED", 0, 136, width=240)
|
2020-11-14 20:40:16 +00:00
|
|
|
backlight.set(1)
|
2020-12-12 21:35:45 +00:00
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
free = gc.mem_free()
|