Skip to content

Commit

Permalink
Added lighting options + before/after funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
damaredayo committed Feb 16, 2022
1 parent b500554 commit de5b178
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 27 deletions.
24 changes: 22 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 6 additions & 13 deletions glorious.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

}
70 changes: 58 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strconv"
"strings"

"github.com/sstallion/go-hid"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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")
}

0 comments on commit de5b178

Please sign in to comment.