-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackpack.go
86 lines (70 loc) · 1.77 KB
/
backpack.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
package gopherhelmet
import (
"image/color"
"machine"
"tinygo.org/x/drivers/ws2812"
)
// BackpackDevice controls the Gopherbot Backpack LED.
type BackpackDevice struct {
ws2812.Device
LED []color.RGBA
alt bool
}
// Backpack returns a BackpackDevice to control Gopherbot Backpack.
func Backpack() *BackpackDevice {
neo := neopixelsPin
neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
v := ws2812.New(neo)
return &BackpackDevice{
Device: v,
LED: make([]color.RGBA, BackpackLEDCount),
}
}
// Show sets the Backpack to display the current LED array state.
func (b *BackpackDevice) Show() {
b.WriteColors(b.LED)
}
// Off turns off all the LEDs.
func (b *BackpackDevice) Off() {
b.Clear()
}
// Clear clears the Backpack LEDs.
func (b *BackpackDevice) Clear() {
b.SetColor(color.RGBA{R: 0x00, G: 0x00, B: 0x00})
}
// SetColor sets the Backpack LEDs to a single color.
func (b *BackpackDevice) SetColor(color color.RGBA) {
for i := range b.LED {
b.LED[i] = color
}
b.Show()
}
// Red turns all of the Backpack LEDs red.
func (b *BackpackDevice) Red() {
b.SetColor(color.RGBA{R: 0xff, G: 0x00, B: 0x00})
}
// Green turns all of the Backpack LEDs green.
func (b *BackpackDevice) Green() {
b.SetColor(color.RGBA{R: 0x00, G: 0xff, B: 0x00})
}
// Blue turns all of the Backpack LEDs b;ue.
func (b *BackpackDevice) Blue() {
b.SetColor(color.RGBA{R: 0x00, G: 0x00, B: 0xff})
}
// Xmas light style that rotates
func (b *BackpackDevice) Xmas() {
b.Alternate(color.RGBA{R: 0xff, G: 0x00, B: 0x00}, color.RGBA{R: 0x00, G: 0xff, B: 0x00})
}
// Alternate the 2 colors.
func (b *BackpackDevice) Alternate(color1, color2 color.RGBA) {
b.alt = !b.alt
for i := range b.LED {
b.alt = !b.alt
if b.alt {
b.LED[i] = color1
} else {
b.LED[i] = color2
}
}
b.Show()
}