summaryrefslogtreecommitdiff
path: root/wasp/apps/clock.py
blob: cde39a2dc407a682e422f8931703df9bbfb466d2 (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
import fonts.clock as digits
import watch
import widgets
import manager

DIGITS = (
        digits.clock_0,
        digits.clock_1,
        digits.clock_2,
        digits.clock_3,
        digits.clock_4,
        digits.clock_5,
        digits.clock_6,
        digits.clock_7,
        digits.clock_8,
        digits.clock_9
)

MONTH = 'JanFebMarAprMayJunJulAugSepOctNovDec'

class ClockApp(object):
    """Simple digital clock application.

    Shows a time (as HH:MM) together with a battery meter and the date.
    """

    def __init__(self):
        self.meter = widgets.BatteryMeter()

    def handle_event(self, event_view):
        """Process events that the app is subscribed to."""
        if event_view[0] == manager.EVENT_TICK:
            self.update()
        else:
            # TODO: Raise an unexpected event exception
            pass

    def foreground(self, manager, effect=None):
        """Activate the application."""
        self.on_screen = ( -1, -1, -1, -1, -1, -1 )
        self.draw(effect)
        manager.request_tick(1000)

    def tick(self, ticks):
        self.update()

    def background(self):
        """De-activate the application (without losing state)."""
        pass

    def sleep(self):
        return True

    def wake(self):
        self.update()

    def draw(self, effect=None):
        """Redraw the display from scratch."""
        display = watch.display

        display.fill(0)
        display.rleblit(digits.clock_colon, pos=(2*48, 80), fg=0xb5b6)
        self.on_screen = ( -1, -1, -1, -1, -1, -1 )
        self.update()
        self.meter.draw()

    def update(self):
        """Update the display (if needed).

        The updates are a lazy as possible and rely on an prior call to
        draw() to ensure the screen is suitably prepared.
        """
        now = watch.rtc.get_localtime()
        if now[3] == self.on_screen[3] and now[4] == self.on_screen[4]:
            if now[5] != self.on_screen[5]:
                self.meter.update()
                self.on_screen = now
            return False

        display = watch.display
        display.rleblit(DIGITS[now[4]  % 10], pos=(4*48, 80))
        display.rleblit(DIGITS[now[4] // 10], pos=(3*48, 80), fg=0xbdb6)
        display.rleblit(DIGITS[now[3]  % 10], pos=(1*48, 80))
        display.rleblit(DIGITS[now[3] // 10], pos=(0*48, 80), fg=0xbdb6)
        self.on_screen = now

        draw = watch.drawable
        month = now[1] - 1
        month = MONTH[month*3:(month+1)*3]
        draw.string('{} {} {}'.format(now[2], month, now[0]),
                0, 180, width=240)

        self.meter.update()
        return True