diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-02-24 19:53:56 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-02-24 19:53:56 -0500 |
commit | 203389517a8c07771aa69c89b28a8c62b04f6b49 (patch) | |
tree | 353546fe8f9df509214b64167b38e3aa82edac57 | |
parent | 5fe626948a562eaa48517ef46ff6134916243abb (diff) |
Added error checking to Light.Dim and Light.SetBrightness
-rw-r--r-- | light.go | 13 |
1 files changed, 10 insertions, 3 deletions
@@ -180,8 +180,12 @@ 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)} + newBri := uint8(originalBri-int(decreaseBri)) + if newBri < 0 { + newBri = 0 + log.Println("Light.Dim state set under 0%, setting brightness to 0. ") + } + lightState := LightState{On: true, Bri: newBri} err := light.SetState(lightState) if err != nil { return err @@ -198,7 +202,10 @@ 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) + err := light.SetState(lightState) + if err != nil { + return err + } return nil } else { return errors.New("Light.SetBrightness percentage is not between 1 and 100. ") |