-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (93 loc) · 1.8 KB
/
main.go
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
package main
import (
"machine"
"time"
"github.com/alexjomin/victron/vedirect"
"tinygo.org/x/drivers/ssd1306"
)
var (
currentPage = 0
display ssd1306.Device
state vedirect.State
lastClic = time.Now()
minimunDelayBetweenClic = time.Millisecond * 300
timeout = time.Second * 30
)
const (
baudRate = 19200
numberOfpages = 4
)
func initButton() {
button := machine.GP13
button.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
callback := func(p machine.Pin) {
delta := time.Now().Sub(lastClic)
if delta > minimunDelayBetweenClic {
incPage()
displayPage()
lastClic = time.Now()
}
}
err := button.SetInterrupt(machine.PinRising, callback)
if err != nil {
println(err)
}
}
func clearDisplayAfterTimeout() {
for {
time.Sleep(timeout)
if time.Now().Sub(lastClic) >= timeout && currentPage != 4 {
currentPage = 4
displayPage()
}
}
}
func main() {
go clearDisplayAfterTimeout()
initButton()
uart, err := initUART()
if err != nil {
println(err)
}
parser, err := vedirect.NewParser()
if err != nil {
println(err)
}
state, err = vedirect.NewState()
if err != nil {
println(err)
}
display, err = initDisplay()
if err != nil {
println(err)
}
welcomePage(&display)
for {
if uart.Buffered() > 0 {
data, err := uart.ReadByte()
if err != nil {
println(err)
continue
}
parser, err = parser.ParseByte(data)
if err != nil {
println(err)
continue
}
if parser.Ready {
data, _ := parser.GetKV()
if data == nil {
continue
}
parser.Ready = false
f, err := vedirect.NewFrame(data)
if err != nil {
println(err)
continue
}
state = state.Update(f)
}
}
time.Sleep(time.Microsecond * 100)
}
}