From b279470a75dd6f3e59809650424a8e21d979eefc Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Thu, 24 May 2018 22:17:58 -0400 Subject: Select proper color temperature depending on time --- colortemp.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'colortemp.go') diff --git a/colortemp.go b/colortemp.go index 051ca54..a708a0b 100644 --- a/colortemp.go +++ b/colortemp.go @@ -1,16 +1,36 @@ package main import ( + "log" "math" "time" hue "github.com/benburwell/gohue" + "github.com/cpucycle/astrotime" ) type ColorTemperature uint -func getDesiredColorTemperature(at time.Time) ColorTemperature { - return 1800 +const LATITUDE = 42.348333 +const LONGITUDE = 71.1675 + +// Get the correct color temperature for a given time. If the time is between +// sunrise and sunset, a high CT is selected. Otherise, during night, a low +// color temperature is used. +// +// TODO: This is a naive approach and should be revisited +func getDesiredColorTemperature(t time.Time) ColorTemperature { + log.Printf("Calculating sunrise/sunset for: %s\n", t.Format(time.RFC3339)) + sunrise := astrotime.CalcSunrise(t, LATITUDE, LONGITUDE) + sunset := astrotime.CalcSunset(t, LATITUDE, LONGITUDE) + log.Printf("Sunrise: %s, Sunset: %s\n", sunrise.Format(time.RFC3339), sunset.Format(time.RFC3339)) + if t.After(sunrise) && t.Before(sunset) { + log.Println("Daytime, setting high CT") + return 6500 + } else { + log.Println("Nighttime, setting low CT") + return 1800 + } } // Translate a desired color temperature in Kelvins to a value comprehensible -- cgit v1.2.3