gpio.go 981 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package core
  2. import (
  3. "log"
  4. "time"
  5. "github.com/brutella/hc/accessory"
  6. "github.com/bwdezend/astoria-hc/internal/telemetry"
  7. "github.com/stianeikeland/go-rpio"
  8. )
  9. func init() {
  10. rpio.Open()
  11. }
  12. // PowerButton sets up the button hooked to pin 26
  13. // on the Rasberry Pi to act as a high/low control
  14. // for the system.
  15. func PowerButton(acc accessory.Thermostat) {
  16. high := 124.0
  17. low := 30.0
  18. button := rpio.Pin(26)
  19. button.Input()
  20. button.PullUp()
  21. for {
  22. if button.Read() == 0 {
  23. log.Println("button pressed")
  24. if acc.Thermostat.TargetTemperature.GetValue() >= (high - 10.0) {
  25. SetTargetTemp(acc, low)
  26. } else {
  27. SetTargetTemp(acc, high)
  28. }
  29. time.Sleep(500 * time.Millisecond)
  30. }
  31. time.Sleep(50 * time.Millisecond)
  32. }
  33. }
  34. // HeaterControl turns on and off voltate to the
  35. // pin hooked to the Solid State Relay.
  36. func HeaterControl(on bool) {
  37. pin := rpio.Pin(14)
  38. pin.Output()
  39. telemetry.RelayActivations.Inc()
  40. if on {
  41. pin.High()
  42. } else {
  43. pin.Low()
  44. }
  45. }