aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2018-05-24 21:24:24 -0400
committerBen Burwell <ben@benburwell.com>2018-05-24 21:24:24 -0400
commit307a3802dc7c9637b9b3a1a95ff47c0686fa1a01 (patch)
tree0e4b9d6bc72054c883cb88f4b7ce926e7c42deca
parentc7e47ab5323f469d43f74397c892fc0c046a9b53 (diff)
Move all CT functionality into colortemp.go
-rw-r--r--colortemp.go27
-rw-r--r--main.go82
2 files changed, 57 insertions, 52 deletions
diff --git a/colortemp.go b/colortemp.go
index d621043..051ca54 100644
--- a/colortemp.go
+++ b/colortemp.go
@@ -1,7 +1,10 @@
package main
import (
+ "math"
"time"
+
+ hue "github.com/benburwell/gohue"
)
type ColorTemperature uint
@@ -9,3 +12,27 @@ type ColorTemperature uint
func getDesiredColorTemperature(at time.Time) ColorTemperature {
return 1800
}
+
+// Translate a desired color temperature in Kelvins to a value comprehensible
+// by a Hue luminaire. According to Hue documentation, the value 153
+// corresponds to 6500K, and 500 corresponds to 2000K. Using these known
+// values, we'll create a mapping between spaces, and additionally limit the
+// resulting value by the range that the luminaire supports.
+//
+// 153 <=> 6500K
+// 500 <=> 2000K
+// =============
+// 347 4500
+func translateCtForLight(ct ColorTemperature, light hue.Light) uint16 {
+ divisor := 12.968
+ scaled := float64(ct) / divisor
+ inverted := 500 - scaled + 153
+ min := float64(light.Capabilities.Control.CT.Min)
+ max := float64(light.Capabilities.Control.CT.Max)
+ return uint16(math.Max(min, math.Min(max, inverted)))
+}
+
+// Determine whether there is a non-zero color temperature range for the given light
+func supportsColorTemp(l hue.Light) bool {
+ return l.Capabilities.Control.CT.Max > l.Capabilities.Control.CT.Min
+}
diff --git a/main.go b/main.go
index 7636ad2..481f712 100644
--- a/main.go
+++ b/main.go
@@ -2,7 +2,6 @@ package main
import (
"log"
- "math"
"os"
"time"
@@ -17,60 +16,39 @@ func main() {
log.Fatalf("Error finding bridges: %s\n", err.Error())
}
log.Printf("Found %d bridge(s)\n", len(bridges))
+ desiredColorTemp := getDesiredColorTemperature(time.Now())
for _, bridge := range bridges {
log.Printf("Bridge: %s\n", bridge.IPAddress)
- //username, err := bridge.CreateUser(username)
- //if err != nil {
- // panic("Could not create user on bridge")
- //}
- //fmt.Printf("Made user %s for bridge %s\n", username, bridge.IPAddress)
- err = bridge.Login(os.Getenv("HUE_LOGIN"))
- if err != nil {
- log.Fatalf("Could not log in to bridge: %s", err.Error())
- }
- log.Println("Logged in to bridge")
- lights, err := bridge.GetAllLights()
- if err != nil {
- log.Fatalf("Error getting lights: %s\n", err.Error())
- }
- 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)
- if light.Index == 8 {
- newCt := translateCtForLight(getDesiredColorTemperature(time.Now()), light)
- log.Printf(" Setting CT to %d\n", newCt)
- light.SetState(hue.LightState{
- On: light.State.On,
- CT: newCt,
- })
- }
- }
- }
+ updateBridge(bridge, desiredColorTemp)
}
}
-// Determine whether there is a non-zero color temperature range for the given light
-func supportsColorTemp(l hue.Light) bool {
- return l.Capabilities.Control.CT.Max > l.Capabilities.Control.CT.Min
-}
-
-// Translate a desired color temperature in Kelvins to a value comprehensible
-// by a Hue luminaire. According to Hue documentation, the value 153
-// corresponds to 6500K, and 500 corresponds to 2000K. Using these known
-// values, we'll create a mapping between spaces, and additionally limit the
-// resulting value by the range that the luminaire supports.
-//
-// 153 <=> 6500K
-// 500 <=> 2000K
-// =============
-// 347 4500
-func translateCtForLight(ct ColorTemperature, light hue.Light) uint16 {
- divisor := 12.968
- scaled := float64(ct) / divisor
- inverted := 500 - scaled + 153
- min := float64(light.Capabilities.Control.CT.Min)
- max := float64(light.Capabilities.Control.CT.Max)
- return uint16(math.Max(min, math.Min(max, inverted)))
+func updateBridge(bridge hue.Bridge, desiredColorTemp ColorTemperature) {
+ //username, err := bridge.CreateUser(username)
+ //if err != nil {
+ // panic("Could not create user on bridge")
+ //}
+ //fmt.Printf("Made user %s for bridge %s\n", username, bridge.IPAddress)
+ err := bridge.Login(os.Getenv("HUE_LOGIN"))
+ if err != nil {
+ log.Fatalf("Could not log in to bridge: %s", err.Error())
+ }
+ log.Println("Logged in to bridge")
+ lights, err := bridge.GetAllLights()
+ if err != nil {
+ log.Fatalf("Error getting lights: %s\n", err.Error())
+ }
+ 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,
+ })
+ }
+ }
}