diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-02-14 00:00:42 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-02-14 00:00:42 -0500 |
commit | 067d3da3ad723532aaa0c6a009c35ed9cf132deb (patch) | |
tree | 9c5d1908d8e011d61cb20e8c652c4cdcc2afc4c5 | |
parent | adab2507bfef36bb0ea1150453829e41fbbc89e2 (diff) |
Improved Light.Blink by dimming and raising the brightness instead of turning on and off.
-rw-r--r-- | light.go | 37 | ||||
-rw-r--r-- | light_test.go | 2 |
2 files changed, 23 insertions, 16 deletions
@@ -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) diff --git a/light_test.go b/light_test.go index c03b412..bdf589f 100644 --- a/light_test.go +++ b/light_test.go @@ -31,7 +31,7 @@ func TestSetLightState(t *testing.T) { selectedLight.SetName(selectedLight.Name) - selectedLight.Blink(2) + selectedLight.Blink(3) // _ := selectedLight.Delete() } |