aboutsummaryrefslogtreecommitdiff
path: root/light.go
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-02-14 00:00:42 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-02-14 00:00:42 -0500
commit067d3da3ad723532aaa0c6a009c35ed9cf132deb (patch)
tree9c5d1908d8e011d61cb20e8c652c4cdcc2afc4c5 /light.go
parentadab2507bfef36bb0ea1150453829e41fbbc89e2 (diff)
Improved Light.Blink by dimming and raising the brightness instead of turning on and off.
Diffstat (limited to 'light.go')
-rw-r--r--light.go37
1 files changed, 22 insertions, 15 deletions
diff --git a/light.go b/light.go
index efdf188..54762a6 100644
--- a/light.go
+++ b/light.go
@@ -103,27 +103,35 @@ func (self *Light) Delete() error {
return nil
}
-// Light.Blink will turn the light off and on repeatedly for a given seconds
-// interval and return the light back to its off or on state afterwards.
+// Light.Blink will increase and decrease the brightness
+// repeatedly for a given seconds interval and return the
+// light back to its off or on state afterwards.
// Note: time will vary based on connection speed and algorithm speed.
func (self *Light) Blink(seconds int) error {
- originalState := self.State.On
+ originalPosition := self.State.On
+ originalBrightness := self.State.Bri
+ blinkMax := LightState{On: true, Bri: uint8(200)}
+ blinkMin := LightState{On: true, Bri: uint8(50)}
// Toggle the light on and off
- for i := 0; i <= seconds; i++ {
- err := self.Toggle()
- if err != nil {
- return err
+ for i := 0; i <= seconds*2; i++ {
+ if i % 2 == 0 {
+ err = self.SetState(blinkMax)
+ if err != nil {
+ return err
+ }
+ } else {
+ err = self.SetState(blinkMin)
+ if err != nil {
+ return err
+ }
}
- time.Sleep(time.Second)
+ time.Sleep(time.Second/2)
}
- // Return the light to its original on or off state
- if self.State.On != originalState {
- err := self.Toggle()
- if err != nil {
- return err
- }
+ // Return the light to its original on or off state and brightness
+ if self.State.Bri != originalBrightness || self.State.On != originalPosition {
+ self.SetState(LightState{On: originalPosition, Bri: uint8(originalBrightness)})
}
return nil
}
@@ -174,7 +182,6 @@ func GetAllLights(bridge *Bridge) ([]Light, error) {
// a light given its index stored on the bridge. This is used for
// quickly updating an individual light.
func GetLightByIndex(bridge *Bridge, index int) (Light, error) {
-
// Send an http GET and inspect the response
uri := fmt.Sprintf("/api/%s/lights/%d", bridge.Username, index)
body, _, err := bridge.Get(uri)