This repository was archived by the owner on Nov 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
177 lines (158 loc) · 4.38 KB
/
main.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
package main
import (
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"time"
"github.com/mrmorphic/hwio"
"github.com/sameer/fsm/moore"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
const tick = time.Duration(1000 / 22 * time.Millisecond) // convert ticks per second to useful number
type SignState struct {
Init bool
BackgroundFill sdl.Color // Background fill
Window *sdl.Window
Renderer *sdl.Renderer
Fonts map[int]*ttf.Font
Open bool
DoorOpen bool
SwitchValue SwitchState
Motion bool
Title string
Subtitle string
LogAndPostChan chan SignState
gpio22 hwio.Pin
}
func initState(s *SignState) (*SignState, error) {
// Init to default state
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
return nil, err
}
if i, err := sdl.ShowCursor(sdl.QUERY); i == sdl.ENABLE && err == nil {
sdl.ShowCursor(sdl.DISABLE)
if i, err := sdl.ShowCursor(sdl.QUERY); i == sdl.ENABLE && err != nil {
fmt.Println("failed to hide cursor")
}
}
window, rend, err := sdl.CreateWindowAndRenderer(width, height, sdl.WINDOW_FULLSCREEN|sdl.WINDOW_SHOWN|sdl.WINDOW_BORDERLESS)
if err != nil {
return nil, err
}
s.Window, s.Renderer = window, rend
if err := ttf.Init(); err != nil {
return nil, err
}
s.Fonts = make(map[int]*ttf.Font)
for _, size := range desiredFontSizes {
font, err := ttf.OpenFont(font, size)
if err != nil {
return nil, err
}
font.SetStyle(ttf.STYLE_BOLD)
font.SetHinting(ttf.HINTING_MONO)
s.Fonts[size] = font
}
s.BackgroundFill = white
s.Open = false
s.DoorOpen = false
s.SwitchValue = stateClosedForced
s.Motion = false
s.Title = "Closed"
s.Subtitle = ""
spawnSignalBroadcaster()
spawnSDLEventWaiter()
s.LogAndPostChan = spawnLogAndPost()
spawnStatsPoster()
if s.gpio22, err = hwio.GetPin("gpio22"); err == nil {
hwio.PinMode(s.gpio22, hwio.OUTPUT)
}
s.Init = true // Mark as succeeded
return s, nil
}
var transitionFunction moore.TransitionFunction = func(state moore.State, input moore.Input) (moore.State, error) {
s := state.(*SignState)
i := input.(*SignInput)
if !s.Init {
if _, err := initState(s); err != nil {
return nil, err
}
}
// Put inputs into state struct
s.Open, s.DoorOpen = i.IsOpen()
s.SwitchValue = i.GetSwitchValue()
s.Motion = i.IsThereMotion()
// State-based handling of tile
if s.Open {
s.Title = "Open"
} else {
s.Title = "Closed"
}
// State-based handling of subtitle
if s.Open && s.SwitchValue == stateShifts {
s.Subtitle = ""
for _, mentorShift := range shifts.getMentorsOnDuty() {
if s.Subtitle != "" {
s.Subtitle += " & "
}
s.Subtitle += mentorShift.name
}
} else if !s.Open && s.SwitchValue == stateShifts { // Show when the studio opens next if there are shifts today
nextShifts := shifts.getNextMentorsOnDutyToday()
if len(shifts.getMentorsOnDuty()) > 0 || len(nextShifts) == 0 {
// TODO: How to handle a missed shift in between other shifts?
// If there is supposed to be a shift right now and it's closed, we know that the opens at time is probably
// wrong so we shouldn't misinform the users. What about a shift that is separated from other shifts? Should
// we still say anything if that shift was missed? i.e. the possibility that there is a day where no one is
// on duty, due to a school holiday or other reason. For now, we depend upon a mentor to switch the sign to
// force closed to indicate that we shouldn't tell anyone when it opens.
s.Subtitle = "?"
} else if len(nextShifts) > 0 {
s.Subtitle = nextShifts[0].time(time.Now().Date()).Format(time.Kitchen)
} else {
s.Subtitle = ""
}
} else {
s.Subtitle = ""
}
if reason := signalStateStr.Load(); reason != "" {
fmt.Print("Gracefully shutting down because of \"", reason, "\"...")
if s.gpio22 != 0 {
hwio.ClosePin(s.gpio22)
}
s.Window.Destroy()
for _, font := range s.Fonts {
font.Close()
}
s = nil
}
if s == nil { // This is the quit state. Cleanup after ourselves.
inputState.finish()
ttf.Quit()
sdl.Quit()
fmt.Println("done!")
return nil, nil
}
return s, nil
}
func main() {
mm := moore.Make(
&SignState{},
nil,
transitionFunction,
inputFunction,
outputFunction,
)
if os.Getenv("DEV") != "" {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
} else {
go func() {
http.ListenAndServe("0.0.0.0:6060", nil)
}()
}
mm.Run(time.NewTicker(tick))
}