Skip to content

Commit

Permalink
Reorganization for clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Suderman committed Jan 19, 2020
1 parent a8a38f5 commit 8fb91b1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 74 deletions.
61 changes: 48 additions & 13 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,58 @@ package main

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

// On turns on the lights
func On(colorName string) {
opt := ws2811.DefaultOptions
func init() {
rootCmd.AddCommand(onCmd)
rootCmd.AddCommand(offCmd)
onCmd.Flags().StringVarP(&colorName, "color", "c", "white", "The color to turn the lights on to.")

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

var onCmd = &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) {
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.display(colors[colorName], 0)
},
}

var offCmd = &cobra.Command{
Use: "off",
Short: "Turn off the lights.",
Long: `Turns off the lights.`,
Run: func(cmd *cobra.Command, args []string) {
opt := ws2811.DefaultOptions

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

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

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

_ = cw.on(colors[colorName])
_ = cw.display(off, 0)
},
}
50 changes: 31 additions & 19 deletions demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,44 @@ package main

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

// Demo runs a demo of the lights
func Demo() {
opt := ws2811.DefaultOptions
func init() {
rootCmd.AddCommand(demoCmd)

opt.Channels[0].Brightness = brightness
opt.Channels[0].LedCount = ledCount
demoCmd.Flags().IntVar(&demoDelay, "speed", 100, "The delay in ms of the demo program.")
demoCmd.Flags().IntVar(&demoCount, "count", 1, "The number of loops to run the demo.")
}

var demoCmd = &cobra.Command{
Use: "demo",
Short: "Run a demo.",
Long: `Runs a demo.`,
Run: func(cmd *cobra.Command, args []string) {

opt := ws2811.DefaultOptions

dev, err := ws2811.MakeWS2811(&opt)
checkError(err)
opt.Channels[0].Brightness = brightness
opt.Channels[0].LedCount = ledCount

cw := &colorWipe{
ws: dev,
delay: demoDelay,
}
checkError(cw.setup())
defer dev.Fini()
dev, err := ws2811.MakeWS2811(&opt)
checkError(err)

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

for i := 1; i < (demoCount + 1); i++ {
for colorName, color := range colors {
klog.Infof("displaying: %s", colorName)
_ = cw.display(color)
for i := 1; i < (demoCount + 1); i++ {
for colorName, color := range colors {
klog.Infof("displaying: %s", colorName)
_ = cw.display(color, demoDelay)
}
}
}

_ = cw.display(off)
_ = cw.display(off, demoDelay)
},
}
29 changes: 1 addition & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
brightness int
demoDelay int
demoCount int
color string
colorName string
)

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

//Commands
rootCmd.AddCommand(demoCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(onCmd)

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

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

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

var demoCmd = &cobra.Command{
Use: "demo",
Short: "Run a demo.",
Long: `Runs a demo.`,
Run: func(cmd *cobra.Command, args []string) {
Demo()
},
}

var onCmd = &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
17 changes: 3 additions & 14 deletions neopixel.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,20 @@ func checkError(err error) {
}

type colorWipe struct {
ws wsEngine
delay int
ws wsEngine
}

func (cw *colorWipe) setup() error {
return cw.ws.Init()
}

func (cw *colorWipe) display(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
}
time.Sleep(time.Duration(cw.delay) * time.Millisecond)
}
return nil
}

func (cw *colorWipe) on(color uint32) error {
func (cw *colorWipe) display(color uint32, delay int) 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
}
time.Sleep(time.Duration(delay) * time.Millisecond)
}
return nil
}

0 comments on commit 8fb91b1

Please sign in to comment.