diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-02-24 19:51:00 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-02-24 19:51:00 -0500 |
commit | 5fe626948a562eaa48517ef46ff6134916243abb (patch) | |
tree | bd06ba06b5a95caac476517d91a6d7037a0762ce | |
parent | 8ec7808b6aab7ee830b4f9eb49b82da3335245d3 (diff) |
Implemented Light.Brighten to complement Light.Dim
-rw-r--r-- | light.go | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -12,6 +12,7 @@ import ( "fmt" "time" "errors" + "log" ) // Light struct defines attributes of a light. @@ -204,6 +205,28 @@ func (light *Light) SetBrightness(percent int) error { } } + +// Light.Brighten will increase LightStruct.Bri by a given percent (integer) +func (light *Light) Brighten(percent int) error { + if percent > 0 && percent <= 100 { + originalBri := light.State.Bri + increaseBri := float32(originalBri)*float32((float32(percent)/100.0)) + newBri := uint8(originalBri+int(increaseBri)) + if newBri > 254 { // LightState.Bri must be between 1 and 254 inclusive + newBri = 254 + log.Println("Light.Brighten state set over 100%, setting brightness to 100%. ") + } + lightState := LightState{On: true, Bri: newBri} + err := light.SetState(lightState) + if err != nil { + return err + } + return nil + } else { + return errors.New("Light.Brighten percentage 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) |