From 1ed1083ed0408bf1e7eae5516c36038c75154b3f Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Wed, 24 Feb 2016 19:33:35 -0500 Subject: Implemented Light.SetBrightness as a shortcut to using a SetState struct. --- light.go | 17 +++++++++++++++-- light_test.go | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/light.go b/light.go index f1aadb4..cc4b502 100644 --- a/light.go +++ b/light.go @@ -173,7 +173,7 @@ func (light *Light) SetColor(color *[2]float32) error { return nil } -// Light.Dim will lower the brightness by a percent. +// Light.Dim lowers 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 { @@ -187,7 +187,20 @@ func (light *Light) Dim(percent int) error { } return nil } else { - return errors.New("Light.Dim percentage given is not between 1 and 100") + return errors.New("Light.Dim percentage given is not between 1 and 100. ") + } +} + +// Light.SetBrightness sets the brightness to a percentage of the maximum +// maximum brightness as an integer (`LightStruct.Bri between 1 and 254 inclusive`) +func (light *Light) SetBrightness(percent int) error { + if percent > 0 && percent <= 100 { + brightness := uint8(float32(percent)*2.54) // 100=254x --> 2.54 + lightState := LightState{On: true, Bri: brightness} + light.SetState(lightState) + return nil + } else { + return errors.New("Light.SetBrightness percentage is not between 1 and 100. ") } } diff --git a/light_test.go b/light_test.go index 0d486cd..ecc5460 100644 --- a/light_test.go +++ b/light_test.go @@ -21,6 +21,7 @@ func TestSetLightState(t *testing.T) { selectedLight, _ := bridge.GetLightByIndex(7) selectedLight.On() + selectedLight.SetBrightness(100) time.Sleep(time.Second) selectedLight.Off() time.Sleep(time.Second) -- cgit v1.2.3