From de5b178018812ff8d364c3fc5150ea4ff195d544 Mon Sep 17 00:00:00 2001 From: Tyler Thompson Date: Wed, 16 Feb 2022 23:22:59 +0000 Subject: [PATCH] Added lighting options + before/after funcs --- cli.go | 24 ++++++++++++++++-- glorious.go | 19 +++++---------- main.go | 70 ++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 86 insertions(+), 27 deletions(-) diff --git a/cli.go b/cli.go index ce862ed..b9f14e0 100644 --- a/cli.go +++ b/cli.go @@ -1,5 +1,25 @@ package main -func cliInit() { - // +import ( + "fmt" + + "github.com/urfave/cli/v2" +) + +func runBefore(c *cli.Context) error { + if c.Args().Len() == 0 { + return fmt.Errorf("no arguments passed") + } + return nil +} + +func (device *Device) runAfter(c *cli.Context) error { + if c.Err() == nil { + err := device.SetConfig() + if err != nil { + return fmt.Errorf("An error occured while writing the config: %v\n", err) + } + fmt.Println("Successfully updated configuration") + } + return nil } diff --git a/glorious.go b/glorious.go index 5df884b..3088b6b 100644 --- a/glorious.go +++ b/glorious.go @@ -148,6 +148,8 @@ func NameToRGBEffect(n string) (RGBEffect, bool) { switch n { case "Off": return RGB_OFF, true + case "Glorious Mode": + return RGB_GLORIOUS, true case "Single Color": return RGB_SINGLE, true case "RGB Breathing": @@ -252,7 +254,7 @@ func (dev *Device) GetFirmwareVersion() string { log.Fatalln("res:", res, "in read firmware version, go err:", err, "hid err:", dev.hid.Error()) } - return fmt.Sprintf("%s", version) + return fmt.Sprintf("%c", version) } func (dev *Device) GetDebounceTime() int { @@ -355,33 +357,24 @@ func (c *GloriousConfig) SetRGBBrightness(brightness int) error { return fmt.Errorf("brightness can not be set on RGB_GLORIOUS") } - oldBrightness, speed, err := c.GetRGBMode() + _, speed, err := c.GetRGBMode() if err != nil { return err } - fmt.Println("Old Brightness:", oldBrightness) - - fmt.Println("New Brightness:", brightness) - return c.SetRGBMode(brightness, speed) } func (c *GloriousConfig) SetRGBSpeed(speed int) error { if speed < 0 || speed > 3 { - return fmt.Errorf("brightness level too high or low") + return fmt.Errorf("speed level too high or low") } - brightness, oldSpeed, err := c.GetRGBMode() + brightness, _, err := c.GetRGBMode() if err != nil { return err } - - fmt.Println("Old Speed:", oldSpeed) - - fmt.Println("New Speed:", speed) - return c.SetRGBMode(brightness, speed) } diff --git a/main.go b/main.go index c1b537b..99cbf0b 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "strconv" + "strings" "github.com/sstallion/go-hid" "github.com/urfave/cli/v2" @@ -46,13 +47,12 @@ func main() { Name: "set", Aliases: []string{"s"}, Usage: "set config option", - Action: func(c *cli.Context) error { - return fmt.Errorf("Please specify what to set") - }, Subcommands: []*cli.Command{ { - Name: "dpi", - Usage: "Set dpi", + Name: "dpi", + Usage: "Set dpi", + Before: runBefore, + After: device.runAfter, Action: func(c *cli.Context) error { dpiInt, err := strconv.Atoi(c.Args().First()) if err != nil { @@ -72,6 +72,8 @@ func main() { Name: "debounce", Usage: "Set debounce time", Aliases: []string{"db", "dbt"}, + Before: runBefore, + After: device.runAfter, Action: func(c *cli.Context) error { dbtInt, err := strconv.Atoi(c.Args().First()) if err != nil { @@ -84,20 +86,64 @@ func main() { return nil }, }, + { + Name: "lighting", + Usage: "Lighting configuration", + Aliases: []string{"l"}, + Subcommands: []*cli.Command{ + { + Name: "effect", + Aliases: []string{"e"}, + Before: runBefore, + After: device.runAfter, + Action: func(c *cli.Context) error { + lightingName := strings.Join(c.Args().Slice(), " ") + lightingMode, ok := NameToRGBEffect(lightingName) + if !ok { + return fmt.Errorf("invalid lighting mode") + } + device.conf.SetRGBEffect(lightingMode) + return nil + }, + }, + { + Name: "brightness", + Aliases: []string{"b"}, + Before: runBefore, + After: device.runAfter, + Action: func(c *cli.Context) error { + brightness, err := strconv.Atoi(c.Args().First()) + if err != nil { + return err + } + return device.conf.SetRGBBrightness(brightness) + }, + }, + { + Name: "speed", + Aliases: []string{"s"}, + Before: runBefore, + After: device.runAfter, + Action: func(c *cli.Context) error { + speed, err := strconv.Atoi(c.Args().First()) + if err != nil { + return err + } + return device.conf.SetRGBSpeed(speed) + }, + }, + }, + }, }, }, }, - Version: "v1.0.0", + Version: "v1.1.0", } app.EnableBashCompletion = true - app.Run(os.Args) - - err = device.SetConfig() + err = app.Run(os.Args) if err != nil { - fmt.Printf("An error occured while writing the config: %v\n", err) + fmt.Println(err) os.Exit(1) } - - fmt.Println("Successfully updated configuration") }