summaryrefslogtreecommitdiff
path: root/wasp/boards/pinetime/watch.py.in
blob: 058cdfdca625229d4bcd2302881cb1198e36aa71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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.bma421 import BMA421
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)

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")
button = Pin('BUTTON', Pin.IN)
safe_mode = button.value()
if safe_mode:
    backlight.set(1)
    time.sleep(1)

try:
    # 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)
    touch = CST816S(i2c, Pin('TP_INT', Pin.IN), Pin('TP_RST', Pin.OUT, value=0))
    vibrator = Vibrator(Pin('MOTOR', Pin.OUT, value=0), active_low=True)

    # Release flash from deep power-down
    boot_msg("Wake SPINOR")
    nor_cs = Pin('NOR_CS', Pin.OUT, value=1)
    nor_cs(0)
    spi.write('\xAB')
    nor_cs(1)

    # Mount the filesystem
    boot_msg("Init SPINOR")
    flash = FLASH(spi, (Pin('NOR_CS', Pin.OUT, value=1),))
    try:
        boot_msg("Mount FS")
        os.mount(flash, '/flash')
    except AttributeError:
        # 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('''\
#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 safe_mode:
        boot_msg("Enter /flash")
        os.chdir('/flash')
        boot_msg("Run main.py")
    else:
        boot_msg("Safe mode")
except:
    drawable.string("FAILED", 0, 136, width=240)
    backlight.set(1)