Skip to content

Commit

Permalink
Separating code into packages (#2)
Browse files Browse the repository at this point in the history
* Reorganizing into packages. Much nicer organization now.
  • Loading branch information
Andrew Suderman committed Jan 20, 2020
1 parent be7d703 commit 0dfc8de
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 237 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ orbs:
jobs:
test:
docker:
- image: circleci/golang:1.12-stretch
- image: circleci/golang:1.13-stretch
environment:
GO111MODULE: "on"
steps:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repos:
- id: forbid-binary
exclude: >
(?x)^(
testdata/.+\.png
pkg/color/testdata/.+\.png
)$
- id: shellcheck
- id: git-check
Expand Down
25 changes: 17 additions & 8 deletions control.go → cmd/control.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
},
}

Expand All @@ -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)
},
}
56 changes: 30 additions & 26 deletions demo.go → cmd/demo.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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{
Expand All @@ -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)
},
}
33 changes: 33 additions & 0 deletions cmd/homekit.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
87 changes: 87 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
89 changes: 7 additions & 82 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 0dfc8de

Please sign in to comment.