-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.go
149 lines (124 loc) · 3.36 KB
/
relay.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"sync"
"time"
"github.com/warthog618/gpiod"
"github.com/warthog618/gpiod/device/rpi"
)
var once sync.Once
type gpiodProperties struct {
relay4line *gpiod.Line
relay3line *gpiod.Line
relay2line *gpiod.Line
relay1line *gpiod.Line
chip *gpiod.Chip
}
// ChangeRelayStatus struct
type ChangeRelayStatus struct {
relayNumber int
relayNewValue bool
changedAt time.Time
}
// RelayController struct
type RelayController struct {
gpiodProperties
changeStatusChannel chan (ChangeRelayStatus)
}
var controllerInstance *RelayController
// SetRelay1Status allows set relay #1 status
func (gc RelayController) SetRelay1Status(status bool) {
if gc.relay1line.SetValue(boolToInt(status)) == nil {
gc.changeStatusChannel <- ChangeRelayStatus{
relayNumber: 1,
relayNewValue: status,
changedAt: time.Now(),
}
}
}
// SetRelay2Status allows set relay #2 status
func (gc RelayController) SetRelay2Status(status bool) {
if gc.relay2line.SetValue(boolToInt(status)) == nil {
gc.changeStatusChannel <- ChangeRelayStatus{
relayNumber: 2,
relayNewValue: status,
changedAt: time.Now(),
}
}
}
// SetRelay3Status allows set relay #3 status
func (gc RelayController) SetRelay3Status(status bool) {
if gc.relay3line.SetValue(boolToInt(status)) == nil {
gc.changeStatusChannel <- ChangeRelayStatus{
relayNumber: 3,
relayNewValue: status,
changedAt: time.Now(),
}
}
}
// SetRelay4Status allows set relay #4 status
func (gc RelayController) SetRelay4Status(status bool) {
if gc.relay4line.SetValue(boolToInt(status)) == nil {
gc.changeStatusChannel <- ChangeRelayStatus{
relayNumber: 4,
relayNewValue: status,
changedAt: time.Now(),
}
}
}
// GetAllRelaysStatus returns all relays status
func (gc RelayController) GetAllRelaysStatus() (bool, bool, bool, bool) {
relay1value, _ := gc.relay1line.Value()
relay2value, _ := gc.relay2line.Value()
relay3value, _ := gc.relay3line.Value()
relay4value, _ := gc.relay4line.Value()
return intToBool(relay1value), intToBool(relay2value), intToBool(relay3value), intToBool(relay4value)
}
// Close releases all resources
func (gc RelayController) Close() {
close(gc.changeStatusChannel)
gc.relay1line.Close()
gc.relay2line.Close()
gc.relay3line.Close()
gc.relay4line.Close()
gc.chip.Close()
}
// InitController initialize the controller
func InitController() error {
if controllerInstance == nil {
once.Do(func() {
cc := gpiod.Chips()
c, _ := gpiod.NewChip(cc[0])
relay1line, _ := c.RequestLine(rpi.J8p15, gpiod.AsOutput(0)) // RELAY 1
relay2line, _ := c.RequestLine(rpi.J8p13, gpiod.AsOutput(0)) // RELAY 2
relay3line, _ := c.RequestLine(rpi.J8p11, gpiod.AsOutput(0)) // RELAY 3
relay4line, _ := c.RequestLine(rpi.J8p7, gpiod.AsOutput(0)) // RELAY 4
controllerInstance = &RelayController{
changeStatusChannel: make(chan ChangeRelayStatus),
gpiodProperties: gpiodProperties{
chip: c,
relay1line: relay1line,
relay2line: relay2line,
relay3line: relay3line,
relay4line: relay4line,
},
}
})
}
return nil
}
// GetControllerInstance gets relay controller instance
func GetControllerInstance() *RelayController {
return controllerInstance
}
func boolToInt(boolean bool) int {
if boolean {
return 1
}
return 0
}
func intToBool(integer int) bool {
if integer == 0 {
return false
}
return true
}