mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-05 07:27:49 +00:00
b3af3f594e
Previously, a new implementation had to be written for each processor
speed. This is tedious and error-prone, therefore I've rewritten all of
the assembly code using a tool that will calculate exactly how many NOP
instructions are required.
I've also switched from build tags to a switch statement over all
processor speeds.
All in all, this means that:
- Chips with a supported CPU performance will automatically be
supported (no build tags necessary).
- Adding a new chip is as easy as adding the MHz count to the
generator script and to the switch statement.
Right now this is only for the Cortex-M, but it's certainly feasible to
extend this to other architectures such as the AVR.
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
// Package ws2812 implements a driver for WS2812 and SK6812 RGB LED strips.
|
|
package ws2812 // import "tinygo.org/x/drivers/ws2812"
|
|
|
|
//go:generate go run gen-ws2812-arm.go
|
|
|
|
import (
|
|
"errors"
|
|
"image/color"
|
|
"machine"
|
|
)
|
|
|
|
var errUnknownClockSpeed = errors.New("ws2812: unknown CPU clock speed")
|
|
|
|
// Device wraps a pin object for an easy driver interface.
|
|
type Device struct {
|
|
Pin machine.Pin
|
|
}
|
|
|
|
// New returns a new WS2812 driver. It does not touch the pin object: you have
|
|
// to configure it as an output pin before calling New.
|
|
func New(pin machine.Pin) Device {
|
|
return Device{pin}
|
|
}
|
|
|
|
// Write the raw bitstring out using the WS2812 protocol.
|
|
func (d Device) Write(buf []byte) (n int, err error) {
|
|
for _, c := range buf {
|
|
d.WriteByte(c)
|
|
}
|
|
return len(buf), nil
|
|
}
|
|
|
|
// Write the given color slice out using the WS2812 protocol.
|
|
// Colors are sent out in the usual GRB format.
|
|
func (d Device) WriteColors(buf []color.RGBA) error {
|
|
for _, color := range buf {
|
|
d.WriteByte(color.G) // green
|
|
d.WriteByte(color.R) // red
|
|
d.WriteByte(color.B) // blue
|
|
}
|
|
return nil
|
|
}
|