-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb-led.go
245 lines (189 loc) · 5.4 KB
/
web-led.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
package main
import (
"os"
"os/signal"
"time"
"fmt"
"strconv"
"github.com/ddrager/go-pi-blaster"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
const (
redPin = 17
greenPin = 22
bluePin = 24
jump = 5 // must be factor of 255
)
var (
a []int64
b piblaster.Blaster
control int = 0
)
func setRGB(red int64, green int64, blue int64) {
b.Apply(redPin, float64(red)/255.0)
b.Apply(greenPin, float64(green)/255.0)
b.Apply(bluePin, float64(blue)/255.0)
}
// cycle colors
func cycle(c web.C, w http.ResponseWriter, r *http.Request) {
stopAndClear()
delay, _ := strconv.ParseInt(c.URLParams["delay"], 10, 64)
if delay == 0 {
delay = 30;
}
// set initial colors
var red int64 = 255
var green int64 = 0
var blue int64 = 0
control = 1
fmt.Printf("Cycling with delay %d\n", delay)
fmt.Fprintf(w, "Cycling with a delay of %d milliseconds", delay)
// main loop
go func() {
timer := time.Tick(time.Millisecond * time.Duration(delay))
for _ = range timer {
if control == 0 { break }
if red >= 255 && blue <= 0 && green < 255 {
green = green + jump
} else if green >= 255 && blue <= 0 && red > 0 {
red = red - jump
} else if red <= 0 && green >= 255 && blue < 255 {
blue = blue + jump
} else if red <= 0 && blue >= 255 && green > 0 {
green = green - jump
} else if green <= 0 && blue >= 255 && red < 255 {
red = red + jump
} else if red >= 255 && green <= 0 && blue > 0 {
blue = blue - jump
}
setRGB(red, green, blue)
//fmt.Printf("Red: %d, Green: %d, Blue: %d\n", red, green, blue)
}
fmt.Printf("Cycle timer broken")
}()
}
func christmas(c web.C, w http.ResponseWriter, r *http.Request) {
stopAndClear()
control = 1
fmt.Printf("Turning on Christmas mode\n")
delay := 2000
color := 0 // current color
// set initial color - before timer starts
setRGB(0, 255, 0)
// main loop
go func() {
timer := time.Tick(time.Millisecond * time.Duration(delay))
for _ = range timer {
if control == 0 { break }
if color == 0 {
setRGB(255, 0, 0);
color = 1
} else {
setRGB(0, 255, 0);
color = 0
}
}
fmt.Printf("Christmas timer broken")
}()
}
// cycle colors
func christmas_fade(c web.C, w http.ResponseWriter, r *http.Request) {
stopAndClear()
delay, _ := strconv.ParseInt(c.URLParams["delay"], 10, 64)
if delay == 0 {
delay = 30;
}
// set initial colors
var red int64 = 0
var green int64 = 0
var blue int64 = 0
var direction int64 = 0
var color int64 = 0
control = 1
fmt.Printf("Cycling with delay %d\n", delay)
fmt.Fprintf(w, "Cycling with a delay of %d milliseconds", delay)
// main loop
go func() {
timer := time.Tick(time.Millisecond * time.Duration(delay))
for _ = range timer {
if control == 0 { break }
// make the jump in color
if direction == 0 {
if color == 0 {
red = red + jump
} else { green = green + jump }
} else if direction == 1 {
if color == 0 {
red = red - jump
} else { green = green - jump }
}
// set the lights
setRGB(red, green, blue)
// switch colors & direction if at bottom of range
if color == 0 && direction == 1 && red == 0 { color = 1; direction = 0;
} else if color == 1 && direction == 1 && green == 0 { color = 0; direction = 0; }
// if at top of range, switch direction and keep color
if (red >= 255 || green >= 255) && direction == 0 {
direction = 1
}
if control == 0 { break }
// fmt.Printf("Red: %d, Green: %d, Blue: %d\n", red, green, blue)
}
fmt.Printf("Cycle timer broken")
}()
}
func clear(c web.C, w http.ResponseWriter, r *http.Request) {
stopAndClear()
setRGB(0, 0, 0)
}
func stopAndClear() {
control = 0
// allow running processes to stop
time.Sleep(100 * time.Millisecond)
setRGB(0, 0, 0)
b.Reset();
}
func menu(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}
func websetRGB(c web.C, w http.ResponseWriter, r *http.Request) {
stopAndClear()
red, _ := strconv.ParseInt(c.URLParams["red"], 10, 64)
green, _ := strconv.ParseInt(c.URLParams["green"], 10, 64)
blue, _ := strconv.ParseInt(c.URLParams["blue"], 10, 64)
setRGB(red, green, blue)
}
func main() {
fmt.Printf("Running web server\n")
// set initial pins and start interface
a := []int64{redPin, greenPin, bluePin}
b.Start(a)
control = 1
// reset pins when program ends
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
pwd, _ := os.Getwd()
staticFilesLocation := pwd + "/static"
fmt.Printf("Launching web server...\n")
fmt.Printf("Loading static files from %s\n", staticFilesLocation)
// web functionality
goji.Get("/hello/:name", menu)
goji.Get("/cycle", cycle)
goji.Get("/cycle/:delay", cycle)
goji.Get("/christmas", christmas)
goji.Get("/christmas_fade", christmas_fade)
goji.Get("/clear", clear)
goji.Get("/setrgb/:red/:green/:blue", websetRGB)
goji.Handle("/*", http.FileServer(http.Dir(staticFilesLocation)))
goji.Serve()
// capture control c, probably a better way to do this
//time.Sleep(time.Millisecond * 15000)
for sig := range c {
fmt.Printf("captured %v, exiting..", sig)
//b.DumpCurrent()
stopAndClear()
os.Exit(1)
}
}