commit 1f278170616ab681e250e5b712d84c397b991dad Author: Ayke van Laethem Date: Mon Oct 15 20:47:33 2018 +0200 Initial commit diff --git a/ws2812/ws2812.go b/ws2812/ws2812.go new file mode 100644 index 0000000..22c9432 --- /dev/null +++ b/ws2812/ws2812.go @@ -0,0 +1,31 @@ +package ws2812 + +import ( + "machine" +) + +type WS2812 struct { + Pin machine.GPIO +} + +func New(pin machine.GPIO) WS2812 { + return WS2812{pin} +} + +// Write the raw bitstring out using the WS2812 protocol. +func (p WS2812) Write(buf []byte) { + for _, c := range buf { + p.WriteByte(c) + } +} + +// Write the given color slice out using the WS2812 protocol. +// Colors are specified in RGB format, and are send out in the common GRB +// format. +func (p WS2812) WriteColors(buf []uint32) { + for _, color := range buf { + p.WriteByte(byte(color >> 8)) // green + p.WriteByte(byte(color >> 16)) // red + p.WriteByte(byte(color >> 0)) // blue + } +} diff --git a/ws2812/ws2812_m0_16m.go b/ws2812/ws2812_m0_16m.go new file mode 100644 index 0000000..e2b59cb --- /dev/null +++ b/ws2812/ws2812_m0_16m.go @@ -0,0 +1,40 @@ +// +build nrf51 + +package ws2812 + +import ( + "device/arm" +) + +// Send a single byte using the WS2812 protocol. +func (p WS2812) WriteByte(c byte) { + // For the Cortex-M0 at 16MHz + portSet, maskSet := p.Pin.PortMaskSet() + portClear, maskClear := p.Pin.PortMaskClear() + + value := uint32(c) << 24 + arm.AsmFull(` + send_bit: + str {maskSet}, {portSet} + lsls {value}, #1 + bcs.n skip_store + str {maskClear}, {portClear} + skip_store: + nop + nop + nop + nop + nop + nop + str {maskClear}, {portClear} + subs {i}, #1 + bne.n send_bit + `, map[string]interface{}{ + "value": value, + "i": 8, + "maskSet": maskSet, + "portSet": portSet, + "maskClear": maskClear, + "portClear": portClear, + }) +}