From a3786c8698f8b7c85d09e241cf09dc0e8ced9da2 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Thu, 24 May 2018 22:23:35 -0400 Subject: Split out updateLight function --- colortemp.go | 2 +- main.go | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/colortemp.go b/colortemp.go index a708a0b..8cf3454 100644 --- a/colortemp.go +++ b/colortemp.go @@ -43,7 +43,7 @@ func getDesiredColorTemperature(t time.Time) ColorTemperature { // 500 <=> 2000K // ============= // 347 4500 -func translateCtForLight(ct ColorTemperature, light hue.Light) uint16 { +func (ct ColorTemperature) TranslateForLight(light hue.Light) uint16 { divisor := 12.968 scaled := float64(ct) / divisor inverted := 500 - scaled + 153 diff --git a/main.go b/main.go index 481f712..76f5838 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ func main() { } } -func updateBridge(bridge hue.Bridge, desiredColorTemp ColorTemperature) { +func updateBridge(bridge hue.Bridge, ct ColorTemperature) { //username, err := bridge.CreateUser(username) //if err != nil { // panic("Could not create user on bridge") @@ -40,15 +40,19 @@ func updateBridge(bridge hue.Bridge, desiredColorTemp ColorTemperature) { } log.Printf("Found %d lights\n", len(lights)) for _, light := range lights { - log.Printf("Light %d: %s (%s)\n", light.Index, light.Name, light.Type) - if supportsColorTemp(light) { - log.Printf(" CT range: %d-%d\n", light.Capabilities.Control.CT.Min, light.Capabilities.Control.CT.Max) - newCt := translateCtForLight(desiredColorTemp, light) - log.Printf(" Setting CT to %d\n", newCt) - light.SetState(hue.LightState{ - On: light.State.On, - CT: newCt, - }) - } + updateLight(light, ct) + } +} + +func updateLight(light hue.Light, ct ColorTemperature) { + log.Printf("Light %d: %s (%s)\n", light.Index, light.Name, light.Type) + if supportsColorTemp(light) { + log.Printf(" CT range: %d-%d\n", light.Capabilities.Control.CT.Min, light.Capabilities.Control.CT.Max) + newCt := ct.TranslateForLight(light) + log.Printf(" Setting CT to %d\n", newCt) + light.SetState(hue.LightState{ + On: light.State.On, + CT: newCt, + }) } } -- cgit v1.2.3