diff --git a/.circleci/config.yml b/.circleci/config.yml index 5cc655d..826457f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,7 +6,7 @@ orbs: jobs: test: docker: - - image: circleci/golang:1.12-stretch + - image: circleci/golang:1.13-stretch environment: GO111MODULE: "on" steps: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 553dffa..2ced456 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,7 +25,7 @@ repos: - id: forbid-binary exclude: > (?x)^( - testdata/.+\.png + pkg/color/testdata/.+\.png )$ - id: shellcheck - id: git-check diff --git a/control.go b/cmd/control.go similarity index 60% rename from control.go rename to cmd/control.go index 5e1e62e..73f3633 100644 --- a/control.go +++ b/cmd/control.go @@ -1,8 +1,11 @@ -package main +package cmd import ( "github.com/spf13/cobra" "k8s.io/klog" + + "github.com/sudermanjr/led-controller/pkg/color" + "github.com/sudermanjr/led-controller/pkg/neopixel" ) var onBrightness int @@ -20,13 +23,16 @@ 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 := neopixel.NewLEDArray(minBrightness, maxBrightness, ledCount, fadeDuration) + if err != nil { + klog.Fatal(err) + } + defer led.WS.Fini() + led.Color = color.HexToColor(color.ColorMap[colorName]) + err = led.SetMaxBrightness() if err != nil { klog.Fatal(err) } - defer led.ws.Fini() - led.color = HexToColor(colors[colorName]) - _ = led.fade(onBrightness) }, } @@ -35,11 +41,14 @@ 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 := neopixel.NewLEDArray(minBrightness, maxBrightness, ledCount, fadeDuration) + if err != nil { + klog.Fatal(err) + } + defer led.WS.Fini() + err = led.SetMinBrightness() if err != nil { klog.Fatal(err) } - defer led.ws.Fini() - _ = led.fade(minBrightness) }, } diff --git a/demo.go b/cmd/demo.go similarity index 54% rename from demo.go rename to cmd/demo.go index e36a665..53f02e5 100644 --- a/demo.go +++ b/cmd/demo.go @@ -1,9 +1,13 @@ -package main +package cmd import ( + "time" + "github.com/spf13/cobra" "k8s.io/klog" - "time" + + "github.com/sudermanjr/led-controller/pkg/color" + "github.com/sudermanjr/led-controller/pkg/neopixel" ) var ( @@ -22,18 +26,18 @@ func init() { demoCmd.Flags().IntVar(&demoGradientLength, "gradient-count", 2048, "The number of steps in the gradient.") } -var demoGradient = GradientTable{ - {HexToColor("#9e0142"), 0.0}, - {HexToColor("#d53e4f"), 0.1}, - {HexToColor("#f46d43"), 0.2}, - {HexToColor("#fdae61"), 0.3}, - {HexToColor("#fee090"), 0.4}, - {HexToColor("#ffffbf"), 0.5}, - {HexToColor("#e6f598"), 0.6}, - {HexToColor("#abdda4"), 0.7}, - {HexToColor("#66c2a5"), 0.8}, - {HexToColor("#3288bd"), 0.9}, - {HexToColor("#5e4fa2"), 1.0}, +var demoGradient = color.GradientTable{ + {color.HexToColor("#9e0142"), 0.0}, + {color.HexToColor("#d53e4f"), 0.1}, + {color.HexToColor("#f46d43"), 0.2}, + {color.HexToColor("#fdae61"), 0.3}, + {color.HexToColor("#fee090"), 0.4}, + {color.HexToColor("#ffffbf"), 0.5}, + {color.HexToColor("#e6f598"), 0.6}, + {color.HexToColor("#abdda4"), 0.7}, + {color.HexToColor("#66c2a5"), 0.8}, + {color.HexToColor("#3288bd"), 0.9}, + {color.HexToColor("#5e4fa2"), 1.0}, } var demoCmd = &cobra.Command{ @@ -43,34 +47,34 @@ var demoCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // Initialize the LEDs - led, err := newledArray() + led, err := neopixel.NewLEDArray(minBrightness, maxBrightness, ledCount, fadeDuration) if err != nil { klog.Fatal(err) } - defer led.ws.Fini() + defer led.WS.Fini() - led.brightness = demoBrightness + led.Brightness = demoBrightness // Loops through our list of pre-defined colors and display them in order. for i := 0; i < (demoCount); i++ { - for colorName, color := range colors { + for colorName, colorValue := range color.ColorMap { klog.Infof("displaying: %s", colorName) - led.color = HexToColor(color) - _ = led.display(demoDelay) + led.Color = color.HexToColor(colorValue) + _ = led.Display(demoDelay) } - _ = led.fade(minBrightness) + _ = led.Fade(minBrightness) time.Sleep(500 * time.Millisecond) // Second part of demo - go through a color gradient really fast. klog.V(3).Infof("starting color gradient") - colorList := GradientColorList(demoGradient, demoGradientLength) + colorList := color.GradientColorList(demoGradient, demoGradientLength) for _, gradColor := range colorList { - led.color = gradColor - led.brightness = demoBrightness - _ = led.display(0) + led.Color = gradColor + led.Brightness = demoBrightness + _ = led.Display(0) time.Sleep(time.Duration(demoDelay) * time.Nanosecond) } } - _ = led.fade(minBrightness) + _ = led.Fade(minBrightness) }, } diff --git a/cmd/homekit.go b/cmd/homekit.go new file mode 100644 index 0000000..41c7f38 --- /dev/null +++ b/cmd/homekit.go @@ -0,0 +1,33 @@ +package cmd + +import ( + "github.com/spf13/cobra" + "k8s.io/klog" + + "github.com/sudermanjr/led-controller/pkg/homekit" + "github.com/sudermanjr/led-controller/pkg/neopixel" +) + +var homekitPin string + +func init() { + rootCmd.AddCommand(homekitCmd) + + homekitCmd.Flags().StringVar(&homekitPin, "homekit-pin", "29847290", "The pin that homekit will use to authenticate with this device.") +} + +var homekitCmd = &cobra.Command{ + Use: "homekit", + Short: "Run the lights as a homekit accessory.", + Long: `Run the lights as a homekit accessory.`, + Run: func(cmd *cobra.Command, args []string) { + + led, err := neopixel.NewLEDArray(minBrightness, maxBrightness, ledCount, fadeDuration) + if err != nil { + klog.Fatal(err) + } + defer led.WS.Fini() + + homekit.Start(homekitPin, led) + }, +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..a6ceccb --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,87 @@ +package cmd + +import ( + "flag" + "fmt" + "os" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "k8s.io/klog" +) + +var ( + version = "development" + commit = "n/a" + ledCount int + maxBrightness int + minBrightness int + fadeDuration int + colorName string +) + +func init() { + // Flags + rootCmd.PersistentFlags().IntVarP(&ledCount, "led-count", "l", 12, "The number of LEDs in the array.") + rootCmd.PersistentFlags().IntVar(&maxBrightness, "max-brightness", 200, "The maximum brightness that will work within the 0-250 range.") + rootCmd.PersistentFlags().IntVar(&minBrightness, "min-brightness", 25, "The minimum brightness that will work within the 0-250 range.") + rootCmd.PersistentFlags().IntVarP(&fadeDuration, "fade-duration", "f", 100, "The duration of fade-ins and fade-outs in ms.") + + //Commands + rootCmd.AddCommand(versionCmd) + + klog.InitFlags(nil) + flag.Parse() + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) + + environmentVariables := map[string]string{ + "LED_COUNT": "led-count", + "MAX_BRIGHTNESS": "max-brightness", + "MIN_BRIGHTNESS": "min-brightness", + "FADE_DURTION": "fade-duration", + } + + for env, flag := range environmentVariables { + flag := rootCmd.PersistentFlags().Lookup(flag) + flag.Usage = fmt.Sprintf("%v [%v]", flag.Usage, env) + if value := os.Getenv(env); value != "" { + err := flag.Value.Set(value) + if err != nil { + klog.Errorf("Error setting flag %v to %s from environment variable %s", flag, value, env) + } + } + } +} + +var rootCmd = &cobra.Command{ + Use: "led-controller", + Short: "led-controller", + Long: `A cli for running neopixels`, + Run: func(cmd *cobra.Command, args []string) { + klog.Error("You must specify a sub-command.") + err := cmd.Help() + if err != nil { + klog.Error(err) + } + os.Exit(1) + }, +} + +// Execute the stuff +func Execute(VERSION string, COMMIT string) { + version = VERSION + commit = COMMIT + if err := rootCmd.Execute(); err != nil { + klog.Error(err) + os.Exit(1) + } +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Prints the current version of the tool.", + Long: `Prints the current version.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Version:" + version + " Commit:" + commit) + }, +} diff --git a/main.go b/main.go index 346304e..6c8bdfd 100644 --- a/main.go +++ b/main.go @@ -1,91 +1,16 @@ package main import ( - "flag" - "fmt" - "os" - - "github.com/spf13/cobra" - "github.com/spf13/pflag" - "k8s.io/klog" + "github.com/sudermanjr/led-controller/cmd" ) -func main() { - Execute(version, commit) -} - var ( - version = "development" - commit = "n/a" - ledCount int - maxBrightness int - minBrightness int - fadeDuration int - colorName string + // version is set during build + version = "development" + // commit is set during build + commit = "n/a" ) -func init() { - // Flags - rootCmd.PersistentFlags().IntVarP(&ledCount, "led-count", "l", 12, "The number of LEDs in the array.") - rootCmd.PersistentFlags().IntVar(&maxBrightness, "max-brightness", 200, "The maximum brightness that will work within the 0-250 range.") - rootCmd.PersistentFlags().IntVar(&minBrightness, "min-brightness", 25, "The minimum brightness that will work within the 0-250 range.") - rootCmd.PersistentFlags().IntVarP(&fadeDuration, "fade-duration", "f", 100, "The duration of fade-ins and fade-outs in ms.") - - //Commands - rootCmd.AddCommand(versionCmd) - - klog.InitFlags(nil) - flag.Parse() - pflag.CommandLine.AddGoFlagSet(flag.CommandLine) - - environmentVariables := map[string]string{ - "LED_COUNT": "led-count", - "MAX_BRIGHTNESS": "max-brightness", - "MIN_BRIGHTNESS": "min-brightness", - "FADE_DURTION": "fade-duration", - } - - for env, flag := range environmentVariables { - flag := rootCmd.PersistentFlags().Lookup(flag) - flag.Usage = fmt.Sprintf("%v [%v]", flag.Usage, env) - if value := os.Getenv(env); value != "" { - err := flag.Value.Set(value) - if err != nil { - klog.Errorf("Error setting flag %v to %s from environment variable %s", flag, value, env) - } - } - } -} - -var rootCmd = &cobra.Command{ - Use: "led-controller", - Short: "led-controller", - Long: `A cli for running neopixels`, - Run: func(cmd *cobra.Command, args []string) { - klog.Error("You must specify a sub-command.") - err := cmd.Help() - if err != nil { - klog.Error(err) - } - os.Exit(1) - }, -} - -// Execute the stuff -func Execute(VERSION string, COMMIT string) { - version = VERSION - commit = COMMIT - if err := rootCmd.Execute(); err != nil { - klog.Error(err) - os.Exit(1) - } -} - -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Prints the current version of the tool.", - Long: `Prints the current version.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Version:" + version + " Commit:" + commit) - }, +func main() { + cmd.Execute(version, commit) } diff --git a/color.go b/pkg/color/color.go similarity index 84% rename from color.go rename to pkg/color/color.go index aa1da4b..33fcbe9 100644 --- a/color.go +++ b/pkg/color/color.go @@ -1,4 +1,4 @@ -package main +package color import ( "image" @@ -12,6 +12,19 @@ import ( "k8s.io/klog" ) +//ColorMap is a map of named colors to hex values +var ColorMap = 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. +} + // GradientTable contains the "keypoints" of the colorgradient you want to generate. // The position of each keypoint has to live in the range [0,1] type GradientTable []struct { @@ -47,12 +60,11 @@ func HexToColor(s string) colorful.Color { return c } -// ColorToUint32 converts a color object to a uint32 +// ToUint32 converts a color object to a uint32 // for use by the neopixel -func ColorToUint32(color colorful.Color) uint32 { +func ToUint32(color colorful.Color) uint32 { hex := color.Hex() hex = strings.Replace(hex, "#", "", -1) - klog.V(10).Infof("hex value: %s", hex) value, _ := strconv.ParseUint(hex, 16, 32) return uint32(value) diff --git a/color_test.go b/pkg/color/color_test.go similarity index 96% rename from color_test.go rename to pkg/color/color_test.go index c79c3eb..0117343 100644 --- a/color_test.go +++ b/pkg/color/color_test.go @@ -1,4 +1,4 @@ -package main +package color import ( "fmt" @@ -7,6 +7,7 @@ import ( "github.com/lucasb-eyer/go-colorful" "github.com/stretchr/testify/assert" + "github.com/sudermanjr/led-controller/pkg/utils" ) var testGradient1 = GradientTable{ @@ -71,7 +72,7 @@ func TestColorToUint32(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := ColorToUint32(tt.color) + got := ToUint32(tt.color) assert.Equal(t, tt.want, got) }) } @@ -112,7 +113,7 @@ func TestGradientPNG(t *testing.T) { t.Run(tt.name, func(t *testing.T) { GradientPNG(tt.gradient, tt.h, tt.w) assert.FileExistsf(t, "gradient.png", "gradient.png should exist") - match := deepCompare("gradient.png", "testdata/"+tt.testFile) + match := utils.DeepCompareFiles("gradient.png", "testdata/"+tt.testFile) assert.Truef(t, match, "the files must match") }) os.Remove("gradient.png") diff --git a/testdata/1024x1024-gradient-1.png b/pkg/color/testdata/1024x1024-gradient-1.png similarity index 100% rename from testdata/1024x1024-gradient-1.png rename to pkg/color/testdata/1024x1024-gradient-1.png diff --git a/testdata/1024x40-gradient-1.png b/pkg/color/testdata/1024x40-gradient-1.png similarity index 100% rename from testdata/1024x40-gradient-1.png rename to pkg/color/testdata/1024x40-gradient-1.png diff --git a/testdata/2048x40-gradient-2.png b/pkg/color/testdata/2048x40-gradient-2.png similarity index 100% rename from testdata/2048x40-gradient-2.png rename to pkg/color/testdata/2048x40-gradient-2.png diff --git a/homekit.go b/pkg/homekit/homekit.go similarity index 68% rename from homekit.go rename to pkg/homekit/homekit.go index 4662d00..3d86b5d 100644 --- a/homekit.go +++ b/pkg/homekit/homekit.go @@ -1,31 +1,16 @@ -package main +package homekit import ( "github.com/brutella/hc" "github.com/brutella/hc/accessory" "github.com/lucasb-eyer/go-colorful" - "github.com/spf13/cobra" "k8s.io/klog" -) - -var homekitPin string - -func init() { - rootCmd.AddCommand(homekitCmd) - homekitCmd.Flags().StringVar(&homekitPin, "homekit-pin", "29847290", "The pin that homekit will use to authenticate with this device.") -} - -var homekitCmd = &cobra.Command{ - Use: "homekit", - Short: "Run the lights as a homekit accessory.", - Long: `Run the lights as a homekit accessory.`, - Run: func(cmd *cobra.Command, args []string) { - startHomekit() - }, -} + "github.com/sudermanjr/led-controller/pkg/neopixel" +) -func startHomekit() { +//Start starts the homekit server +func Start(homekitPin string, led *neopixel.LEDArray) { // create an accessory info := accessory.Info{ Name: "LED", @@ -41,22 +26,16 @@ func startHomekit() { klog.Fatal(err) } - led, err := newledArray() - if err != nil { - klog.Fatal(err) - } - defer led.ws.Fini() - ac.Lightbulb.On.OnValueRemoteUpdate(func(on bool) { if on { klog.Infof("Switch is on") - err = led.fade(maxBrightness) + err = led.Fade(led.MaxBrightness) if err != nil { klog.Error(err) } } else { klog.Infof("Switch is off") - err = led.fade(minBrightness) + err = led.Fade(led.MinBrightness) if err != nil { klog.Error(err) } @@ -65,8 +44,8 @@ func startHomekit() { ac.Lightbulb.Hue.OnValueRemoteUpdate(func(value float64) { klog.Infof("homekit hue set to: %f", value) - led.color = modifyHue(led.color, value) - err = led.display(0) + led.Color = modifyHue(led.Color, value) + err = led.Display(0) if err != nil { klog.Error(err) } @@ -74,8 +53,8 @@ func startHomekit() { ac.Lightbulb.Saturation.OnValueRemoteUpdate(func(value float64) { klog.Infof("homekit saturation set to %f", value) - led.color = modifySaturation(led.color, value) - err = led.display(0) + led.Color = modifySaturation(led.Color, value) + err = led.Display(0) if err != nil { klog.Error(err) } @@ -83,7 +62,7 @@ func startHomekit() { ac.Lightbulb.Brightness.OnValueRemoteUpdate(func(value int) { klog.Infof("homekit brightness set to: %d", value) - err = led.fade(scaleHomekitBrightness(value)) + err = led.Fade(scaleHomekitBrightness(value, led.MinBrightness, led.MaxBrightness)) if err != nil { klog.Error(err) } @@ -91,7 +70,7 @@ func startHomekit() { hc.OnTermination(func() { klog.Info("terminated. turning off lights") - err = led.fade(minBrightness) + err = led.Fade(led.MinBrightness) if err != nil { klog.Error(err) } @@ -99,9 +78,9 @@ func startHomekit() { }) klog.Info("starting homekit server...") - klog.Infof("max-brightness: %d", maxBrightness) - klog.Infof("min-brightness: %d", minBrightness) - klog.Infof("fade-duration %d", fadeDuration) + klog.Infof("max-brightness: %d", led.MaxBrightness) + klog.Infof("min-brightness: %d", led.MinBrightness) + klog.Infof("fade-duration %d", led.FadeDuration) t.Start() } @@ -110,11 +89,11 @@ func startHomekit() { // to the scale of the controller (min - max) // math isn't as easy as it used to be for me: // https://stackoverflow.com/questions/5294955/how-to-scale-down-a-range-of-numbers-with-a-known-min-and-max-value -func scaleHomekitBrightness(value int) int { +func scaleHomekitBrightness(value int, minArray int, maxArray int) int { min := 0 max := 100 - a := minBrightness - b := maxBrightness + a := minArray + b := maxArray new := ((b-a)*(value-min))/(max-min) + a diff --git a/homekit_test.go b/pkg/homekit/homekit_test.go similarity index 77% rename from homekit_test.go rename to pkg/homekit/homekit_test.go index 2b4e782..96f0b29 100644 --- a/homekit_test.go +++ b/pkg/homekit/homekit_test.go @@ -1,4 +1,4 @@ -package main +package homekit import ( "testing" @@ -7,6 +7,8 @@ import ( ) func Test_scaleHomekitBrightness(t *testing.T) { + minBrightness := 30 + maxBrightness := 200 tests := []struct { name string value int @@ -19,7 +21,7 @@ func Test_scaleHomekitBrightness(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := scaleHomekitBrightness(tt.value) + got := scaleHomekitBrightness(tt.value, minBrightness, maxBrightness) assert.Equal(t, tt.want, got) }) } diff --git a/neopixel.go b/pkg/neopixel/neopixel.go similarity index 52% rename from neopixel.go rename to pkg/neopixel/neopixel.go index a09f6d8..6361ead 100644 --- a/neopixel.go +++ b/pkg/neopixel/neopixel.go @@ -1,4 +1,4 @@ -package main +package neopixel import ( "time" @@ -6,19 +6,9 @@ import ( "github.com/lucasb-eyer/go-colorful" ws2811 "github.com/rpi-ws281x/rpi-ws281x-go" "k8s.io/klog" -) -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. -} + "github.com/sudermanjr/led-controller/pkg/color" +) type wsEngine interface { Init() error @@ -29,14 +19,18 @@ type wsEngine interface { SetBrightness(channel int, brightness int) } -// ledArray is a struct for interacting with LEDs -type ledArray struct { - ws wsEngine - brightness int - color colorful.Color +// LEDArray is a struct for interacting with LEDs +type LEDArray struct { + WS wsEngine + MaxBrightness int + MinBrightness int + Brightness int + Color colorful.Color + FadeDuration int } -func newledArray() (*ledArray, error) { +// NewLEDArray creates a new array and initializes it +func NewLEDArray(minBrightness int, maxBrightness, ledCount int, fadeDuration int) (*LEDArray, error) { // Setup the LED lights opt := ws2811.DefaultOptions opt.Channels[0].Brightness = maxBrightness @@ -47,8 +41,13 @@ func newledArray() (*ledArray, error) { return nil, err } - led := &ledArray{ - ws: dev, + led := &LEDArray{ + WS: dev, + Brightness: minBrightness, + MinBrightness: minBrightness, + MaxBrightness: maxBrightness, + FadeDuration: fadeDuration, + Color: color.HexToColor(color.ColorMap["white"]), } err = dev.Init() @@ -57,26 +56,23 @@ func newledArray() (*ledArray, error) { klog.Error(err) return nil, err } - // Start with brightness off and color white - led.brightness = minBrightness - led.color = HexToColor(colors["white"]) return led, nil } -// display changes all of the LEDs one at a time +// 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(delay int) error { - klog.V(6).Infof("setting led array to color: %v, delay: %d, brightness: %d", led.color, delay, led.brightness) +func (led *LEDArray) Display(delay int) error { + klog.V(6).Infof("setting led array to color: %v, delay: %d, brightness: %d", led.Color, delay, led.Brightness) err := led.setBrightness() if err != nil { return err } - for i := 0; i < len(led.ws.Leds(0)); i++ { - led.ws.Leds(0)[i] = ColorToUint32(led.color) + for i := 0; i < len(led.WS.Leds(0)); i++ { + led.WS.Leds(0)[i] = color.ToUint32(led.Color) klog.V(10).Infof("setting led %d", i) - if err := led.ws.Render(); err != nil { + if err := led.WS.Render(); err != nil { klog.Error(err) return err } @@ -88,11 +84,29 @@ func (led *ledArray) display(delay 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() error { +func (led *LEDArray) setBrightness() error { led.checkBrightness() - klog.V(8).Infof("setting brightness to %d", led.brightness) - led.ws.SetBrightness(0, led.brightness) - err := led.ws.Render() + klog.V(8).Infof("setting brightness to %d", led.Brightness) + led.WS.SetBrightness(0, led.Brightness) + err := led.WS.Render() + if err != nil { + return err + } + return nil +} + +// SetMaxBrightness fades the LED array to maximum brightness +func (led *LEDArray) SetMaxBrightness() error { + err := led.Fade(led.MaxBrightness) + if err != nil { + return err + } + return nil +} + +// SetMinBrightness fades the LED array to the minimum brightness +func (led *LEDArray) SetMinBrightness() error { + err := led.Fade(led.MinBrightness) if err != nil { return err } @@ -101,36 +115,36 @@ func (led *ledArray) setBrightness() error { // checkBrightness checks to see if the value is // inside the min/max bounds. If it is out, fix it -func (led *ledArray) checkBrightness() { +func (led *LEDArray) checkBrightness() { // Check the bounds - klog.V(10).Infof("comparing value %d to min: %d, max: %d", led.brightness, minBrightness, maxBrightness) - if led.brightness < minBrightness { - klog.V(8).Infof("brightness %d below bounds, setting to %d", led.brightness, minBrightness) - led.brightness = minBrightness + klog.V(10).Infof("comparing value %d to min: %d, max: %d", led.Brightness, led.MinBrightness, led.MaxBrightness) + if led.Brightness < led.MinBrightness { + klog.V(8).Infof("brightness %d below bounds, setting to %d", led.Brightness, led.MinBrightness) + led.Brightness = led.MinBrightness return } - if led.brightness > maxBrightness { - klog.V(8).Infof("brightness %d above bounds, setting to %d", led.brightness, maxBrightness) - led.brightness = maxBrightness + if led.Brightness > led.MaxBrightness { + klog.V(8).Infof("brightness %d above bounds, setting to %d", led.Brightness, led.MaxBrightness) + led.Brightness = led.MaxBrightness return } - klog.V(8).Infof("not out of bounds. leaving it set as %d", led.brightness) + klog.V(8).Infof("not out of bounds. leaving it set as %d", led.Brightness) } -// fade goes to a new brightness in the duration specified -func (led *ledArray) fade(target int) error { +// Fade goes to a new brightness in the duration specified +func (led *LEDArray) Fade(target int) error { klog.V(8).Infof("fading brightness to %d", target) - klog.V(8).Infof("setting color to %v", led.color) - ramp := stepRamp(float64(led.brightness), float64(target), float64(fadeDuration)) + klog.V(8).Infof("setting color to %v", led.Color) + ramp := stepRamp(float64(led.Brightness), float64(target), float64(led.FadeDuration)) //Set the color on all the LEDs - for i := 0; i < len(led.ws.Leds(0)); i++ { - led.ws.Leds(0)[i] = ColorToUint32(led.color) + for i := 0; i < len(led.WS.Leds(0)); i++ { + led.WS.Leds(0)[i] = color.ToUint32(led.Color) } for _, step := range ramp { klog.V(10).Infof("processing step: %d", step) - led.brightness = step + led.Brightness = step err := led.setBrightness() if err != nil { return err diff --git a/neopixel_test.go b/pkg/neopixel/neopixel_test.go similarity index 90% rename from neopixel_test.go rename to pkg/neopixel/neopixel_test.go index 667616f..698964a 100644 --- a/neopixel_test.go +++ b/pkg/neopixel/neopixel_test.go @@ -1,4 +1,4 @@ -package main +package neopixel import ( "testing" @@ -17,11 +17,14 @@ func Test_brightnessBounds(t *testing.T) { {name: "low", value: 10, want: 30}, } for _, tt := range tests { - led := &ledArray{} + led := &LEDArray{ + MinBrightness: 30, + MaxBrightness: 200, + } t.Run(tt.name, func(t *testing.T) { - led.brightness = tt.value + led.Brightness = tt.value led.checkBrightness() - assert.Equal(t, tt.want, led.brightness) + assert.Equal(t, tt.want, led.Brightness) }) } } diff --git a/main_test.go b/pkg/utils/utils.go similarity index 63% rename from main_test.go rename to pkg/utils/utils.go index 80dd42e..8f819d1 100644 --- a/main_test.go +++ b/pkg/utils/utils.go @@ -1,31 +1,18 @@ -package main +package utils import ( "bytes" "io" "log" "os" - "testing" "k8s.io/klog" ) -// workaround for some weird go 1.13 testing thing with flags -// https://stackoverflow.com/questions/29699982/go-test-flag-flag-provided-but-not-defined -// https://github.com/golang/go/issues/31859 -var _ = func() bool { - testing.Init() - return true -}() - -func init() { - minBrightness = 30 - maxBrightness = 200 -} - const chunkSize = 64000 -func deepCompare(file1, file2 string) bool { +//DeepCompareFiles compares two files +func DeepCompareFiles(file1, file2 string) bool { // Check file size ... f1, err := os.Open(file1)