Skip to content

Commit

Permalink
Adding an on command. Restructuring colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Suderman committed Jan 19, 2020
1 parent 97b99d0 commit b338a1b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
24 changes: 24 additions & 0 deletions control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
ws2811 "github.com/rpi-ws281x/rpi-ws281x-go"
)

// On turns on the lights
func On(colorName string) {
opt := ws2811.DefaultOptions

opt.Channels[0].Brightness = brightness
opt.Channels[0].LedCount = ledCount

dev, err := ws2811.MakeWS2811(&opt)
checkError(err)

cw := &colorWipe{
ws: dev,
}
checkError(cw.setup())
defer dev.Fini()

_ = cw.on(colors[colorName])
}
15 changes: 6 additions & 9 deletions demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
ws2811 "github.com/rpi-ws281x/rpi-ws281x-go"
"k8s.io/klog"
)

// Demo runs a demo of the lights
Expand All @@ -21,15 +22,11 @@ func Demo() {
checkError(cw.setup())
defer dev.Fini()

for i := 1; i < demoCount; i++ {
_ = cw.display(blue)
_ = cw.display(green)
_ = cw.display(yellow)
_ = cw.display(purple)
_ = cw.display(red)
_ = cw.display(teal)
_ = cw.display(pink)
_ = cw.display(white)
for i := 1; i < (demoCount + 1); i++ {
for colorName, color := range colors {
klog.Infof("displaying: %s", colorName)
_ = cw.display(color)
}
}

_ = cw.display(off)
Expand Down
23 changes: 19 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
brightness int
demoDelay int
demoCount int
color string
)

func init() {
Expand All @@ -29,11 +30,16 @@ func init() {
rootCmd.PersistentFlags().IntVar(&brightness, "brightness", 100, "The brightnes to run the LEDs at.")

//Commands
rootCmd.AddCommand(demo)
rootCmd.AddCommand(demoCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(on)

demo.Flags().IntVar(&demoDelay, "speed", 200, "The delay in ms of the demo program.")
demo.Flags().IntVar(&demoCount, "count", 2, "The number of loops to run the demo.")
// Demo Flags
demoCmd.Flags().IntVar(&demoDelay, "speed", 200, "The delay in ms of the demo program.")
demoCmd.Flags().IntVar(&demoCount, "count", 2, "The number of loops to run the demo.")

// On Flags
on.Flags().StringVarP(&color, "color", "c", "white", "The color to turn the lights on to.")

klog.InitFlags(nil)
flag.Parse()
Expand Down Expand Up @@ -69,7 +75,7 @@ var rootCmd = &cobra.Command{
},
}

var demo = &cobra.Command{
var demoCmd = &cobra.Command{
Use: "demo",
Short: "Run a demo.",
Long: `Runs a demo.`,
Expand All @@ -78,6 +84,15 @@ var demo = &cobra.Command{
},
}

var on = &cobra.Command{
Use: "on",
Short: "Turn on the lights.",
Long: `Turns on the lights to a specific color.`,
Run: func(cmd *cobra.Command, args []string) {
On(color)
},
}

// Execute the stuff
func Execute(VERSION string, COMMIT string) {
version = VERSION
Expand Down
33 changes: 22 additions & 11 deletions neopixel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"time"
)

const (
blue = uint32(0x0000ff)
green = uint32(0x00ff00)
yellow = uint32(0xffaf33)
purple = uint32(0xaf33ff)
red = uint32(0xff0000)
teal = uint32(0x33ffd1)
pink = uint32(0xff08c7)
white = uint32(0xffffff)
off = uint32(0x000000)
)
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),
}

const off = uint32(0x000000)

type wsEngine interface {
Init() error
Expand Down Expand Up @@ -49,3 +50,13 @@ func (cw *colorWipe) display(color uint32) error {
}
return nil
}

func (cw *colorWipe) on(color uint32) error {
for i := 0; i < len(cw.ws.Leds(0)); i++ {
cw.ws.Leds(0)[i] = color
if err := cw.ws.Render(); err != nil {
return err
}
}
return nil
}

0 comments on commit b338a1b

Please sign in to comment.