aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-02-24 19:33:35 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-02-24 19:33:35 -0500
commit1ed1083ed0408bf1e7eae5516c36038c75154b3f (patch)
tree5cc507f19f64301146bc6bf5b06b46f12bf5e351
parentdbf10ecf3bb19d4b335767e0b9dad0f9e94198e6 (diff)
Implemented Light.SetBrightness as a shortcut to using a SetState struct.
-rw-r--r--light.go17
-rw-r--r--light_test.go1
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)