Skip to content

Commit

Permalink
refactored to use color instead of uint32 everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Suderman committed Jan 20, 2020
1 parent 9ecd7b5 commit 27af166
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
6 changes: 3 additions & 3 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ var onCmd = &cobra.Command{
Short: "Turn on the lights.",
Long: `Turns on the lights to a specific color.`,
Run: func(cmd *cobra.Command, args []string) {
led, err := newLEDArray()
led, err := newledArray()
if err != nil {
klog.Fatal(err)
}
defer led.ws.Fini()
led.color = colors[colorName]
led.color = HexToColor(colors[colorName])
_ = led.fade(led.color, onBrightness)
},
}
Expand All @@ -35,7 +35,7 @@ var offCmd = &cobra.Command{
Short: "Turn off the lights.",
Long: `Turns off the lights.`,
Run: func(cmd *cobra.Command, args []string) {
led, err := newLEDArray()
led, err := newledArray()
if err != nil {
klog.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var demoCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

// Initialize the LEDs
led, err := newLEDArray()
led, err := newledArray()
if err != nil {
klog.Fatal(err)
}
Expand All @@ -53,7 +53,7 @@ var demoCmd = &cobra.Command{
for i := 0; i < (demoCount); i++ {
for colorName, color := range colors {
klog.Infof("displaying: %s", colorName)
_ = led.display(color, demoDelay, demoBrightness)
_ = led.display(HexToColor(color), demoDelay, demoBrightness)
}
_ = led.fade(led.color, minBrightness)
time.Sleep(500 * time.Millisecond)
Expand All @@ -62,7 +62,7 @@ var demoCmd = &cobra.Command{
klog.V(3).Infof("starting color gradient")
colorList := GradientColorList(demoGradient, demoGradientLength)
for _, gradColor := range colorList {
_ = led.display(ColorToUint32(gradColor), 0, demoBrightness)
_ = led.display(gradColor, 0, demoBrightness)
time.Sleep(time.Duration(demoDelay) * time.Nanosecond)
}
}
Expand Down
4 changes: 2 additions & 2 deletions homekit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func startHomekit() {
klog.Fatal(err)
}

led, err := newLEDArray()
led, err := newledArray()
if err != nil {
klog.Fatal(err)
}
Expand Down Expand Up @@ -72,7 +72,7 @@ func startHomekit() {

ac.Lightbulb.Brightness.OnValueRemoteUpdate(func(value int) {
klog.Infof("homekit brightness set to: %d", value)
err = led.fade(colors["white"], scaleHomekitBrightness(value))
err = led.fade(HexToColor(colors["white"]), scaleHomekitBrightness(value))
if err != nil {
klog.Error(err)
}
Expand Down
52 changes: 27 additions & 25 deletions neopixel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package main
import (
"time"

"github.com/lucasb-eyer/go-colorful"
ws2811 "github.com/rpi-ws281x/rpi-ws281x-go"
"k8s.io/klog"
)

var colors = map[string]uint32{
"blue": uint32(0x0000ff),
"green": uint32(0x00ff00),
"yellow": uint32(0xffaf33),
"purple": uint32(0xaf33ff),
"red": uint32(0xff0000),
"teal": uint32(0x33ffd1),
"pink": uint32(0xff08c7),
"white": uint32(0xffffff),
"black": uint32(0x000000), // This basically equates to off.
var colors = map[string]string{
"blue": "#0000ff",
"green": "#00ff00",
"yellow": "#ffaf33",
"purple": "#af33ff",
"red": "#ff0000",
"teal": "#33ffd1",
"pink": "#ff08c7",
"white": "#ffffff",
"black": "#000000", // This basically equates to off.
}

type wsEngine interface {
Expand All @@ -28,14 +29,14 @@ type wsEngine interface {
SetBrightness(channel int, brightness int)
}

// LEDArray is a struct for interacting with LEDs
type LEDArray struct {
// ledArray is a struct for interacting with LEDs
type ledArray struct {
ws wsEngine
brightness int
color uint32
color colorful.Color
}

func newLEDArray() (*LEDArray, error) {
func newledArray() (*ledArray, error) {
// Setup the LED lights
opt := ws2811.DefaultOptions
opt.Channels[0].Brightness = maxBrightness
Expand All @@ -46,7 +47,7 @@ func newLEDArray() (*LEDArray, error) {
return nil, err
}

led := &LEDArray{
led := &ledArray{
ws: dev,
}

Expand All @@ -56,23 +57,24 @@ func newLEDArray() (*LEDArray, error) {
klog.Error(err)
return nil, err
}
// Start off
// Start with brightness off and color white
led.brightness = minBrightness
led.color = colors["white"]
led.color = HexToColor(colors["white"])

return led, nil
}

// display changes all of the LEDs one at a time
// delay: sets the time between each LED coming on
// brightness: sets the brightness for the entire thing
func (led *LEDArray) display(color uint32, delay int, brightness int) error {
klog.V(6).Infof("setting led array to color: %d, delay: %d, brightness: %d", color, delay, brightness)
func (led *ledArray) display(color colorful.Color, delay int, brightness int) error {
klog.V(6).Infof("setting led array to color: %v, delay: %d, brightness: %d", color, delay, brightness)
err := led.setBrightness(brightness)
if err != nil {
return err
}
for i := 0; i < len(led.ws.Leds(0)); i++ {
led.ws.Leds(0)[i] = color
led.ws.Leds(0)[i] = ColorToUint32(color)
led.color = color
klog.V(10).Infof("setting led %d", i)
if err := led.ws.Render(); err != nil {
Expand All @@ -87,7 +89,7 @@ func (led *LEDArray) display(color uint32, delay int, brightness int) error {
// setBrightness turns the LED array to a brightness value
// and sets the led.brightness value accordingly
// if it goes out of bounds, it will be set to min or max
func (led *LEDArray) setBrightness(value int) error {
func (led *ledArray) setBrightness(value int) error {
value = brightnessBounds(value)
klog.V(8).Infof("setting brightness to %d", value)
led.ws.SetBrightness(0, value)
Expand Down Expand Up @@ -118,15 +120,15 @@ func brightnessBounds(value int) int {
}

// fade goes to a new brightness in the duration specified
func (led *LEDArray) fade(color uint32, target int) error {

func (led *ledArray) fade(color colorful.Color, target int) error {
klog.V(8).Infof("fading brightness to %d", target)
klog.V(8).Infof("setting color to %d", color)
klog.V(8).Infof("setting color to %v", color)
ramp := stepRamp(float64(led.brightness), float64(target), float64(fadeDuration))

//Set the color on all the LEDs
led.color = color
for i := 0; i < len(led.ws.Leds(0)); i++ {
led.ws.Leds(0)[i] = color
led.ws.Leds(0)[i] = ColorToUint32(color)
}

for _, step := range ramp {
Expand Down

0 comments on commit 27af166

Please sign in to comment.