-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheater.go
61 lines (49 loc) · 905 Bytes
/
heater.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"github.com/mrmorphic/hwio"
"log"
)
type Heater interface {
TurnOn()
TurnOff()
ShutDown()
On() bool
}
// Heater is a light wrapper around the hwio package
type RealHeater struct {
on bool
pin hwio.Pin
}
func (h *RealHeater) On() bool {
return h.on
}
func (h *RealHeater) TurnOn() {
h.setPin(true)
h.on = true
}
func (h *RealHeater) TurnOff() {
h.setPin(false)
h.on = false
}
func (h *RealHeater) ShutDown() {
h.TurnOff()
hwio.CloseAll()
}
func (h *RealHeater) setPin(on bool) {
if h.pin == 0 { // pin will be 0 if not set
h.openPin()
}
if on {
hwio.DigitalWrite(h.pin, hwio.LOW)
} else {
hwio.DigitalWrite(h.pin, hwio.HIGH)
}
}
func (h *RealHeater) openPin() {
hwio.SetDriver(new(hwio.RaspberryPiDTDriver))
pin, err := hwio.GetPinWithMode("gpio17", hwio.OUTPUT)
if err != nil {
log.Printf("Error opening pin! %s\n", err)
}
h.pin = pin
}