-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
263 lines (236 loc) · 6.16 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
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
// Package main implements flapperd, the splitflap display daemon. This runs on
// the system directly connected to the splitflap display via USB, and exposes
// http endpoints for controlling the display.
package main
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/trapgate/flapper"
"github.com/trapgate/flapper/idle"
)
const (
defaultIdlerDelay = 10 * time.Minute
)
var (
errNoFormValue = errors.New("form value not set")
)
type serveCmd struct {
d *flapper.Display
idler idle.Display
cancelIdler context.CancelFunc
}
type displayCmd struct {
Text string `arg:"" name:"text" help:"String to display."`
}
type statusCmd struct {
}
var cli struct {
Serve serveCmd `cmd:"" help:"Listen on http for strings to display." default:"1"`
Display displayCmd `cmd:"" help:"Display a string on the splitflaps."`
Status statusCmd `cmd:"" help:"Display the status of the splitflaps."`
}
func main() {
ctx := kong.Parse(&cli)
err := ctx.Run(ctx)
ctx.FatalIfErrorf(err)
}
func (c *serveCmd) Run(ctx *kong.Context) error {
d, err := flapper.NewDisplay()
if err != nil {
return err
}
d.Init()
c.d = d
fmt.Println("listening on port 8080")
http.HandleFunc("/text", c.httpText)
http.HandleFunc("/status", c.httpStatus)
http.HandleFunc("/idle", c.httpIdle)
// Set up the "screensaver"
c.idler = idle.NewQuakeMon(defaultIdlerDelay)
idlerCtx, cancel := context.WithCancel(context.Background())
c.cancelIdler = cancel
go c.idler.Run(idlerCtx, d)
err = http.ListenAndServe(":8080", nil)
fmt.Println(err)
c.cancelIdler()
return err
}
func (c *serveCmd) httpText(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
text := c.d.Text()
if len(text) > 12 {
text = text[:12] + "\n" + text[12:]
}
fmt.Fprintf(w, "%v", text)
case http.MethodPost:
c.idler.Reset()
// maxmoving will limit the number of displays that animate at a time.
if maxMoving, err := readFormUint(r, "maxmoving"); err != errNoFormValue {
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
err = c.d.SetMaxMoving(uint32(maxMoving))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
}
// fullrotation specifies whether cells that are not changing are still
// moved.
if fullRotation, err := readFormBool(r, "fullrotation"); err != errNoFormValue {
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
err = c.d.SetForceRotation(fullRotation)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
}
// startdelay specifies the number of milliseconds to delay between
// starting modules moving.
if startDelay, err := readFormUint(r, "startdelay"); err != errNoFormValue {
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
err = c.d.SetStartDelay(uint32(startDelay))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
}
// animStyle specifies what order to start the modules in. It will have
// no visible effect unless startdelay or maxmoving is also set.
if animStyle, err := readFormString(r, "animstyle"); err != errNoFormValue {
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
err = c.d.SetAnimStyle(animStyle)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
}
// Features to add:
// - Move the word left across the display. Start the letters of the
// word and the cell to the left animating so that they finish at the
// same time.
// - Fall letters in from the top row to the bottom.
// For multi-line text, delay between each line.
delay := 5 * time.Second
delayStr := r.PostFormValue("delay")
if delayStr != "" {
delaySecs, err := strconv.Atoi(delayStr)
delay = time.Duration(delaySecs) * time.Second
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
}
lines := strings.Split(r.PostFormValue("text"), "\n")
fmt.Println(lines)
for i, line := range lines {
err := c.d.SetText(line)
if err != nil {
fmt.Println(err)
}
if i+1 < len(lines) {
time.Sleep(delay)
}
}
}
}
func (c *serveCmd) httpStatus(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
fmt.Fprintf(w, "%v", c.d.Status())
}
}
func (c *serveCmd) httpIdle(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
fmt.Fprintf(w, "%v", c.idler.Name())
case http.MethodPost:
if enable, err := readFormBool(r, "enable"); err != errNoFormValue {
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
c.idler.Enable(enable)
}
// TODO: allow the delay and the idler name to be set.
}
}
func readFormInt(r *http.Request, valName string) (int, error) {
valStr := r.PostFormValue(valName)
if valStr == "" {
return 0, errNoFormValue
}
val, err := strconv.ParseInt(valStr, 10, 32)
if err != nil {
return 0, err
}
return int(val), nil
}
func readFormUint(r *http.Request, valName string) (uint, error) {
valStr := r.PostFormValue(valName)
if valStr == "" {
return 0, errNoFormValue
}
val, err := strconv.ParseUint(valStr, 10, 32)
if err != nil {
return 0, err
}
return uint(val), nil
}
// readFormBool will attempt to read a boolean value from a form. It will return
// errNoFormValue if the specified value name wasn't sent.
func readFormBool(r *http.Request, valName string) (bool, error) {
valStr := r.PostFormValue(valName)
if valStr == "" {
return false, errNoFormValue
}
switch strings.ToLower(valStr) {
case "true", "on":
return true, nil
case "false", "off":
return false, nil
default:
return false, errors.New("invalid boolean value in form")
}
}
func readFormString(r *http.Request, valName string) (string, error) {
valStr := r.PostFormValue(valName)
if valStr == "" {
return "", errNoFormValue
}
return valStr, nil
}
func (c *displayCmd) Run(ctx *kong.Context) error {
d, err := flapper.NewDisplay()
if err != nil {
return err
}
d.SetText(c.Text)
return nil
}
func (c *statusCmd) Run(ctx *kong.Context) error {
d, err := flapper.NewDisplay()
if err != nil {
return err
}
d.Init()
fmt.Println(d.Status())
return nil
}