From b356e22256b6956a29fa10f7310d8789d79e9c04 Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Fri, 19 Feb 2016 20:46:05 -0500 Subject: Implemented light.Dim func. --- light.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'light.go') diff --git a/light.go b/light.go index 982938c..817c941 100644 --- a/light.go +++ b/light.go @@ -11,6 +11,7 @@ package hue import ( "fmt" "time" + "errors" ) // Light struct defines attributes of a light. @@ -172,6 +173,24 @@ func (light *Light) SetColor(color *[2]float32) error { return nil } +// Light.Dim will lower the brightness by a percent. +// Note the required value is an integer, for example "20" is converted to 20%. +func (light *Light) Dim(percent int) error { + if percent > 0 && percent <= 100 { + originalBri := light.State.Bri + decreaseBri := float32(originalBri)*float32((float32(percent)/100.0)) + newBri := (originalBri-int(decreaseBri)) + lightState := LightState{On: true, Bri: uint8(newBri)} + err := light.SetState(lightState) + if err != nil { + return err + } + return nil + } else { + return errors.New("Light.Dim percentage given is not between 1 and 100") + } +} + // Light.SetState modifyies light attributes. See `LightState` struct for attributes. // Brightness must be between 1 and 254 (inclusive) // Hue must be between 0 and 65535 (inclusive) -- cgit v1.2.3