-
Notifications
You must be signed in to change notification settings - Fork 5
/
raspberry_pi_door_controller.go
61 lines (52 loc) · 1.47 KB
/
raspberry_pi_door_controller.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 (
log "github.com/Sirupsen/logrus"
"github.com/stianeikeland/go-rpio"
"time"
)
type RaspberryPiDoorController struct {
relayPin rpio.Pin
contactSwitchPin rpio.Pin
}
func NewRaspberryPiDoorController(relayPinId int, contactSwitchPinId int) RaspberryPiDoorController {
// Open and map memory to access gpio, check for errors
if err := rpio.Open(); err != nil {
log.Error(err)
}
relayPin := rpio.Pin(relayPinId)
contactSwitchPin := rpio.Pin(contactSwitchPinId)
relayPin.Output()
contactSwitchPin.Input()
return RaspberryPiDoorController{relayPin: relayPin, contactSwitchPin: contactSwitchPin}
}
func (this RaspberryPiDoorController) toggleDoor() {
// Toggle pin on/off
this.relayPin.Toggle()
log.Infof("Toggle relay switch on")
time.Sleep(time.Millisecond * 500)
this.relayPin.Toggle()
log.Infof("Toggle relay switch off")
}
func (this RaspberryPiDoorController) getDoorState() DoorState {
log.Debugf("Using pin %d to read contact switch pin", this.contactSwitchPin)
pin := rpio.Pin(this.contactSwitchPin)
// Open and map memory to access gpio, check for errors
if err := rpio.Open(); err != nil {
log.Error(err)
}
// Read state
state := pin.Read()
log.Infof("Sensor reading state: %v", state)
doorState := int8(state)
if (doorState == int8(closed)) {
return closed
} else {
return open
}
}
func (this RaspberryPiDoorController) close() {
err := rpio.Close()
if err != nil {
log.WithError(err).Error("Could not close pins")
}
}