aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-02-19 20:46:05 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-02-19 20:46:05 -0500
commitb356e22256b6956a29fa10f7310d8789d79e9c04 (patch)
tree1f07a42a425e83bd61cb316707371c00c08313ae
parenta902f8994241fc2484af6241726fd3cfc6d7ddc5 (diff)
Implemented light.Dim func.
-rw-r--r--light.go19
-rw-r--r--light_test.go4
2 files changed, 22 insertions, 1 deletions
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)