-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.go
More file actions
93 lines (84 loc) · 1.48 KB
/
ui.go
File metadata and controls
93 lines (84 loc) · 1.48 KB
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
package main
import (
"machine"
"time"
ui "github.com/itohio/tinygui"
)
func button(p machine.Pin) time.Duration {
now := time.Now()
time.Sleep(time.Millisecond * 10)
for !p.Get() {
time.Sleep(time.Millisecond)
machine.Watchdog.Update()
}
return time.Since(now)
}
func runButtons() chan ui.UserCommand {
command := make(chan ui.UserCommand)
cmd := func(c ui.UserCommand) {
select {
case command <- c:
default:
}
}
go func() {
N := 0
for {
switch {
case !btnUp.Get():
d := button(btnUp)
if d > time.Second*5 {
cmd(ui.RESET)
} else if d > time.Second {
cmd(ui.LONG_UP)
} else {
cmd(ui.PREV)
}
case !btnDown.Get():
d := button(btnDown)
if d > time.Second {
cmd(ui.LONG_DOWN)
} else {
cmd(ui.NEXT)
}
case !btnSelect.Get():
d := button(btnSelect)
if d > time.Second {
cmd(ui.ESC)
} else {
cmd(ui.ENTER)
}
}
time.Sleep(time.Millisecond * 10)
if N == 0 {
cmd(ui.IDLE)
}
N = (N + 1) % 100
}
}()
return command
}
func runUI(cmd chan ui.UserCommand, cfg *Config, w *ui.ContainerBase[ui.Widget]) {
for {
select {
case c := <-cmd:
machine.Watchdog.Update()
if w.Interact(c) {
continue
}
switch c {
case ui.RESET:
machine.CPUReset()
case ui.LONG_DOWN:
saveConfig(*cfg)
case ui.LONG_UP:
for _, i := range w.Items {
if w, ok := i.(*Waterer); ok {
w.active = !w.active
w.pump.Low()
}
}
}
}
}
}