-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
206 lines (164 loc) · 3.4 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
package main
import (
"bufio"
"errors"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"os"
"strconv"
)
const (
REDCHAN = "0"
BLUECHAN = "1"
GREENCHAN = "2"
)
var (
templates *template.Template
CURRENT_R int = 0
CURRENT_G int = 0
CURRENT_B int = 0
)
func FloatToString(input_num float64) string {
return strconv.FormatFloat(input_num, 'f', 6, 64)
}
func parseTemplates() (err error) {
templates, err = template.ParseGlob("templates/*.html")
return err
}
func setchan(channel string, val float64) error {
chancmd := channel + "=" + FloatToString(val) + "\n"
file, err := os.OpenFile("/dev/pi-blaster", os.O_RDWR, os.ModeNamedPipe)
if err != nil {
panic(err)
}
defer file.Close()
stream := bufio.NewWriter(file)
_, err = stream.WriteString(chancmd)
if err != nil {
panic(err)
}
stream.Flush()
return nil
}
func setChannelInteger(val int, channel string) error {
if val > 255 {
return errors.New("can't go over 255. sorry mate.")
}
if val < 0 {
return errors.New("can't go below 0. sorry mate.")
}
floatval := float64(val) / 255.0
setchan(channel, float64(floatval))
return nil
}
func setRed(val int) error {
return setChannelInteger(val, REDCHAN)
}
func setGreen(val int) error {
return setChannelInteger(val, GREENCHAN)
}
func setBlue(val int) error {
return setChannelInteger(val, BLUECHAN)
}
func setAll(r, g, b int) {
step := 1
for CURRENT_R != r ||
CURRENT_G != g ||
CURRENT_B != b {
if CURRENT_R < r {
CURRENT_R += step
setRed(CURRENT_R)
}
if CURRENT_R > r {
CURRENT_R -= step
setRed(CURRENT_R)
}
if CURRENT_G < g {
CURRENT_G += step
setGreen(CURRENT_G)
}
if CURRENT_G > g {
CURRENT_G -= step
setGreen(CURRENT_G)
}
if CURRENT_B < b {
CURRENT_B += step
setBlue(CURRENT_B)
}
if CURRENT_B > b {
CURRENT_B -= step
setBlue(CURRENT_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'})
} else {
fmt.Fprintf(w, "%s\n\n", err)
}
log.Println(
"panic catched:", err,
"\nRequest data:", r)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
defer errorHandler(w, r)
values, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
panic(err)
}
color := values.Get("targetcolor")
manual := values.Get("manual")
if manual == "set" {
man_r, _ := strconv.Atoi(values.Get("manual_r"))
man_g, _ := strconv.Atoi(values.Get("manual_g"))
man_b, _ := strconv.Atoi(values.Get("manual_b"))
fmt.Println("r: ", man_r, " - g: ", man_g, " - b:", man_b)
setAll(man_r, man_g, man_b)
} else {
switch color {
case "rot":
setAll(255, 0, 0)
break
case "gruen":
setAll(0, 255, 0)
break
case "blau":
setAll(0, 0, 255)
break
case "orange":
setAll(205, 55, 0)
break
case "favgruen":
setAll(255, 200, 0)
break
case "pink":
setAll(255, 0, 255)
break
case "lighter":
setAll(CURRENT_R+10, CURRENT_G+10, CURRENT_B+10)
break
case "darker":
setAll(CURRENT_R-10, CURRENT_G-10, CURRENT_B-10)
break
case "off":
setAll(0, 0, 0)
break
}
}
templates.ExecuteTemplate(w, "index.html", nil)
}
func main() {
setAll(CURRENT_R, CURRENT_G, CURRENT_B)
parseTemplates()
http.HandleFunc("/", indexHandler)
log.Fatal(http.ListenAndServe(":1337", nil))
}