-
Notifications
You must be signed in to change notification settings - Fork 0
/
MorphingClock.go
290 lines (250 loc) · 6.54 KB
/
MorphingClock.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
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
package main
import (
"time"
"strconv"
"os"
"net"
"bytes"
"image"
"image/draw"
"image/color"
"encoding/json"
"io/ioutil"
"github.com/Sunoo/go-rpi-rgb-led-matrix"
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/Sunoo/hsv"
"github.com/lmittmann/ppm"
)
var (
clockConfig = ClockConfig{hsv.HSVColor{240, 100, 30}, 1, 2, 4, false, "03:04", 30 * time.Millisecond, time.Second / 2, 16, 32, 1, 1, "regular", false, false, false}
matrix rgbmatrix.Matrix
canvas *rgbmatrix.Canvas
sketch *image.RGBA
power = true
stopchan chan bool
stoppedchan chan bool
)
type ClockConfig struct {
ClockColor hsv.HSVColor
XOrig int
YOrig int
SegLength int
LeadingZero bool
TimeFormat string
AnimSpeed time.Duration
ClockReturn time.Duration
Rows int
Cols int
Parallel int
ChainLength int
HardwareMapping string
ShowRefreshRate bool
InverseColors bool
DisableHardwarePulsing bool
}
func Render() {
if (power) {
newR, newG, newB, _ := clockConfig.ClockColor.RGBA()
bounds := sketch.Bounds()
for curX := bounds.Min.X; curX < bounds.Max.X; curX++ {
for curY := bounds.Min.Y; curY < bounds.Max.Y; curY++ {
curR, curG, curB, curA := sketch.At(curX, curY).RGBA()
curR = curR * newR / 65535
curG = curG * newG / 65535
curB = curB * newB / 65535
canvas.Set(curX, curY, color.RGBA{uint8(curR), uint8(curG), uint8(curB), uint8(curA)})
}
}
canvas.Render()
}
}
func RunClock() {
sketch = image.NewRGBA(canvas.Bounds())
d1 := NewDigit(clockConfig.SegLength)
d2 := NewDigit(clockConfig.SegLength)
co := NewColon(clockConfig.SegLength)
d3 := NewDigit(clockConfig.SegLength)
d4 := NewDigit(clockConfig.SegLength)
d1start := image.Point{clockConfig.XOrig, clockConfig.YOrig}
d2start := d1start.Add(image.Point{d1.img.Bounds().Max.X + 1, 0})
costart := d2start.Add(image.Point{d2.img.Bounds().Max.X + 1, 0})
d3start := costart.Add(image.Point{co.img.Bounds().Max.X + 1, 0})
d4start := d3start.Add(image.Point{d3.img.Bounds().Max.X + 1, 0})
d1pos := image.Rectangle{d1start, d1start.Add(d1.img.Bounds().Max)}
d2pos := image.Rectangle{d2start, d2start.Add(d2.img.Bounds().Max)}
copos := image.Rectangle{costart, costart.Add(co.img.Bounds().Max)}
d3pos := image.Rectangle{d3start, d3start.Add(d3.img.Bounds().Max)}
d4pos := image.Rectangle{d4start, d4start.Add(d4.img.Bounds().Max)}
warmUp := true
for {
clock := time.Now().Format(clockConfig.TimeFormat)
h1, _ := strconv.Atoi(clock[0:1])
h2, _ := strconv.Atoi(clock[1:2])
m1, _ := strconv.Atoi(clock[3:4])
m2, _ := strconv.Atoi(clock[4:5])
if !clockConfig.LeadingZero && h1 == 0 {
h1 = -1
}
if m2 != d4.value {
done := false
for !done {
done = d4.Morph(m2)
draw.Draw(sketch, d4pos, d4.img, image.ZP, draw.Src)
Render()
time.Sleep(clockConfig.AnimSpeed)
}
}
if m1 != d3.value {
done := false
for !done {
done = d3.Morph(m1)
draw.Draw(sketch, d3pos, d3.img, image.ZP, draw.Src)
Render()
time.Sleep(clockConfig.AnimSpeed)
}
}
if warmUp {
draw.Draw(sketch, copos, co.img, image.ZP, draw.Src)
Render()
time.Sleep(clockConfig.AnimSpeed)
}
if h2 != d2.value {
done := false
for !done {
done = d2.Morph(h2)
draw.Draw(sketch, d2pos, d2.img, image.ZP, draw.Src)
Render()
time.Sleep(clockConfig.AnimSpeed)
}
}
if h1 != d1.value {
done := false
for !done {
done = d1.Morph(h1)
draw.Draw(sketch, d1pos, d1.img, image.ZP, draw.Src)
Render()
time.Sleep(clockConfig.AnimSpeed)
}
}
select {
case <-time.After(time.Now().Truncate(time.Minute).Add(time.Minute).Sub(time.Now())):
//Just keep running
case <-stopchan:
canvas.Render()
stoppedchan <- true
return
}
}
}
func Flaschen() {
pc, err := net.ListenPacket("udp", ":1337")
if err != nil {
return
}
defer pc.Close()
doneChan := make(chan error, 1)
buffer := make([]byte, 65535)
clockStopped := false
f := func() {
clockStopped = false
matrix.SetBrightness(clockConfig.ClockColor.V)
go RunClock()
}
timer := time.AfterFunc(clockConfig.ClockReturn, f)
timer.Stop()
go func() {
for {
n, _, err := pc.ReadFrom(buffer)
if err != nil {
doneChan <- err
return
}
if !clockStopped {
matrix.SetBrightness(100)
stopchan <- true
<-stoppedchan
clockStopped = true
}
timer.Reset(clockConfig.ClockReturn)
img, err := ppm.Decode(bytes.NewReader(buffer[:n]))
if err != nil {
doneChan <- err
return
}
draw.Draw(canvas, canvas.Bounds(), img, image.ZP, draw.Src)
canvas.Render()
}
}()
select {}
}
func main() {
jsonConfig, _ := ioutil.ReadFile("config.json")
json.Unmarshal(jsonConfig, &clockConfig)
stopchan = make(chan bool)
stoppedchan = make(chan bool)
config := &rgbmatrix.DefaultConfig
config.Rows = clockConfig.Rows
config.Cols = clockConfig.Cols
config.Parallel = clockConfig.Parallel
config.ChainLength = clockConfig.ChainLength
config.Brightness = clockConfig.ClockColor.V
config.HardwareMapping = clockConfig.HardwareMapping
config.ShowRefreshRate = clockConfig.ShowRefreshRate
config.InverseColors = clockConfig.InverseColors
config.DisableHardwarePulsing = clockConfig.DisableHardwarePulsing
matrix, _ = rgbmatrix.NewRGBLedMatrix(config)
canvas = rgbmatrix.NewCanvas(matrix)
defer canvas.Close()
info := accessory.Info{
Name: "Clock",
SerialNumber: "rpi-rgb-led-matrix",
Manufacturer: "Sunoo",
Model: "Morphing Clock",
}
acc := accessory.NewLightbulb(info)
acc.Lightbulb.On.SetValue(true)
acc.Lightbulb.Brightness.SetValue(clockConfig.ClockColor.V)
acc.Lightbulb.Saturation.SetValue(clockConfig.ClockColor.S)
acc.Lightbulb.Hue.SetValue(clockConfig.ClockColor.H)
acc.Lightbulb.On.OnValueRemoteUpdate(func(on bool) {
if power != on {
power = on;
if on {
go RunClock()
} else {
stopchan <- true
<-stoppedchan
}
}
})
acc.Lightbulb.Brightness.OnValueRemoteUpdate(func(bright int) {
clockConfig.ClockColor.V = bright
matrix.SetBrightness(bright)
Render()
})
acc.Lightbulb.Saturation.OnValueRemoteUpdate(func(sat float64) {
clockConfig.ClockColor.S = sat
Render()
})
acc.Lightbulb.Hue.OnValueRemoteUpdate(func(hue float64) {
clockConfig.ClockColor.H = hue
Render()
})
t, _ := hc.NewIPTransport(hc.Config{Pin: "12312312"}, acc.Accessory)
hc.OnTermination(func() {
<-t.Stop()
jsonConfig, _ := json.MarshalIndent(clockConfig, "", "\t")
ioutil.WriteFile("config.json", jsonConfig, 0666)
os.Exit(0)
})
go t.Start()
go RunClock()
go Flaschen()
select {}
}
func fatal(err error) {
if err != nil {
panic(err)
}
}