-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.toit
49 lines (41 loc) · 1.29 KB
/
main.toit
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
// Copyright (C) 2023 Kasper Lund.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the LICENSE file.
import esp32
import log
import net
import ntp
import .mode as mode
RETRIES ::= mode.DEVELOPMENT ? 2 : 5
PERIOD ::= mode.DEVELOPMENT ? (Duration --s=10) : (Duration --m=1)
main:
// If the setup container is supposed to run, we allow
// the application container to terminate eagerly. This
// allows the two containers to always start without
// interfering with each other.
if not mode.RUNNING: return
retries := 0
while ++retries < RETRIES:
network/net.Interface? := null
exception := catch --trace:
network = net.open
run network
retries = 0
if network: network.close
sleep PERIOD
// We keep failing to connect or run the app. We assume
// that this is because we've got the wrong WiFi credentials
// so we enter the setup mode.
mode.run_setup
run network/net.Interface:
tags/Map? := null
if mode.DEVELOPMENT: tags = {"mode": "development"}
while true:
log.info "running" --tags=tags
result := ntp.synchronize --network=network
if result:
log.info "contacted ntp server" --tags={
"adjustment" : result.adjustment,
"accuracy" : result.accuracy,
}
sleep PERIOD