ws2812: add brightness control

This commit is contained in:
Carlos Henrique Guardão Gandarez
2026-04-01 14:35:53 -03:00
committed by Ron Evans
parent e232a4f136
commit 21a7d0a96a
2 changed files with 33 additions and 13 deletions
+28 -10
View File
@@ -18,7 +18,8 @@ var errUnknownClockSpeed = errors.New("ws2812: unknown CPU clock speed")
// Device wraps a pin object for an easy driver interface. // Device wraps a pin object for an easy driver interface.
type Device struct { type Device struct {
Pin machine.Pin Pin machine.Pin
writeColorFunc func(Device, []color.RGBA) error brightness uint8
writeColorFunc func(Device, []color.RGBA, uint8) error
} }
// deprecated, use NewWS2812 or NewSK6812 depending on which device you want. // deprecated, use NewWS2812 or NewSK6812 depending on which device you want.
@@ -40,10 +41,16 @@ func NewWS2812(pin machine.Pin) Device {
func NewSK6812(pin machine.Pin) Device { func NewSK6812(pin machine.Pin) Device {
return Device{ return Device{
Pin: pin, Pin: pin,
brightness: 255,
writeColorFunc: writeColorsRGBA, writeColorFunc: writeColorsRGBA,
} }
} }
// SetBrightness sets the global brightness (0-255).
func (d *Device) SetBrightness(b uint8) {
d.brightness = b
}
// Write the raw bitstring out using the WS2812 protocol. // Write the raw bitstring out using the WS2812 protocol.
func (d Device) Write(buf []byte) (n int, err error) { func (d Device) Write(buf []byte) (n int, err error) {
for _, c := range buf { for _, c := range buf {
@@ -55,24 +62,35 @@ func (d Device) Write(buf []byte) (n int, err error) {
// Write the given color slice out using the WS2812 protocol. // Write the given color slice out using the WS2812 protocol.
// Colors are sent out in the usual GRB(A) format. // Colors are sent out in the usual GRB(A) format.
func (d Device) WriteColors(buf []color.RGBA) (err error) { func (d Device) WriteColors(buf []color.RGBA) (err error) {
return d.writeColorFunc(d, buf) return d.writeColorFunc(d, buf, d.brightness)
} }
func writeColorsRGB(d Device, buf []color.RGBA) (err error) { func writeColorsRGB(d Device, buf []color.RGBA, brightness uint8) (err error) {
for _, color := range buf { for _, color := range buf {
d.WriteByte(color.G) // green r, g, b := applyBrightness(color, brightness)
d.WriteByte(color.R) // red d.WriteByte(g) // green
err = d.WriteByte(color.B) // blue d.WriteByte(r) // red
err = d.WriteByte(b) // blue
} }
return return
} }
func writeColorsRGBA(d Device, buf []color.RGBA) (err error) { func writeColorsRGBA(d Device, buf []color.RGBA, brightness uint8) (err error) {
for _, color := range buf { for _, color := range buf {
d.WriteByte(color.G) // green r, g, b := applyBrightness(color, brightness)
d.WriteByte(color.R) // red
d.WriteByte(color.B) // blue d.WriteByte(g) // green
d.WriteByte(r) // red
d.WriteByte(b) // blue
err = d.WriteByte(color.A) // alpha err = d.WriteByte(color.A) // alpha
} }
return return
} }
// applyBrightness scales a color by the brightness value.
func applyBrightness(c color.RGBA, brightness uint8) (r, g, b uint8) {
r = uint8((uint16(c.R) * uint16(brightness)) >> 8)
g = uint8((uint16(c.G) * uint16(brightness)) >> 8)
b = uint8((uint16(c.B) * uint16(brightness)) >> 8)
return
}
+5 -3
View File
@@ -22,10 +22,12 @@ func newWS2812Device(pin machine.Pin) Device {
return Device{Pin: pin, writeColorFunc: writeColorsRGB} return Device{Pin: pin, writeColorFunc: writeColorsRGB}
} }
return Device{ return Device{
Pin: pin, Pin: pin,
writeColorFunc: func(_ Device, buf []color.RGBA) error { brightness: 255,
writeColorFunc: func(_ Device, buf []color.RGBA, brightness uint8) error {
for _, c := range buf { for _, c := range buf {
ws.PutRGB(c.R, c.G, c.B) r, g, b := applyBrightness(c, brightness)
ws.PutRGB(r, g, b)
} }
return nil return nil
}, },