-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (80 loc) · 2.18 KB
/
main.go
File metadata and controls
92 lines (80 loc) · 2.18 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
package main
import (
"VolumioLCD/display"
"VolumioLCD/logger"
"VolumioLCD/volumio"
"VolumioLCD/utils"
"context"
"os"
"os/signal"
"time"
)
// import "fmt"
const (
updateInterval int = 200
volumioURI string = "http://localhost:3000"
)
func main() {
// Correctly handle os messages
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
defer func() {
signal.Stop(sig)
cancel()
}()
// Initialize volumio client
volumio.URI = volumioURI
timeout := 60.0 * time.Second;
for(!utils.PathExists("/dev/i2c-1") && timeout > 0.0) {
waitTime := 1.0 * time.Second
time.Sleep(waitTime)
timeout -= waitTime
}
time.Sleep(5.0 * time.Second)
lcd := display.New(1, 0x27)
lcd.Screen = display.NewScreen(2, 16)
var artistText display.TextView
var titleText display.TextView
var titleScroll display.ScrollView
titleScroll.SetChild(&titleText)
titleScroll.SetLength(16)
titleScroll.SetSpeed(2)
lcd.Screen.GetRow(0).SetChild(&artistText)
lcd.Screen.GetRow(1).SetChild(&titleScroll)
go lcd.Loop()
defer lcd.Close()
go func() {
select {
case <-sig:
cancel()
case <-ctx.Done():
}
}()
go func() {
// allow to check for changes
var prevTitle string
var prevArtist string
// main loop
for {
time.Sleep(time.Duration(updateInterval) * time.Millisecond)
state, err := volumio.GetPlayerState()
if err != nil {
logger.Warningf(err.Error())
continue
}
artistText.SetText(state.Artist)
titleText.SetText(state.Title)
if state.Status == "play" &&
(state.Artist != prevArtist || state.Title != prevTitle) &&
state.Artist != "" &&
state.Title != "" {
lcd.Backlight = true
} else if state.Status == "stop" || state.Status == "pause" {
lcd.Backlight = false
}
}
}()
<-sig
}