Skip to content

Commit

Permalink
Add temp color temperature command
Browse files Browse the repository at this point in the history
  • Loading branch information
tessro committed May 4, 2021
1 parent 9f3255f commit 6bcab99
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ func (c Client) SelectEffect(name string) error {
return nil
}

// SetColorTemperature sets the Nanoleaf's color temperature.
func (c Client) SetColorTemperature(temperature int) error {
state := State{
ColorTemperature: &ColorTemperatureProperty{temperature},
}

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

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

// SetHSL sets the Nanoleaf's hue, saturation, and lightness (brightness).
func (c Client) SetHSL(hue int, sat int, lightness int) error {
state := State{
Expand Down
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func usage() {
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()
os.Exit(1)
}
Expand Down Expand Up @@ -94,6 +95,8 @@ func main() {
doHSLCommand(client, flag.Args()[1:])
case "rgb":
doRGBCommand(client, flag.Args()[1:])
case "temp":
doColorTemperatureCommand(client, flag.Args()[1:])
case "effect":
doEffectCommand(client, flag.Args()[1:])
default:
Expand All @@ -104,6 +107,25 @@ func main() {
}
}

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

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

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

func doEffectCommand(client Client, args []string) {
usage := func() {
fmt.Println("usage: picoleaf effect list")
Expand Down

0 comments on commit 6bcab99

Please sign in to comment.