Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SK6812 Support #608

Closed
SoftTacos opened this issue Oct 14, 2023 · 1 comment
Closed

SK6812 Support #608

SoftTacos opened this issue Oct 14, 2023 · 1 comment

Comments

@SoftTacos
Copy link
Contributor

SoftTacos commented Oct 14, 2023

The WS2812 driver does exactly what you want for RGB strips, but there are a considerable number of RGBA/SK6812 strips as well, SK6812 is the same as WS2812 but it as an additional byte for white/alpha. I went ahead and made the following modifications locally to accommodate for that. If you think this should be a separate driver all together so no breaking changes are added I'm happy to do that too.

Additionally, while I was poking around I noticed WriteByte can return an error, but Write and WriteColors ignore the error. I modified WriteColors to return the error from the last WriteByte call made. Would it make sense to instead check the machine.CPUFrequency() for valid values in Device.New() and return an error there? That way we avoid dealing with errors in Write, WriteByte, and WriteColor. This assumes machine.CPUFrequency() is constant, which the comments say it usually is.

Please let me know if you would like to see any changes made.

Example Code:

type DeviceType uint8

const(
	WS2812 DeviceType = iota // RGB
	SK6812 //RGBA / RGBW
)

// Device wraps a pin object for an easy driver interface.
type Device struct {
	Pin machine.Pin
	DeviceType DeviceType
}

// 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.
// WS2812 is for RGB, SK6812 is for RGBA
func New(pin machine.Pin,deviceType DeviceType) Device {
	return Device{
		Pin:pin,
		DeviceType:deviceType,
	}
}

// Device.Write() is unchanged

// Write the given color slice out using the WS2812 protocol.
// Colors are sent out in the usual GRB(A) format.
func (d Device) WriteColors(buf []color.RGBA) (err error) {
	switch d.DeviceType {
	case WS2812:
		err = d.writeColorsRGB(buf)
	case SK6812:
		err = d.writeColorsRGBA(buf)
	}
	return
}

func (d Device) writeColorsRGB(buf []color.RGBA) (err error) {
	for _, color := range buf {
		d.WriteByte(color.G)       // green
		d.WriteByte(color.R)       // red
		err = d.WriteByte(color.B) // blue
	}
	return
}

func (d Device) writeColorsRGBA(buf []color.RGBA) (err error) {
	for _, color := range buf {
		d.WriteByte(color.G)       // green
		d.WriteByte(color.R)       // red
		d.WriteByte(color.B)       // blue
		err = d.WriteByte(color.A) // alpha
	}
	return
}
This was referenced Oct 14, 2023
@SoftTacos
Copy link
Contributor Author

PR: #610

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant