forked from hybridgroup/gopherbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisor.go
157 lines (131 loc) · 3.32 KB
/
visor.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
package gopherbot
import (
"image/color"
"machine"
"tinygo.org/x/drivers/ws2812"
)
// VisorDevice controls the Gopherbot Visor Neopixel LED.
type VisorDevice struct {
ws2812.Device
LED []color.RGBA
rg bool
forward bool
pos int
}
// Visor returns a new VisorDevice to control Gopherbot Visor.
func Visor() *VisorDevice {
neo := machine.A3
neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
v := ws2812.New(neo)
return &VisorDevice{
Device: v,
LED: make([]color.RGBA, VisorLEDCount),
}
}
// Show sets the visor to display the current LED array state.
func (v *VisorDevice) Show() {
v.WriteColors(v.LED)
}
// Off turns off all the LEDs.
func (v *VisorDevice) Off() {
v.Clear()
}
// Clear clears the visor.
func (v *VisorDevice) Clear() {
for i := range v.LED {
v.LED[i] = color.RGBA{R: 0x00, G: 0x00, B: 0x00}
}
v.Show()
}
// Red turns all of the Visor LEDs red.
func (v *VisorDevice) Red() {
for i := range v.LED {
v.LED[i] = color.RGBA{R: 0xff, G: 0x00, B: 0x00}
}
v.Show()
}
// Green turns all of the Visor LEDs green.
func (v *VisorDevice) Green() {
for i := range v.LED {
v.LED[i] = color.RGBA{R: 0x00, G: 0xff, B: 0x00}
}
v.Show()
}
// Blue turns all of the Visor LEDs blue.
func (v *VisorDevice) Blue() {
for i := range v.LED {
v.LED[i] = color.RGBA{R: 0x00, G: 0x00, B: 0xff}
}
v.Show()
}
// Purple turns all of the Visor LEDs purple.
func (v *VisorDevice) Purple() {
for i := range v.LED {
v.LED[i] = color.RGBA{R: 0x80, G: 0x00, B: 0x80}
}
v.Show()
}
// Xmas light style
func (v *VisorDevice) Xmas() {
v.rg = !v.rg
for i := range v.LED {
v.rg = !v.rg
if v.rg {
v.LED[i] = color.RGBA{R: 0xff, G: 0x00, B: 0x00}
} else {
v.LED[i] = color.RGBA{R: 0x00, G: 0xff, B: 0x00}
}
}
v.Show()
}
// Cylon visor mode.
func (v *VisorDevice) Cylon() {
if v.forward {
v.pos += 2
if v.pos >= VisorLEDCount {
v.pos = VisorLEDCount - 2
v.forward = false
}
} else {
v.pos -= 2
if v.pos < 0 {
v.pos = 0
v.forward = true
}
}
for i := 0; i < VisorLEDCount; i += 2 {
if i == v.pos {
v.LED[i] = color.RGBA{R: 0xff, G: 0x00, B: 0x00}
v.LED[i+1] = color.RGBA{R: 0xff, G: 0x00, B: 0x00}
} else {
v.LED[i] = color.RGBA{R: 0x00, G: 0x00, B: 0x00}
v.LED[i+1] = color.RGBA{R: 0x00, G: 0x00, B: 0x00}
}
}
v.Show()
}
// Rainbow visor mode.
func (v *VisorDevice) Rainbow() {
var r map[int]color.RGBA
r = make(map[int]color.RGBA)
// Pick some rainbowish colors for the map, as many as you want
// (If you have more colors than LEDs, the remaining colors won't appear)
r[0] = color.RGBA{R: 0xff, G: 0xff, B: 0xff} // White
r[1] = color.RGBA{R: 0xff, G: 0xff, B: 0x00} // Yellow
r[2] = color.RGBA{R: 0xff, G: 0x7f, B: 0x00} // Orange
r[3] = color.RGBA{R: 0xff, G: 0x00, B: 0x00} // Red
r[4] = color.RGBA{R: 0xff, G: 0x00, B: 0x7f} // Pink
r[5] = color.RGBA{R: 0x94, G: 0x00, B: 0xd3} // Violet
r[6] = color.RGBA{R: 0x4b, G: 0x00, B: 0x82} // Purple
r[7] = color.RGBA{R: 0x00, G: 0x00, B: 0xff} // Blue
r[8] = color.RGBA{R: 0x46, G: 0xf0, B: 0xf0} // Cyan
r[9] = color.RGBA{R: 0x00, G: 0x98, B: 0x88} // Teal
r[10] = color.RGBA{R: 0x00, G: 0xff, B: 0x00} // Green
r[11] = color.RGBA{R: 0xbc, G: 0xf6, B: 0x0c} // Lime
m := len(r)
// For each position, choose a color in the map
for i := 0; i < VisorLEDCount; i ++ {
v.LED[i] = r[i%m]
}
v.Show()
}