neopixel: WIP on implementation for GPIO-based neopixels
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
88167d31d0
commit
c166d91328
|
@ -1,6 +1,8 @@
|
|||
package gpio
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gobot.io/x/gobot"
|
||||
)
|
||||
|
||||
|
@ -9,6 +11,7 @@ type NeopixelDriver struct {
|
|||
name string
|
||||
pin string
|
||||
pixelCount uint16
|
||||
pixels []uint32
|
||||
connection DigitalWriter
|
||||
gobot.Eventer
|
||||
}
|
||||
|
@ -20,6 +23,7 @@ func NewNeopixelDriver(a DigitalWriter, pin string, pixelCount uint16) *Neopixel
|
|||
connection: a,
|
||||
pin: pin,
|
||||
pixelCount: pixelCount,
|
||||
pixels: make([]uint32, pixelCount),
|
||||
Eventer: gobot.NewEventer(),
|
||||
}
|
||||
|
||||
|
@ -57,11 +61,23 @@ func (neo *NeopixelDriver) Off() error {
|
|||
|
||||
// Show activates all the Neopixels in the strip
|
||||
func (neo *NeopixelDriver) Show() error {
|
||||
for i := 0; i < int(neo.pixelCount); i++ {
|
||||
r := byte((neo.pixels[i] >> 16) & 0xff)
|
||||
g := byte((neo.pixels[i] >> 8) & 0xff)
|
||||
b := byte(neo.pixels[i] & 0xff)
|
||||
|
||||
neo.writeByte(g)
|
||||
neo.writeByte(r)
|
||||
neo.writeByte(b)
|
||||
neo.reset()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetPixel sets the color of one specific Neopixel in the strip
|
||||
func (neo *NeopixelDriver) SetPixel(pix uint16, color uint32) error {
|
||||
neo.pixels[pix] = color
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -69,3 +85,46 @@ func (neo *NeopixelDriver) SetPixel(pix uint16, color uint32) error {
|
|||
func (neo *NeopixelDriver) SetConfig(pin uint8, len uint16) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// writePixel
|
||||
func (neo *NeopixelDriver) writeByte(b byte) error {
|
||||
for i := 0; i < 8; i++ {
|
||||
if hasBit(b, i) {
|
||||
neo.write1()
|
||||
} else {
|
||||
neo.write0()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// write0
|
||||
func (neo *NeopixelDriver) write0() error {
|
||||
neo.connection.DigitalWrite(neo.pin, 1)
|
||||
time.Sleep(350 * time.Nanosecond)
|
||||
neo.connection.DigitalWrite(neo.pin, 0)
|
||||
time.Sleep(800 * time.Nanosecond)
|
||||
return nil
|
||||
}
|
||||
|
||||
// write1
|
||||
func (neo *NeopixelDriver) write1() error {
|
||||
neo.connection.DigitalWrite(neo.pin, 1)
|
||||
time.Sleep(700 * time.Nanosecond)
|
||||
neo.connection.DigitalWrite(neo.pin, 0)
|
||||
time.Sleep(600 * time.Nanosecond)
|
||||
return nil
|
||||
}
|
||||
|
||||
// frame reset
|
||||
func (neo *NeopixelDriver) reset() error {
|
||||
neo.connection.DigitalWrite(neo.pin, 0)
|
||||
time.Sleep(300 * time.Microsecond)
|
||||
return nil
|
||||
}
|
||||
|
||||
// check to see if a particular bit is set
|
||||
func hasBit(n byte, pos int) bool {
|
||||
val := n & (1 << uint(pos))
|
||||
return (val > 0)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package gpio
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gobot.io/x/gobot"
|
||||
"gobot.io/x/gobot/gobottest"
|
||||
)
|
||||
|
||||
var _ gobot.Driver = (*NeopixelDriver)(nil)
|
||||
|
||||
func initTestNeopixelDriver(conn DigitalWriter) *NeopixelDriver {
|
||||
return NewNeopixelDriver(conn, "1", 5)
|
||||
}
|
||||
|
||||
func TestNeopixelDriverDefaultName(t *testing.T) {
|
||||
g := initTestNeopixelDriver(newGpioTestAdaptor())
|
||||
gobottest.Assert(t, strings.HasPrefix(g.Name(), "Neopixel"), true)
|
||||
}
|
||||
|
||||
func TestNeopixelDriverSetName(t *testing.T) {
|
||||
g := initTestNeopixelDriver(newGpioTestAdaptor())
|
||||
g.SetName("mybot")
|
||||
gobottest.Assert(t, g.Name(), "mybot")
|
||||
}
|
Loading…
Reference in New Issue