Skip to content

Commit

Permalink
Add brightness command
Browse files Browse the repository at this point in the history
  • Loading branch information
tessro committed May 4, 2021
1 parent b2b2e29 commit 120663c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ picoleaf off # Turn Nanoleaf off
picoleaf hsl <hue> <saturation> <lightness> # Set Nanoleaf to the provided HSL
picoleaf rgb <red> <green> <blue> # Set Nanoleaf to the provided RGB
picoleaf temp <temperature> # Set Nanoleaf to the provided color temperature
picoleaf brightness <temperature> # Set Nanoleaf to the provided brightness

# Effects
picoleaf effect list # List installed effects
Expand Down
15 changes: 15 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ func (c Client) SelectEffect(name string) error {
return nil
}

// SetBrightness sets the Nanoleaf's brightness.
func (c Client) SetBrightness(brightness int) error {
state := State{
Brightness: &BrightnessProperty{brightness, 0},
}

bytes, err := json.Marshal(state)
if err != nil {
return err
}

c.Put("state", bytes)
return nil
}

// SetColorTemperature sets the Nanoleaf's color temperature.
func (c Client) SetColorTemperature(temperature int) error {
state := State{
Expand Down
34 changes: 28 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ func usage() {
fmt.Println()
fmt.Println("Commands:")
fmt.Println()
fmt.Println(" on Turn on Nanoleaf")
fmt.Println(" off Turn off Nanoleaf")
fmt.Println(" on Turn on Nanoleaf")
fmt.Println(" off Turn off Nanoleaf")
fmt.Println()
fmt.Println(" effect Control Nanoleaf effects")
fmt.Println(" effect Control Nanoleaf effects")
fmt.Println()
fmt.Println(" hsl Set Nanoleaf to the provided HSL")
fmt.Println(" rgb Set Nanoleaf to the provided RGB")
fmt.Println(" temp Set Nanoleaf to the provided color temperature")
fmt.Println(" hsl Set Nanoleaf to the provided HSL")
fmt.Println(" rgb Set Nanoleaf to the provided RGB")
fmt.Println(" temp Set Nanoleaf to the provided color temperature")
fmt.Println(" brightness Set Nanoleaf to the provided brightness")
fmt.Println()
os.Exit(1)
}
Expand Down Expand Up @@ -62,6 +63,8 @@ func main() {
if flag.NArg() > 0 {
cmd := flag.Arg(0)
switch cmd {
case "brightness":
doBrightnessCommand(client, flag.Args()[1:])
case "effect":
doEffectCommand(client, flag.Args()[1:])
case "hsl":
Expand Down Expand Up @@ -90,6 +93,25 @@ func main() {
}
}

func doBrightnessCommand(client Client, args []string) {
if len(args) < 1 {
fmt.Println("usage: picoleaf brightness <brightness>")
os.Exit(1)
}

brightness, err := strconv.Atoi(args[0])
if err != nil || brightness < 0 || brightness > 100 {
fmt.Println("error: temperature must be an integer 0-100")
os.Exit(1)
}

err = client.SetBrightness(brightness)
if err != nil {
fmt.Printf("error: failed to set brightness: %v", err)
os.Exit(1)
}
}

func doColorTemperatureCommand(client Client, args []string) {
if len(args) < 1 {
fmt.Println("usage: picoleaf temp <temperature>")
Expand Down

0 comments on commit 120663c

Please sign in to comment.