-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneopixel.go
159 lines (145 loc) · 4.74 KB
/
neopixel.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
package ws2812fxfirmata
import (
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/firmata"
)
const (
neopixelCmd = 0x51
neopixelSetBrightness = 0x00
neopixelSetColor = 0x01
neopixelSetPixel = 0x02
neopixelSetStrip = 0x03
neopixelShift = 0x04
neopixelSetMode = 0x05
neopixelModeCycle = 0x06
neopixelStart = 0x07
neopixelStop = 0x08
neopixelConfig = 0x09
// WS2812FX neopixel modes
// a list of modes can be found in the WS2812FX repo:
// https://github.com/kitesurfer1404/WS2812FX/blob/837b7dd843f0e0c38347c13e4b4594d6dc603162/src/WS2812FX.h#L120-L180
FXModeStatic = 0
FXModeBlink = 1
FXModeBreath = 2
FXModeColorWipe = 3
FXModeColorWipeInv = 4
FXModeColorWipeRev = 5
FXModeColorWipeRevInv = 6
FXModeColorWipeRandom = 7
FXModeRandomColor = 8
FXModeSingleDynamic = 9
FXModeMultiDynamic = 10
FXModeRainbow = 11
FXModeRainbowCycle = 12
FXModeScan = 13
FXModeDualScan = 14
FXModeFade = 15
FXModeTheaterChase = 16
FXModeTheaterChaseRainbow = 17
FXModeRunningLights = 18
FXModeTwinkle = 19
FXModeTwinkleRandom = 20
FXModeTwinkleFade = 21
FXModeTwinkleFadeRandom = 22
FXModeSparkle = 23
FXModeFlashSparkle = 24
FXModeHyperSparkle = 25
FXModeStrobe = 26
FXModeStrobeRainbow = 27
FXModeMultiStrobe = 28
FXModeBlinkRainbow = 29
FXModeChaseWhite = 30
FXModeChaseColor = 31
FXModeChaseRandom = 32
FXModeChaseRainbow = 33
FXModeChaseFlash = 34
FXModeChaseFlashRandom = 35
FXModeChaseRainbowWhite = 36
FXModeChaseBlackout = 37
FXModeChaseBlackoutRainbow = 38
FXModeColorSweepRandom = 39
FXModeRunningColor = 40
FXModeRunningRedBlue = 41
FXModeRunningRandom = 42
FXModeLarsonScanner = 43
FXModeComet = 44
FXModeFireworks = 45
FXModeFireworksRandom = 46
FXModeMerryChristmas = 47
FXModeFireFlicker = 48
FXModeFireFlickerSoft = 49
FXModeFireFlickerIntense = 50
FXModeCircusCombustus = 51
FXModeHalloween = 52
FXModeBicolorChase = 53
FXModeTricolorChase = 54
FXModeIcu = 55
FXModeCustom = 56
FXModeCustom0 = 56
FXModeCustom1 = 57
FXModeCustom2 = 58
FXModeCustom3 = 59
)
// NeopixelDriver represents a connection to a NeoPixel
type NeopixelDriver struct {
name string
connection *firmata.Adaptor
gobot.Eventer
}
// NewNeopixelDriver returns a new NeopixelDriver
func NewNeopixelDriver(a *firmata.Adaptor) *NeopixelDriver {
neo := &NeopixelDriver{
name: gobot.DefaultName("Neopixel"),
connection: a,
Eventer: gobot.NewEventer(),
}
return neo
}
// SetConfig sets the pin and pixel count for the Neopixel strip
func (neo *NeopixelDriver) SetConfig(pin uint8, pixelCount uint16) error {
return neo.connection.WriteSysex([]byte{neopixelCmd, neopixelConfig,
byte(pin),
byte(pixelCount & 0x7F),
byte((pixelCount >> 7) & 0x7F),
})
}
// Stop turns off all the Neopixels in the strip
func (neo *NeopixelDriver) Stop() error {
return neo.connection.WriteSysex([]byte{neopixelCmd, neopixelStop})
}
// Start turns on all the Neopixels in the strip
func (neo *NeopixelDriver) Start() error {
return neo.connection.WriteSysex([]byte{neopixelCmd, neopixelStart})
}
// ModeCycle cycles through modes
func (neo *NeopixelDriver) ModeCycle() error {
return neo.connection.WriteSysex([]byte{neopixelCmd, neopixelModeCycle})
}
// SetMode sets the mode
func (neo *NeopixelDriver) SetMode(mode uint8) error {
return neo.connection.WriteSysex([]byte{neopixelCmd, neopixelSetMode, byte(mode)})
}
// SetBrightness sets the brightness
func (neo *NeopixelDriver) SetBrightness(brightness uint8) error {
return neo.connection.WriteSysex([]byte{neopixelCmd, neopixelSetBrightness, byte(brightness)})
}
// SetPixel sets the color of one specific Neopixel in the strip
func (neo *NeopixelDriver) SetPixel(pix uint16, color uint32) error {
return neo.connection.WriteSysex([]byte{neopixelCmd, neopixelSetPixel,
byte(pix & 0x7F),
byte((pix >> 7) & 0x7F),
byte(color & 0x7F),
byte((color >> 7) & 0x7F),
byte((color >> 14) & 0x7F),
byte((color >> 21) & 0x7F),
})
}
// SetColor sets the color of the strip
func (neo *NeopixelDriver) SetColor(color uint32) error {
return neo.connection.WriteSysex([]byte{neopixelCmd, neopixelSetColor,
byte(color & 0x7F),
byte((color >> 7) & 0x7F),
byte((color >> 14) & 0x7F),
byte((color >> 21) & 0x7F),
})
}