Skip to content

Commit 1bb046a

Browse files
committed
Fix control break delay, adds fading, better blink timer logic
1 parent a6440d2 commit 1bb046a

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

static/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ <h3>Available Commands</h3>
4949
<button type="button" class="btn btn-outline-primary pi-clear">Clear</button>
5050
<button type="button" class="btn btn-outline-primary pi-cycle">Cycle All Colors</button>
5151
<button type="button" class="btn btn-success pi-christmas">Christmas Lights</button>
52+
<button type="button" class="btn btn-success pi-christmas-fade">Christmas Fade Lights</button>
5253
</div>
5354
<div class="col">
5455
<h3>Set A Color</h3>
@@ -84,6 +85,11 @@ <h3>Set A Color</h3>
8485
$('.pi-christmas').click(function() {
8586
$.get('/christmas');
8687
});
88+
89+
$('.pi-christmas-fade').click(function() {
90+
$.get('/christmas_fade');
91+
});
92+
8793
</script>
8894

8995

web-led.go

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func cycle(c web.C, w http.ResponseWriter, r *http.Request) {
5656
go func() {
5757
timer := time.Tick(time.Millisecond * time.Duration(delay))
5858
for _ = range timer {
59+
if control == 0 { break }
60+
5961
if red >= 255 && blue <= 0 && green < 255 {
6062
green = green + jump
6163
} else if green >= 255 && blue <= 0 && red > 0 {
@@ -71,8 +73,6 @@ func cycle(c web.C, w http.ResponseWriter, r *http.Request) {
7173
}
7274
setRGB(red, green, blue)
7375

74-
if control == 0 { break }
75-
7676
//fmt.Printf("Red: %d, Green: %d, Blue: %d\n", red, green, blue)
7777
}
7878
fmt.Printf("Cycle timer broken")
@@ -87,21 +87,92 @@ func christmas(c web.C, w http.ResponseWriter, r *http.Request) {
8787

8888
fmt.Printf("Turning on Christmas mode\n")
8989

90-
delay := 4000
90+
delay := 2000
91+
color := 0 // current color
92+
93+
// set initial color - before timer starts
94+
setRGB(0, 255, 0)
9195

9296
// main loop
9397
go func() {
9498
timer := time.Tick(time.Millisecond * time.Duration(delay))
9599
for _ = range timer {
96-
setRGB(255, 0, 0)
97-
time.Sleep(time.Millisecond * time.Duration(delay/2))
98-
setRGB(0, 255, 0)
100+
99101
if control == 0 { break }
102+
103+
if color == 0 {
104+
setRGB(255, 0, 0);
105+
color = 1
106+
} else {
107+
setRGB(0, 255, 0);
108+
color = 0
109+
}
110+
100111
}
101112
fmt.Printf("Christmas timer broken")
102113
}()
103114
}
104115

116+
// cycle colors
117+
func christmas_fade(c web.C, w http.ResponseWriter, r *http.Request) {
118+
stopAndClear()
119+
120+
delay, _ := strconv.ParseInt(c.URLParams["delay"], 10, 64)
121+
122+
if delay == 0 {
123+
delay = 30;
124+
}
125+
126+
// set initial colors
127+
var red int64 = 0
128+
var green int64 = 0
129+
var blue int64 = 0
130+
var direction int64 = 0
131+
var color int64 = 0
132+
133+
control = 1
134+
135+
fmt.Printf("Cycling with delay %d\n", delay)
136+
fmt.Fprintf(w, "Cycling with a delay of %d milliseconds", delay)
137+
138+
// main loop
139+
go func() {
140+
timer := time.Tick(time.Millisecond * time.Duration(delay))
141+
for _ = range timer {
142+
143+
if control == 0 { break }
144+
145+
// make the jump in color
146+
if direction == 0 {
147+
if color == 0 {
148+
red = red + jump
149+
} else { green = green + jump }
150+
} else if direction == 1 {
151+
if color == 0 {
152+
red = red - jump
153+
} else { green = green - jump }
154+
}
155+
// set the lights
156+
setRGB(red, green, blue)
157+
158+
// switch colors & direction if at bottom of range
159+
if color == 0 && direction == 1 && red == 0 { color = 1; direction = 0;
160+
} else if color == 1 && direction == 1 && green == 0 { color = 0; direction = 0; }
161+
162+
// if at top of range, switch direction and keep color
163+
if (red >= 255 || green >= 255) && direction == 0 {
164+
direction = 1
165+
}
166+
167+
if control == 0 { break }
168+
169+
// fmt.Printf("Red: %d, Green: %d, Blue: %d\n", red, green, blue)
170+
}
171+
fmt.Printf("Cycle timer broken")
172+
}()
173+
}
174+
175+
105176

106177

107178
func clear(c web.C, w http.ResponseWriter, r *http.Request) {
@@ -153,6 +224,7 @@ func main() {
153224
goji.Get("/cycle", cycle)
154225
goji.Get("/cycle/:delay", cycle)
155226
goji.Get("/christmas", christmas)
227+
goji.Get("/christmas_fade", christmas_fade)
156228
goji.Get("/clear", clear)
157229
goji.Get("/setrgb/:red/:green/:blue", websetRGB)
158230

0 commit comments

Comments
 (0)