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 +++++++++++++++++++ light_test.go | 4 +++- 2 files changed, 22 insertions(+), 1 deletion(-) 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) diff --git a/light_test.go b/light_test.go index 436d652..086970f 100644 --- a/light_test.go +++ b/light_test.go @@ -10,7 +10,7 @@ package hue import ( "testing" //"fmt" - "time" + //"time" ) func TestSetLightState(t *testing.T) { @@ -33,6 +33,8 @@ func TestSetLightState(t *testing.T) { selectedLight.Blink(3) + selectedLight.Dim(20) + // selectedLight.SetColor(RED) // time.Sleep(time.Second) // selectedLight.SetColor(YELLOW) -- cgit v1.2.3