From 6363f53257d193360cea92a6991ad3097b280e30 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Fri, 28 Sep 2018 16:42:18 -0600 Subject: [PATCH] Commit current stuff, though it doesn't work --- config.go | 4 ++-- config_test.go | 4 ++-- run.go | 56 ++++++++++++++++++++++++++++--------------------- test_config.yml | 4 ++-- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/config.go b/config.go index 6264dc0..1bfb3b9 100644 --- a/config.go +++ b/config.go @@ -13,10 +13,10 @@ type Sensor struct { HighTemp float64 `yaml:"hightemp"` LowTemp float64 `yaml:"lowtemp"` HeatGPIO int32 `yaml:"heatgpio"` - HeatPullup bool `yaml:"heatpullup"` + HeatInvert bool `yaml:"heatinvert"` HeatMinutes float64 `yaml:"heatminutes"` CoolGPIO int32 `yaml:"coolgpio"` - CoolPullup bool `yaml:"coolpullup"` + CoolInvert bool `yaml:"coolinvert"` CoolMinutes float64 `yaml:"coolminutes"` } diff --git a/config_test.go b/config_test.go index 06ca013..0a516c4 100644 --- a/config_test.go +++ b/config_test.go @@ -13,10 +13,10 @@ func Test_LoadConfig(t *testing.T) { HighTemp: 8, LowTemp: 4, HeatGPIO: 5, - HeatPullup: true, + HeatInvert: true, HeatMinutes: 5, CoolGPIO: 17, - CoolPullup: false, + CoolInvert: false, CoolMinutes: 10} testConfig := Config{Sensors: []Sensor{testSensor}} diff --git a/run.go b/run.go index 2f31436..63d42a4 100644 --- a/run.go +++ b/run.go @@ -35,28 +35,28 @@ func ReadTemperature(id string) (float64, error) { } // SetPinState is used to turn a pin on or off. -// If pullup is true, the pin will be pulled up when turned on, and pulled down when turned off. -func SetPinState(pin rpio.Pin, on bool, pullup bool) { +// If invert is false, the pin will be high turned on, and low when turned off. +func SetPinState(pin rpio.Pin, on bool, invert bool) { switch { - case on && pullup: - pin.PullUp() - case on && !pullup: - pin.PullDown() - case !on && !pullup: - pin.PullUp() + case on && !invert: + pin.High() + case on && invert: + pin.Low() + case !on && invert: + pin.High() default: - pin.PullDown() + pin.Low() } } // GetPinState will return true if the pin is on. -// If pullup is true, the pin will be on when pulled up. -func GetPinState(pin rpio.Pin, pullup bool) bool { +// If invert is false, the pin will be on when high. +func GetPinState(pin rpio.Pin, invert bool) bool { switch pin.Read() { case rpio.High: - return pullup + return !invert default: - return !pullup + return invert } } @@ -82,26 +82,34 @@ func RunThermostat(sensor Sensor) { switch { case t > sensor.HighTemp && t < sensor.HighTemp: log.Panic("Invalid state! Temperature is too high AND too low!") - case t > sensor.HighTemp && GetPinState(hpin, sensor.HeatPullup): - SetPinState(hpin, false, sensor.HeatPullup) + case t > sensor.HighTemp && GetPinState(hpin, sensor.HeatInvert): + SetPinState(hpin, false, sensor.HeatInvert) + log.Printf("%s Turned off heat", sensor.Alias) + s.Changed = time.Now() case t > sensor.HighTemp && min > sensor.CoolMinutes: - SetPinState(cpin, true, sensor.CoolPullup) + SetPinState(cpin, true, sensor.CoolInvert) + log.Printf("%s Turned on cool", sensor.Alias) + s.Changed = time.Now() case t > sensor.HighTemp: break - case t < sensor.LowTemp && GetPinState(cpin, sensor.CoolPullup): - SetPinState(cpin, false, sensor.CoolPullup) + case t < sensor.LowTemp && GetPinState(cpin, sensor.CoolInvert): + SetPinState(cpin, false, sensor.CoolInvert) + log.Printf("%s Turned off cool", sensor.Alias) + s.Changed = time.Now() case t < sensor.LowTemp && min > sensor.HeatMinutes: - SetPinState(hpin, true, sensor.HeatPullup) + SetPinState(hpin, true, sensor.HeatInvert) + log.Printf("%s Turned on heat", sensor.Alias) + s.Changed = time.Now() case t < sensor.LowTemp: break default: // Turn off both switches - SetPinState(cpin, false, sensor.CoolPullup) - SetPinState(hpin, false, sensor.HeatPullup) + SetPinState(cpin, false, sensor.CoolInvert) + SetPinState(hpin, false, sensor.HeatInvert) } s.Temp = t - s.Cooling = GetPinState(cpin, sensor.CoolPullup) - s.Heating = GetPinState(hpin, sensor.HeatPullup) - log.Printf("Temp: %.2f, Cooling: %t, Heating: %t, Changed: %s", s.Temp, s.Cooling, s.Heating, s.Changed) + //s.Cooling = GetPinState(cpin, sensor.CoolInvert) + //s.Heating = GetPinState(hpin, sensor.HeatInvert) + log.Printf("%s Temp: %.2f, Cooling: %t, Heating: %t, Duration: %.1f", sensor.Alias, s.Temp, s.Cooling, s.Heating, min) } } diff --git a/test_config.yml b/test_config.yml index 2335b38..3f7ef07 100644 --- a/test_config.yml +++ b/test_config.yml @@ -4,8 +4,8 @@ sensors: hightemp: 8 lowtemp: 4 heatgpio: 5 - heatpullup: true + heatinvert: true heatminutes: 5 coolgpio: 17 - coolpullup: false + coolinvert: false coolminutes: 10