-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathledrace.go
338 lines (302 loc) · 7.86 KB
/
ledrace.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers/buzzer"
"tinygo.org/x/drivers/ws2812"
)
// Define variables for our game
const ACCELERATION = 0.2
const TRACKLENGHT = 300
const FRICTION = 0.015
const GRAVITY = 0.003
const PLAYERS = 4
const LAPS = 3
// Define the track itself
type Track struct {
gravity []uint8
leds []color.RGBA
ws ws2812.Device
}
// Player
type Player struct {
button machine.Pin
led machine.Pin
pressed bool
speed float32
position float32
color color.RGBA
loop uint8
}
var players [4]Player
var track Track
var bzr buzzer.Device
var idleTime time.Time
var activity bool
var black = color.RGBA{0, 0, 0, 0}
var red = color.RGBA{255, 0, 0, 255}
var green = color.RGBA{0, 255, 0, 255}
var yellow = color.RGBA{255, 255, 0, 255}
var blue = color.RGBA{0, 0, 255, 255}
func main() {
// configure the hardware (depends on each board)
configureHardware()
// init for the track
track.leds = make([]color.RGBA, TRACKLENGHT)
track.gravity = make([]uint8, TRACKLENGHT)
for i := 0; i < TRACKLENGHT; i++ {
track.gravity[i] = 127
}
// create a "ramp"
setRamp(12, 90, 100, 110)
// If at start, player 0 is pressed, enter configuration mode
if !players[0].button.Get() {
showGravity()
}
// start the race (reset values)
startRace()
for {
activity = false
for p := uint8(0); p < PLAYERS; p++ {
activity = activity || getPlayerInput(p)
// accelerate or slow a player depending on the gravity
// it slows the player if going UP a RAMP
// it accelerates the player if going DOWN a RAMP
gravity := track.gravity[uint16(players[p].position)%TRACKLENGHT]
if gravity < 127 {
players[p].speed -= GRAVITY * float32(127-gravity)
}
if gravity > 127 {
players[p].speed += GRAVITY * float32(gravity-127)
}
// slows a player due to friction, so if no clicking it will eventually stops
players[p].speed -= players[p].speed * FRICTION
// move the player according to its speed
players[p].position += players[p].speed
// if reached the end of the track, start again (assume a closed loop track)
if players[p].position > TRACKLENGHT*float32(players[p].loop) {
players[p].loop++
}
}
// "paint" the track's players on their positions
paintTrack()
maxPosition := players[0].position
winner := uint8(0)
// check for the player in first position
for p := uint8(0); p < PLAYERS; p++ {
if players[p].position > maxPosition {
maxPosition = players[p].position
winner = p
}
}
// declare a winner
if maxPosition > LAPS*TRACKLENGHT {
time.Sleep(1500 * time.Millisecond)
finishRace(winner)
}
// if no activity in a while, enter demo mode
if activity {
idleTime = time.Now()
} else if time.Since(idleTime) > 30*time.Second {
idleRace()
}
time.Sleep(10 * time.Millisecond)
}
}
// getPlayerInput gets the player's input (if button is pressed or not)
func getPlayerInput(p uint8) bool {
pressed := players[p].button.Get()
if players[p].pressed && !pressed {
players[p].pressed = false
players[p].speed += ACCELERATION
players[p].led.High()
return true
} else if !players[p].pressed && pressed {
players[p].pressed = true
players[p].led.Low()
}
return false
}
// paintTrack "paint" each player's position on the LED strip
func paintTrack() {
for i := 0; i < TRACKLENGHT; i++ {
track.leds[i] = black
}
for p := uint8(0); p < PLAYERS; p++ {
for l := uint8(0); l < players[p].loop; l++ {
track.leds[(uint32(players[p].position)-uint32(l))%TRACKLENGHT] = players[p].color
}
}
track.ws.WriteColors(track.leds)
}
// startRace resets the variables and make a short melody to simulate the start race light
func startRace() {
resetPlayers()
idleTime = time.Now()
time.Sleep(2 * time.Second)
for i := 0; i < TRACKLENGHT; i++ {
track.leds[i] = black
}
track.ws.WriteColors(track.leds)
time.Sleep(1 * time.Second)
track.leds[12] = red
track.leds[11] = red
track.ws.WriteColors(track.leds)
bzr.Tone(buzzer.E4, buzzer.Quarter)
time.Sleep(400 * time.Millisecond)
track.leds[12] = black
track.leds[11] = black
track.leds[10] = yellow
track.leds[9] = yellow
track.ws.WriteColors(track.leds)
bzr.Tone(buzzer.E4, buzzer.Quarter)
time.Sleep(400 * time.Millisecond)
track.leds[10] = black
track.leds[9] = black
track.leds[8] = green
track.leds[7] = green
track.ws.WriteColors(track.leds)
bzr.Tone(buzzer.B4, buzzer.Quarter)
time.Sleep(400 * time.Millisecond)
}
// finishRace makes a short melody and blink the LED strip in the winner's color
func finishRace(winner uint8) {
resetPlayers()
for k := 0; k < 6; k++ {
for i := 0; i < TRACKLENGHT; i++ {
track.leds[i] = players[winner].color
}
track.ws.WriteColors(track.leds)
time.Sleep(300 * time.Millisecond)
if k == 1 {
// winning melody
players[winner].led.High()
bzr.Tone(buzzer.C4, 0.25)
time.Sleep(100 * time.Millisecond)
players[winner].led.Low()
bzr.Tone(buzzer.C4, 0.25)
time.Sleep(100 * time.Millisecond)
players[winner].led.Low()
bzr.Tone(buzzer.C4, 0.25)
time.Sleep(100 * time.Millisecond)
players[winner].led.High()
bzr.Tone(buzzer.C4, 0.25)
time.Sleep(100 * time.Millisecond)
players[winner].led.Low()
bzr.Tone(buzzer.G3, 0.5)
time.Sleep(200 * time.Millisecond)
players[winner].led.High()
bzr.Tone(buzzer.A3, 0.5)
time.Sleep(200 * time.Millisecond)
players[winner].led.Low()
bzr.Tone(buzzer.C4, 0.25)
time.Sleep(100 * time.Millisecond)
players[winner].led.High()
bzr.Tone(buzzer.A3, 0.25)
time.Sleep(100 * time.Millisecond)
players[winner].led.Low()
bzr.Tone(buzzer.C4, 0.5)
time.Sleep(100 * time.Millisecond)
players[winner].led.High()
}
for i := 0; i < TRACKLENGHT; i++ {
track.leds[i] = black
}
track.ws.WriteColors(track.leds)
time.Sleep(300 * time.Millisecond)
}
players[winner].led.Low()
startRace()
}
// resetPlayers resets the players' variables
func resetPlayers() {
for p := uint8(0); p < PLAYERS; p++ {
players[p].speed = 0
players[p].position = 0
players[p].pressed = false
players[p].loop = 0
players[p].led.Low()
}
}
// idleRace is the demo mode (rainbow colors)
func idleRace() {
var k uint16
activity = false
for {
for i := 0; i < TRACKLENGHT; i++ {
track.leds[i] = getRainbowRGB(uint8((i*256)/TRACKLENGHT) + uint8(k))
}
track.ws.WriteColors(track.leds)
k = (k + 1) % 255
for p := uint8(0); p < PLAYERS; p++ {
if getPlayerInput(p) {
activity = true
break
}
}
if activity {
break
}
time.Sleep(16 * time.Millisecond)
}
startRace()
}
// getRainbowRGB returns the color from the rainbow circle
func getRainbowRGB(i uint8) color.RGBA {
if i < 85 {
return color.RGBA{i * 3, 255 - i*3, 0, 255}
} else if i < 170 {
i -= 85
return color.RGBA{255 - i*3, 0, i * 3, 255}
}
i -= 170
return color.RGBA{0, i * 3, 255 - i*3, 255}
}
// setRamps modifies the track's gravity to create a ramp
func setRamp(height uint8, start uint32, middle uint32, end uint32) {
for i := uint32(0); i < (middle - start); i++ {
track.gravity[start+i] = uint8(127 - float32(i)*float32(height)/float32(middle-start))
}
track.gravity[middle] = 127
for i := uint32(0); i < (end - middle); i++ {
track.gravity[middle+i+1] = uint8(127 + float32(height)*(1-float32(i)/float32(middle-start)))
}
}
// showGravity "paint" the ramp on the LED strip
func showGravity() {
for i := 0; i < TRACKLENGHT; i++ {
grav := track.gravity[i]
r := uint8(2)
g := uint8(2)
if grav < 127 {
r = 2 * (127 - grav)
}
if grav > 127 {
g = (grav - 127) * 2
}
track.leds[i] = color.RGBA{r, g, 0, 255}
}
track.ws.WriteColors(track.leds)
longpress := 0
onoff := false
for {
if !players[0].button.Get() {
longpress++
if longpress > 20 {
break
}
} else {
longpress = 0
}
if onoff {
players[0].led.Low()
} else {
players[0].led.High()
}
onoff = !onoff
time.Sleep(100 * time.Millisecond)
}
players[0].led.Low()
time.Sleep(2 * time.Second)
}