forked from lakrizz/go-pidioder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
263 lines (214 loc) · 4.89 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
import (
"errors"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"runtime/debug"
"strconv"
"syscall"
"time"
)
var (
templates *template.Template
blaster *Blaster
flag_R = flag.Uint("r", 17, "Red GPIO pin")
flag_G = flag.Uint("g", 22, "Green GPIO pin")
flag_B = flag.Uint("b", 27, "Blue GPIO pin")
flag_Cooldown = flag.Uint("cooldown", 10, "Milliseconds cooldown between requests")
)
type RGB struct {
R uint8
G uint8
B uint8
}
func (c RGB) String() string {
return fmt.Sprintf("#%02x%02x%02x", c.R, c.G, c.B)
}
func mustParseTemplates() {
var err error
templates, err = template.ParseGlob("templates/*.html")
if err != nil {
log.Fatal(err)
}
}
func attemptPiBlasterStart() error {
cmd := exec.Command("pi-blaster")
return cmd.Run()
}
func mustOpenPiBlaster() *os.File {
file, err := os.OpenFile("/dev/pi-blaster", os.O_RDWR, os.ModeNamedPipe)
if err != nil {
if perr, ok := err.(*os.PathError); ok && perr.Err == syscall.ENOENT {
err = attemptPiBlasterStart()
}
}
if err != nil {
log.Fatal(err)
}
return file
}
type Blaster struct {
pipe *os.File
Input chan RGB
Color chan chan RGB
r, g, b uint8
}
func NewBlaster() *Blaster {
return &Blaster{
Input: make(chan RGB),
Color: make(chan chan RGB),
}
}
func (b *Blaster) Run() {
b.pipe = mustOpenPiBlaster()
defer b.pipe.Close()
for {
select {
case c := <-b.Input:
b.setAll(c)
case c := <-b.Color:
go func(c chan RGB) {
select {
case c <- RGB{b.r, b.g, b.b}:
// delivered, everything's OK
case <-time.After(5 * time.Second):
log.Fatal("Requested color chan blocked too long.")
}
}(c)
}
}
}
func (b *Blaster) setPin(pin uint, val float64) error {
chanCmd := fmt.Sprintf("%d=%f\n", pin, val)
b.pipe.Write([]byte(chanCmd))
return nil
}
func (b *Blaster) setChannelInteger(pin uint, val uint8) error {
switch {
case val > 255:
return errors.New("can't go over 255. sorry mate.")
case val < 0:
return errors.New("can't go below 0. sorry mate.")
default:
fval := float64(val) / 255.0
return b.setPin(pin, fval)
}
}
func (b *Blaster) setRed(val uint8) (err error) {
if err = b.setChannelInteger(*flag_R, val); err == nil {
b.r = val
}
return
}
func (b *Blaster) setGreen(val uint8) (err error) {
if err = b.setChannelInteger(*flag_G, val); err == nil {
b.g = val
}
return
}
func (b *Blaster) setBlue(val uint8) (err error) {
if err = b.setChannelInteger(*flag_B, val); err == nil {
b.b = val
}
return
}
func (_ *Blaster) correctColor(c RGB) RGB {
gcorrection := float64(0x77) / 0xFF
bcorrection := float64(0x33) / 0xFF
c.G = uint8(float64(c.G) * gcorrection)
c.B = uint8(float64(c.B) * bcorrection)
return c
}
func (b *Blaster) setAll(c RGB) {
c = b.correctColor(c)
b.setRed(c.R)
b.setGreen(c.G)
b.setBlue(c.B)
}
func errorHandler(w http.ResponseWriter, r *http.Request) {
if err := recover(); err != nil {
w.WriteHeader(401)
fmt.Fprintf(w, "Oh...:(\n\n")
if e, ok := err.(error); ok {
w.Write([]byte(e.Error()))
w.Write([]byte{'\n', '\n'})
w.Write(debug.Stack())
} else {
fmt.Fprintf(w, "%s\n\n", err)
}
log.Println(
"panic catched:", err,
"\nRequest data:", r)
}
}
func parseUint8OrZero(s string) uint8 {
i, err := strconv.ParseUint(s, 10, 8)
if err != nil {
return 0
}
return uint8(i)
}
func withRequestedColor(f func(color RGB)) {
c := make(chan RGB)
defer close(c)
blaster.Color <- c
f(<-c)
}
func actionHandler(w http.ResponseWriter, r *http.Request) {
defer errorHandler(w, r)
values, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
panic(err)
}
action := values.Get("action")
switch action {
case "lighter":
withRequestedColor(func(c RGB) {
blaster.Input <- RGB{c.R + 10, c.G + 10, c.B + 10}
})
case "darker":
withRequestedColor(func(c RGB) {
blaster.Input <- RGB{c.R - 10, c.G - 10, c.B - 10}
})
case "off":
blaster.Input <- RGB{0, 0, 0}
case "set":
r := parseUint8OrZero(values.Get("r"))
g := parseUint8OrZero(values.Get("g"))
b := parseUint8OrZero(values.Get("b"))
blaster.Input <- RGB{r, g, b}
}
withRequestedColor(func(c RGB) {
log.Println(c.R, c.G, c.B)
w.Write([]byte(c.String()))
})
}
func currentColorHandler(w http.ResponseWriter, r *http.Request) {
defer errorHandler(w, r)
w.Header().Set("Content-Type", "text/plain")
withRequestedColor(func(c RGB) {
w.Write([]byte(c.String()))
})
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
defer errorHandler(w, r)
templates.ExecuteTemplate(w, "index.html", nil)
}
func main() {
flag.Parse()
blaster = NewBlaster()
go blaster.Run()
blaster.Input <- RGB{}
mustParseTemplates()
http.HandleFunc("/", indexHandler)
http.HandleFunc("/do", actionHandler)
http.HandleFunc("/color", currentColorHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("templates"))))
log.Fatal(http.ListenAndServe(":1337", nil))
}