-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.go
111 lines (93 loc) · 2.91 KB
/
clock.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 addons
import (
"fmt"
"image/color"
"math"
"strconv"
"time"
//"github.com/derickr/streamdeck-goui/actionhandlers"
"github.com/crazy3lf/colorconv"
"github.com/magicmonkey/go-streamdeck"
"github.com/magicmonkey/go-streamdeck/buttons"
//sddecorators "github.com/magicmonkey/go-streamdeck/decorators"
"github.com/rs/zerolog/log"
)
type TimerAction struct {
StartTime time.Time
Clock *Clock
ButtonIndex int
}
func (t *TimerAction) Pressed(btn streamdeck.Button) {
index := btn.GetButtonIndex()
if t.Clock.TimersActive[t.ButtonIndex] {
duration := time.Now().Sub(t.Clock.StartTimes[index])
out := time.Time{}.Add(duration)
log.Info().Msgf("Elapsed time for %s: %s (%s to %s)",
t.Clock.ClockNames[index],
out.Format("15:04:05"),
t.Clock.StartTimes[index].Format("2006-01-02 15:04:05"),
time.Now().Format("2006-01-02 15:04:05"))
t.Clock.TimersActive[t.ButtonIndex] = false
return
}
t.Clock.StartTimes[t.ButtonIndex] = t.StartTime
t.Clock.TimersActive[t.ButtonIndex] = true
}
type Clock struct {
SD *streamdeck.StreamDeck
ClockButtons [32]bool
Hues [32]int
ClockNames [32]string
dones [32]chan bool
Tickers [32]*time.Ticker
TimersActive [32]bool
StartTimes [32]time.Time
}
func (c *Clock) Init() {
c.Reset()
for i := 0; i < 32; i++ {
c.dones[i] = make(chan bool)
c.Tickers[i] = time.NewTicker(1000 * time.Millisecond)
go func(index int) {
for {
select {
case <-c.dones[index]:
return
case t := <-c.Tickers[index].C:
if c.ClockButtons[index] {
var button *buttons.TextButton
if c.TimersActive[index] {
st := t.Sub(c.StartTimes[index])
out := time.Time{}.Add(st)
r, g, b, _ := colorconv.HSLToRGB(float64(c.Hues[index]), math.Min(0.25+float64(out.Minute()/30.0), 0.75), 0.5)
if st > 1*time.Hour {
button = buttons.NewTextButtonWithColours(fmt.Sprintf("%s", out.Format("15h04")), color.White, color.RGBA{r, g, b, 255})
} else {
button = buttons.NewTextButtonWithColours(fmt.Sprintf("%s", out.Format("04:05")), color.White, color.RGBA{r, g, b, 255})
}
} else if c.ClockNames[index] != "" {
r, g, b, _ := colorconv.HSLToRGB(float64(c.Hues[index]), 0.5, 0.5)
button = buttons.NewTextButtonWithColours(c.ClockNames[index], color.White, color.RGBA{r, g, b, 255})
} else {
button = buttons.NewTextButton(fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second()))
}
button.SetActionHandler(&TimerAction{StartTime: t, Clock: c, ButtonIndex: index})
c.SD.AddButton(index, button)
}
}
}
}(i)
}
}
func (c *Clock) AddClockButton(offset int, hue string, inactiveImage string) {
c.ClockButtons[offset] = true
c.Hues[offset], _ = strconv.Atoi(hue)
c.ClockNames[offset] = inactiveImage
}
func (c *Clock) Reset() {
for i := 0; i < 32; i++ {
c.ClockButtons[i] = false
c.Hues[i] = 0
c.ClockNames[i] = ""
}
}