-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelayDriver.py
143 lines (119 loc) · 2.86 KB
/
relayDriver.py
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
#
# relayDriver
# - 2015 Brent Petit
#
# routines for controlling a relay board throug RPi
# GPIO pins.
#
import time
import RPi.GPIO as GPIO
# DEBUGGING
debug = 0
# Mapping of relay channels to GPIO pins
# channel: GPIO pin
# Add entries for more relays
relay_pins = {
1: 11,
2: 12,
3: 13,
4: 15
}
# Helpers
relay_open = GPIO.HIGH
relay_closed = GPIO.LOW
# Pulse time - how long to hold pulseRelay
pulse_time = .2
# Global status flag
relayBoardInitialized = 0
#
# initRelayBoard
# Fire up GPIO library and set the GPIO pins
# up for the relay board
# This must be run prior to accessing the relays
#
def initRelayBoard():
global relayBoardInitialized
if relayBoardInitialized == 1:
print "Error: Board already initialized"
return -1
GPIO.setmode(GPIO.BOARD)
# Iterate through pin map and init each pin
# set each relay to open
for relay, pin in relay_pins.iteritems():
if debug:
print "Setting up GPIO pin ", pin
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
relayBoardInitialized = 1
return 0
def shutdownRelayBoard():
global relayBoardInitialized
if relayBoardInitialized == 0:
print "Error: Board not initialized"
return -1
if debug:
print "calling cleanup on GPIO"
GPIO.cleanup()
relayBoardInitialized = 0;
return 0
# getRelayState
# return the state of the queried relay
#
def getRelayState(channel):
pin = relay_pins[channel]
return GPIO.input(pin)
# setRelayState
# set the state to the passed in state
def setRelayState(channel, state):
pin = relay_pins[channel]
GPIO.output(pin, state)
#
# isRelayOpen
# return true if queried relay is in open
# state
def isRelayOpen(channel):
return (getRelayState(channel) == relay_open)
#
# isRelayClosed
# return true if queried relay is in closed
# state
def isRelayClosed(channel):
return (getRelayState(channel) == relay_closed)
#
# openRelay
# flip the channel state to open
def openRelay(channel):
# Check pin state
if isRelayClosed(channel):
setRelayState(channel, relay_open)
else:
if debug:
print "open_relay ", pin, " already open"
#
# closeRelay
# flip the channel state to closed
def closeRelay(channel):
# Check pin state
if isRelayOpen(channel):
setRelayState(channel, relay_closed)
else:
if debug:
print "close_relay ", pin, " already closed"
#
# pulseRelay
# flip the state of relay for a short time
#
def pulseRelay(channel):
toggleRelay(channel)
time.sleep(pulse_time)
toggleRelay(channel)
#
# toggleRelay
# flip the channel state from the current
# state to the opposite state.
def toggleRelay(channel):
if isRelayClosed(channel):
openRelay(channel)
else:
closeRelay(channel)
# END