-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (121 loc) · 3.34 KB
/
main.go
File metadata and controls
142 lines (121 loc) · 3.34 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
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
package main
import (
"fmt"
"image/color"
"machine"
"time"
ui "github.com/itohio/tinygui"
"github.com/itohio/tinygui/widget"
"tinygo.org/x/drivers/ssd1306"
"tinygo.org/x/drivers/ws2812"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/freemono"
)
//go:generate tinygo flash -target=pico
var (
WIDTH int16 = 120
HEIGHT int16 = 8
day bool
)
var (
white = color.RGBA{255, 255, 255, 255}
black = color.RGBA{0, 0, 0, 0}
)
func main() {
machine.InitADC()
lightR.Configure(machine.PinConfig{Mode: machine.PinOutput})
lightG.Configure(machine.PinConfig{Mode: machine.PinOutput})
lightB.Configure(machine.PinConfig{Mode: machine.PinOutput})
btnUp.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
btnDown.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
btnSelect.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
ws := ws2812.NewWS2812(led)
ws.WriteColors([]color.RGBA{{R: 255}})
machine.I2C0.Configure(machine.I2CConfig{Frequency: 400 * machine.KHz})
// machine.LED.High()
// the delay is needed for display start from a cold reboot, not sure why
time.Sleep(time.Second)
display := ssd1306.NewI2C(machine.I2C0)
display.Configure(ssd1306.Config{Width: 128, Height: 64, Address: 0x3C, VccState: ssd1306.SWITCHCAPVCC})
display.ClearDisplay()
cfg, err := loadConfig()
if err != nil {
tinyfont.WriteLine(&display, &freemono.Regular9pt7b, 0, 11, err.Error(), white)
err = saveConfig(cfg)
if err != nil {
tinyfont.WriteLine(&display, &freemono.Regular9pt7b, 0, 22, err.Error(), white)
}
display.Display()
time.Sleep(time.Minute)
}
var dashboard *ui.ContainerBase[ui.Widget]
dashboard = ui.NewContainer[ui.Widget](
uint16(WIDTH), 0, ui.LayoutVList(1),
NewWaterer(0, machine.ADC0, pump01, &cfg),
NewWaterer(1, machine.ADC1, pump02, &cfg),
NewWaterer(2, machine.ADC2, pump03, &cfg),
NewWaterer(3, machine.ADC3, pump04, &cfg),
widget.NewLabel(uint16(WIDTH), 10, &tinyfont.TomThumb, func() string {
active := "o"
for _, i := range dashboard.Items {
if w, ok := i.(*Waterer); ok {
if w.active {
active = "#"
}
break
}
}
param := ""
for _, i := range dashboard.Items {
if w, ok := i.(*Waterer); ok {
if w.Selected() {
param = fmt.Sprintf(" [%.03f %.03f %.03f]", w.cfg.LowThreshold[w.index], w.lastVal, w.cfg.HighThreshold[w.index])
break
}
}
}
return fmt.Sprintf("%s%s", active, param)
}, white),
)
dW, dH := dashboard.Size()
ctx := ui.NewRandomContext(&display, time.Second*1, dW, dH)
// Watering logic
for _, i := range dashboard.Items {
if w, ok := i.(*Waterer); ok {
go w.run()
}
}
machine.Watchdog.Configure(machine.WatchdogConfig{
TimeoutMillis: 3000,
})
machine.Watchdog.Start()
go runDayloop()
cmd := runButtons()
go runUI(cmd, &cfg, dashboard)
// Drawing and moving display around
ticker := time.NewTicker(time.Millisecond * 50)
for range ticker.C {
ws.WriteColors([]color.RGBA{{G: 255}})
display.ClearBuffer()
dashboard.Draw(&ctx)
display.Display()
ws.WriteColors([]color.RGBA{{G: 0}})
}
}
func runDayloop() {
// Illumination
ticker := time.NewTicker(time.Hour * 24)
for {
lightR.High()
lightG.High()
lightB.High()
day = true
time.Sleep(time.Hour * 9)
day = false
lightR.Low()
lightG.Low()
lightB.Low()
<-ticker.C
}
}