-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathblinker.go
243 lines (222 loc) · 7.42 KB
/
blinker.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
// Copyright 2024 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file manages the blink(1) state.
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
blink1 "github.com/kazrakcom/go-blink1"
)
const failureRetries = 3
// calendarState is a display state for the calendar event. It encapsulates both the colors to display and the flash duration.
type CalendarState struct {
Name string
primary blink1.State
secondary blink1.State
primaryFlash time.Duration
secondaryFlash time.Duration
alternate bool
}
func (state CalendarState) Execute(blinker *BlinkerState) {
blinker.newState <- state
}
var (
Black = CalendarState{Name: "Black", primary: blink1.OffState}
Green = CalendarState{Name: "Green", primary: blink1.State{Green: 255}, secondary: blink1.State{Green: 255}}
Yellow = CalendarState{Name: "Yellow", primary: blink1.State{Red: 255, Green: 160}, secondary: blink1.State{Red: 255, Green: 160}}
Red = CalendarState{Name: "Red", primary: blink1.State{Red: 255}, secondary: blink1.State{Red: 255}}
RedFlash = CalendarState{Name: "Red Flash", primary: blink1.State{Red: 255}, secondary: blink1.OffState, primaryFlash: time.Duration(500) * time.Millisecond, alternate: true}
FastRedFlash = CalendarState{Name: "Fast Red Flash", primary: blink1.State{Red: 255}, secondary: blink1.OffState, primaryFlash: time.Duration(125) * time.Millisecond, alternate: true}
BlueFlash = CalendarState{Name: "Red-Blue Flash", primary: blink1.State{Blue: 255}, secondary: blink1.State{Red: 255}, primaryFlash: time.Duration(500) * time.Millisecond, alternate: true}
Blue = CalendarState{Name: "Blue", primary: blink1.State{Blue: 255}, secondary: blink1.State{Blue: 255}}
MagentaFlash = CalendarState{Name: "MagentaFlash", primary: blink1.State{Red: 255, Blue: 255}, secondary: blink1.OffState, primaryFlash: time.Duration(125) * time.Millisecond, alternate: true}
)
// Combines the two states into one state that shows both events
func CombineStates(in1 CalendarState, in2 CalendarState) CalendarState {
combined := CalendarState{Name: in1.Name + "/" + in2.Name,
primary: in1.primary,
secondary: in2.primary,
primaryFlash: in1.primaryFlash,
secondaryFlash: in2.primaryFlash,
alternate: false}
return combined
}
// Swaps the sides for a state, for use in flashing
func SwapState(in CalendarState) CalendarState {
swapped := CalendarState{Name: in.Name + " swapped",
primary: in.secondary,
secondary: in.primary,
primaryFlash: in.secondaryFlash,
secondaryFlash: in.primaryFlash,
alternate: false}
return swapped
}
// blinkerState encapsulates the current device state of the blink(1).
type BlinkerState struct {
device *blink1.Device
newState chan CalendarState
failures int
maxFailures int
}
func NewBlinkerState(maxFailures int) *BlinkerState {
blinker := &BlinkerState{
newState: make(chan CalendarState, 1),
maxFailures: maxFailures,
}
blinker.reinitialize()
return blinker
}
func (blinker *BlinkerState) reinitialize() error {
if blinker.device != nil {
blinker.device.Close()
blinker.device = nil
}
device, err := blink1.OpenNextDevice()
if err != nil {
blinker.failures++
if blinker.failures > blinker.maxFailures {
log.Fatalf("Unable to initialize blink(1): %v", err)
}
fmt.Fprint(dotOut, "X")
} else {
blinker.failures = 0
}
blinker.device = device
return err
}
func (blinker *BlinkerState) turnOff() {
blinker.device.SetState(blink1.OffState)
}
func (blinker *BlinkerState) setState(state blink1.State) error {
if blinker.failures > 0 {
err := blinker.reinitialize()
if err != nil {
fmt.Fprintf(debugOut, "Reinitialize failed, error %v\n", err)
return err
}
}
err := blinker.device.SetState(state)
if err != nil {
fmt.Fprintf(debugOut, "Re-initializing because of error %v\n", err)
err = blinker.reinitialize()
if err != nil {
fmt.Fprintf(debugOut, "Reinitialize failed, error %v\n", err)
return err
}
// Try one more time before giving up for this pass.
err = blinker.device.SetState(state)
if err != nil {
fmt.Fprintf(debugOut, "Setting blinker state failed, error %v\n", err)
}
} else {
blinker.failures = 0
}
return err
}
func (blinker *BlinkerState) patternRunner() {
currentState := Black
failing := false
err := blinker.setState(currentState.primary)
if err != nil {
failing = true
}
var ticker <-chan time.Time
stateFlip := false
for {
select {
case newState := <-blinker.newState:
if newState != currentState || failing {
fmt.Fprintf(debugOut, "Changing from state %v to %v\n", currentState, newState)
currentState = newState
if newState.primaryFlash > 0 || newState.secondaryFlash > 0 {
ticker = time.After(time.Millisecond)
} else {
if ticker != nil {
fmt.Fprintf(debugOut, "Killing timer\n")
ticker = nil
}
state1 := newState.primary
state1.LED = blink1.LED1
state2 := newState.secondary
state2.LED = blink1.LED2
err1 := blinker.setState(state1)
err2 := blinker.setState(state2)
failing = (err1 != nil) || (err2 != nil)
}
} else {
fmt.Fprintf(debugOut, "Retaining state %v unchanged\n", newState)
}
case <-ticker:
fmt.Fprintf(debugOut, "Timer fired\n")
state1 := currentState.primary
state2 := currentState.secondary
if stateFlip {
if currentState.alternate {
state1, state2 = state2, state1
} else {
if currentState.primaryFlash > 0 {
state1 = blink1.OffState
}
if currentState.secondaryFlash > 0 {
state2 = blink1.OffState
}
}
}
state1.Duration = currentState.primaryFlash
state1.FadeTime = state1.Duration
if currentState.alternate {
state2.Duration, state2.FadeTime = state1.Duration, state1.FadeTime
} else {
state2.Duration = currentState.secondaryFlash
state2.FadeTime = state2.Duration
}
// We set state1 on LED 1 and state2 on LED 2. On an original (mk1) blink(1) state2 will be ignored.
state1.LED = blink1.LED1
state2.LED = blink1.LED2
fmt.Fprintf(debugOut, "Setting state (%v and %v)\n", state1, state2)
err1 := blinker.setState(state1)
err2 := blinker.setState(state2)
failing = (err1 != nil) || (err2 != nil)
stateFlip = !stateFlip
nextTick := state1.Duration
if state1.Duration == 0 {
nextTick = state2.Duration
}
fmt.Fprintf(debugOut, "Next tick: %s\n", nextTick)
ticker = time.After(nextTick)
}
}
}
// Signal handler - SIGINT or SIGKILL should turn off the blinker before we exit.
// SIGQUIT should turn on debug mode.
func signalHandler(blinker *BlinkerState) {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGQUIT)
for {
s := <-interrupt
if s == syscall.SIGQUIT {
fmt.Println("Turning on debug mode.")
debugOut = os.Stdout
continue
}
if blinker.failures == 0 {
blinker.turnOff()
}
log.Fatalf("Quitting due to signal %v", s)
}
}