Neale Pickett
·
2026-02-12
neopixel.go
1package led
2
3import (
4 "machine"
5 "image/color"
6 "tinygo.org/x/drivers/ws2812"
7)
8
9type NeoPixel struct {
10 power machine.Pin
11 device ws2812.Device
12}
13
14func NewNeoPixel(data, power machine.Pin) *NeoPixel {
15 return &NeoPixel{
16 power: power,
17 device: ws2812.NewWS2812(data),
18 }
19}
20
21func (p *NeoPixel) Enable() error {
22 p.device.Pin.Configure(machine.PinConfig{machine.PinOutput})
23 p.power.Configure(machine.PinConfig{machine.PinOutput})
24 p.power.High()
25 return nil
26}
27
28func (p *NeoPixel) Disable() {
29 p.power.Low()
30}
31
32func (p *NeoPixel) SetColor(c color.Color) {
33 // sigh. https://github.com/tinygo-org/drivers/issues/837
34 rgba := color.RGBAModel.Convert(c).(color.RGBA)
35 p.device.WriteColors([]color.RGBA{rgba})
36}
37