__init__.py 8.1 KB
Newer Older
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import os
import sys
import leds
import display
import buttons

sys.path.append("/apps/digiclk/")
import monotime as utime
import draw

DIGITS = [
    (True, True, True, True, True, True, False),
    (False, True, True, False, False, False, False),
    (True, True, False, True, True, False, True),
    (True, True, True, True, False, False, True),
    (False, True, True, False, False, True, True),
    (True, False, True, True, False, True, True),
    (True, False, True, True, True, True, True),
    (True, True, True, False, False, False, False),
    (True, True, True, True, True, True, True),
    (True, True, True, True, False, True, True),
]


def renderNum(d, num, x):
    draw.Grid7Seg(d, x, 0, 7, DIGITS[num // 10], (255, 255, 255))
    draw.Grid7Seg(d, x + 5, 0, 7, DIGITS[num % 10], (255, 255, 255))


def renderColon(d):
    draw.GridVSeg(d, 11, 2, 7, 2, (255, 255, 255))
    draw.GridVSeg(d, 11, 4, 7, 2, (255, 255, 255))


def renderText(d, text, blankidx=None):
    bs = bytearray(text)

    if blankidx != None:
        bs[blankidx : blankidx + 1] = b"_"

    d.print(
        MODES[MODE] + " " + bs.decode(), fg=(255, 255, 255), bg=None, posx=0, posy=7 * 8
    )


def renderBar(d, num):
    d.rect(20, 78, 20 + num * 2, 80, col=(255, 255, 255))


def render(d):
    ltime = utime.localtime()
    years = ltime[0]
    months = ltime[1]
    days = ltime[2]
    hours = ltime[3]
    mins = ltime[4]
    secs = ltime[5]

    d.clear()

    if MODE == CHANGE_YEAR:
        renderNum(d, years // 100, 1)
        renderNum(d, years % 100, 13)
    elif MODE == CHANGE_MONTH:
        renderNum(d, months, 13)
    elif MODE == CHANGE_DAY:
        renderNum(d, days, 13)
    else:
        renderNum(d, hours, 1)
        renderNum(d, mins, 13)

    if MODE not in (CHANGE_YEAR, CHANGE_MONTH, CHANGE_DAY) and secs % 2 == 0:
        renderColon(d)

    renderText(d, NAME, None)
    renderBar(d, secs)

    d.update()


LONG_DELAY = 400
BUTTON_UPDATE_TIME = 100
BUTTON_SEL = 1 << 0
BUTTON_SEL_LONG = 1 << 1
BUTTON_UP = 1 << 2
BUTTON_UP_LONG = 1 << 3
BUTTON_DOWN = 1 << 4
BUTTON_DOWN_LONG = 1 << 5
pressed_prev = 0
button_long_prev = {BUTTON_SEL: False, BUTTON_UP: False, BUTTON_DOWN: False}
button_times = {BUTTON_SEL: 0, BUTTON_UP: 0, BUTTON_DOWN: 0}


def checkButton(button, button_long, osbutton, pressed, t):
    cur_buttons = 0

    if pressed & osbutton and not pressed_prev & osbutton:
        button_times[button] = t
        button_long_prev[button] = False
    elif pressed_prev & osbutton:
        if button_times[button] + LONG_DELAY < t:
            cur_buttons |= button_long
            button_times[button] = t
            button_long_prev[button] = True
        elif not pressed & osbutton and not button_long_prev[button]:
            cur_buttons |= button

    return cur_buttons


def checkButtons():
    global pressed_prev

    t = utime.time_monotonic_ms()
    pressed = buttons.read(
        buttons.BOTTOM_LEFT | buttons.TOP_RIGHT | buttons.BOTTOM_RIGHT
    )
    cur_buttons = 0

    cur_buttons |= checkButton(
        BUTTON_SEL, BUTTON_SEL_LONG, buttons.BOTTOM_LEFT, pressed, t
    )
    cur_buttons |= checkButton(BUTTON_UP, BUTTON_UP_LONG, buttons.TOP_RIGHT, pressed, t)
    cur_buttons |= checkButton(
        BUTTON_DOWN, BUTTON_DOWN_LONG, buttons.BOTTOM_RIGHT, pressed, t
    )

    pressed_prev = pressed
    return cur_buttons


def modTime(yrs, mth, day, hrs, mns, sec):
    ltime = utime.localtime()
    new = utime.mktime(
        (
            ltime[0] + yrs,
            ltime[1] + mth,
            ltime[2] + day,
            ltime[3] + hrs,
            ltime[4] + mns,
            ltime[5] + sec,
            None,
            None,
        )
    )
    utime.set_time(new)


def ctrl_display(bs):
    global MODE, updated
    updated = True
    if bs & BUTTON_SEL_LONG:
        MODE = CHANGE_HOURS
    else:
        updated = False


def ctrl_chg_hrs(bs):
    global MODE, updated
    updated = True
    if bs & BUTTON_SEL_LONG:
        MODE = DISPLAY
    elif bs & BUTTON_SEL:
        MODE = CHANGE_MINUTES
    elif bs & BUTTON_UP_LONG:
        modTime(0, 0, 0, 10, 0, 0)
    elif bs & BUTTON_DOWN_LONG:
        modTime(0, 0, 0, -10, 0, 0)
    elif bs & BUTTON_UP:
        modTime(0, 0, 0, 1, 0, 0)
    elif bs & BUTTON_DOWN:
        modTime(0, 0, 0, -1, 0, 0)
    else:
        updated = False


def ctrl_chg_mns(bs):
    global MODE, updated
    updated = True
    if bs & BUTTON_SEL_LONG:
        MODE = DISPLAY
    elif bs & BUTTON_SEL:
        MODE = CHANGE_SECONDS
    elif bs & BUTTON_UP_LONG:
        modTime(0, 0, 0, 0, 10, 0)
    elif bs & BUTTON_DOWN_LONG:
        modTime(0, 0, 0, 0, -10, 0)
    elif bs & BUTTON_UP:
        modTime(0, 0, 0, 0, 1, 0)
    elif bs & BUTTON_DOWN:
        modTime(0, 0, 0, 0, -1, 0)
    else:
        updated = False


def ctrl_chg_sec(bs):
    global MODE, updated
    updated = True
    if bs & BUTTON_SEL_LONG:
        MODE = DISPLAY
    elif bs & BUTTON_SEL:
        MODE = CHANGE_YEAR
    elif bs & BUTTON_UP_LONG:
        modTime(0, 0, 0, 0, 0, 10)
    elif bs & BUTTON_DOWN_LONG:
        modTime(0, 0, 0, 0, 0, -10)
    elif bs & BUTTON_UP:
        modTime(0, 0, 0, 0, 0, 1)
    elif bs & BUTTON_DOWN:
        modTime(0, 0, 0, 0, 0, -1)
    else:
        updated = False


def ctrl_chg_yrs(bs):
    global MODE, updated
    updated = True
    if bs & BUTTON_SEL_LONG:
        MODE = DISPLAY
    elif bs & BUTTON_SEL:
        MODE = CHANGE_MONTH
    elif bs & BUTTON_UP_LONG:
        modTime(10, 0, 0, 0, 0, 0)
    elif bs & BUTTON_DOWN_LONG:
        modTime(-10, 0, 0, 0, 0, 0)
    elif bs & BUTTON_UP:
        modTime(1, 0, 0, 0, 0, 0)
    elif bs & BUTTON_DOWN:
        modTime(-1, 0, 0, 0, 0, 0)
    else:
        updated = False


def ctrl_chg_mth(bs):
    global MODE, updated
    updated = True
    if bs & BUTTON_SEL_LONG:
        MODE = DISPLAY
    elif bs & BUTTON_SEL:
        MODE = CHANGE_DAY
    elif bs & BUTTON_UP_LONG:
        modTime(0, 6, 0, 0, 0, 0)
    elif bs & BUTTON_DOWN_LONG:
        modTime(0, -6, 0, 0, 0, 0)
    elif bs & BUTTON_UP:
        modTime(0, 1, 0, 0, 0, 0)
    elif bs & BUTTON_DOWN:
        modTime(0, -1, 0, 0, 0, 0)
    else:
        updated = False


def ctrl_chg_day(bs):
    global MODE, updated
    updated = True
    if bs & BUTTON_SEL_LONG:
        MODE = DISPLAY
    elif bs & BUTTON_SEL:
        MODE = CHANGE_HOURS
    elif bs & BUTTON_UP_LONG:
        modTime(0, 0, 10, 0, 0, 0)
    elif bs & BUTTON_DOWN_LONG:
        modTime(0, 0, -10, 0, 0, 0)
    elif bs & BUTTON_UP:
        modTime(0, 0, 1, 0, 0, 0)
    elif bs & BUTTON_DOWN:
        modTime(0, 0, -1, 0, 0, 0)
    else:
        updated = False


NAME = None
FILENAME = "nickname.txt"


def load_nickname():
    global NAME
    if FILENAME in os.listdir("."):
        with open("nickname.txt", "rb") as f:
            name = f.read().strip()
    else:
        name = b"no nick"

    if len(name) > 7:
        name = name[0:7]
    else:
        name = b" " * (7 - len(name)) + name

    NAME = name


# MODE values
DISPLAY = 0
CHANGE_HOURS = 1
CHANGE_MINUTES = 2
CHANGE_SECONDS = 3
CHANGE_YEAR = 4
CHANGE_MONTH = 5
CHANGE_DAY = 6

MODE = DISPLAY
MODES = {
    DISPLAY: "---",
    CHANGE_HOURS: "HRS",
    CHANGE_MINUTES: "MNS",
    CHANGE_SECONDS: "SEC",
    CHANGE_YEAR: "YRS",
    CHANGE_MONTH: "MTH",
    CHANGE_DAY: "DAY",
}
updated = False

CTRL_FNS = {
    DISPLAY: ctrl_display,
    CHANGE_HOURS: ctrl_chg_hrs,
    CHANGE_MINUTES: ctrl_chg_mns,
    CHANGE_SECONDS: ctrl_chg_sec,
    CHANGE_YEAR: ctrl_chg_yrs,
    CHANGE_MONTH: ctrl_chg_mth,
    CHANGE_DAY: ctrl_chg_day,
}


def main():
    global updated
    try:
        load_nickname()
        with display.open() as d:
            last_secs, secs = 0, 0
            last_msecs, msecs = 0, 0
            while True:
                updated = False

                bs = checkButtons()
                CTRL_FNS[MODE](bs)

                last_secs, secs = secs, utime.time_monotonic()
                if updated or secs > last_secs:
                    render(d)

                last_msecs, msecs = msecs, utime.time_monotonic_ms()
                if msecs - last_msecs < BUTTON_UPDATE_TIME:
                    utime.sleep_ms(BUTTON_UPDATE_TIME - (msecs - last_msecs))
    except KeyboardInterrupt:
        pass


main()