forked from Dan-in-CA/SIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_pins.py
executable file
·194 lines (169 loc) · 6.44 KB
/
gpio_pins.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
import gv
try:
import RPi.GPIO as GPIO
gv.platform = 'pi'
rev = GPIO.RPI_REVISION
if rev == 1:
# map 26 physical pins (1based) with 0 for pins that do not have a gpio number
if gv.use_pigpio:
gv.pin_map = [0,0,0,0,0,1,0,4,14,0,15,17,18,21,0,22,23,0,24,10,0,9,25,11,8,0,7]
else:
gv.pin_map = [0,0,0,0,0,5,0,7,8,0,10,11,12,13,0,15,16,0,18,19,0,21,22,23,24,0,26]
elif rev == 2:
# map 26 physical pins (1based) with 0 for pins that do not have a gpio number
if gv.use_pigpio:
gv.pin_map = [0,0,0,2,0,3,0,4,14,0,15,17,18,27,0,22,23,0,24,10,0,9,25,11,8,0,7]
else:
gv.pin_map = [0,0,0,0,0,5,0,7,8,0,10,11,12,13,0,15,16,0,18,19,0,21,22,23,24,0,26]
elif rev == 3:
# map 40 physical pins (1based) with 0 for pins that do not have a gpio number
if gv.use_pigpio:
gv.pin_map = [0,0,0,2,0,3,0,4,14,0,15,17,18,27,0,22,23,0,24,10,0,9,25,11,8,0,7,0,0,5,0,6,12,13,0,19,16,26,20,0,21]
else:
gv.pin_map = [0,0,0,3,0,5,0,7,8,0,10,11,12,13,0,15,16,0,18,19,0,21,22,23,24,0,26,0,0,29,0,31,32,33,0,35,36,37,38,0,40]
else:
print 'Unknown pi pin revision. Using pin mapping for rev 3'
except ImportError:
try:
import Adafruit_BBIO.GPIO as GPIO # Required for accessing General Purpose Input Output pins on Beagle Bone Black
gv.pin_map = [''*11] # map only the pins we are using
gv.pin_map.extend(['P9_'+str(i) for i in range(11,17)])
gv.platform = 'bo'
except ImportError:
gv.pin_map = [i for i in range(27)] # assume 26 pins all mapped. Maybe we should not assume anything, but...
gv.platform = '' # if no platform, allows program to still run.
print 'No GPIO module was loaded from GPIO Pins module'
from blinker import signal
zone_change = signal('zone_change')
try:
if gv.use_pigpio:
import pigpio
pi = pigpio.pi()
else:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) #IO channels are identified by header connector pin numbers. Pin numbers are
except Exception:
pass
global pin_rain_sense
try:
if gv.platform == 'pi': # If this will run on Raspberry Pi:
pin_rain_sense = gv.pin_map[8]
elif gv.platform == 'bo': # If this will run on Beagle Bone Black:
pin_rain_sense = gv.pin_map[15]
except AttributeError:
pass
try:
if gv.use_pigpio:
pi.set_mode(pin_rain_sense, pigpio.INPUT)
else:
GPIO.setup(pin_rain_sense, GPIO.IN)
except NameError:
pass
def setup_pins():
"""
Define and setup GPIO pins for shift register operation
"""
global pin_sr_dat
global pin_sr_clk
global pin_sr_noe
global pin_sr_lat
global pi
try:
if gv.platform == 'pi': # If this will run on Raspberry Pi:
if not gv.use_pigpio:
GPIO.setmode(GPIO.BOARD) # IO channels are identified by header connector pin numbers. Pin numbers are always the same regardless of Raspberry Pi board revision.
pin_sr_dat = gv.pin_map[13]
pin_sr_clk = gv.pin_map[7]
pin_sr_noe = gv.pin_map[11]
pin_sr_lat = gv.pin_map[15]
elif gv.platform == 'bo': # If this will run on Beagle Bone Black:
pin_sr_dat = gv.pin_map[11]
pin_sr_clk = gv.pin_map[13]
pin_sr_noe = gv.pin_map[14]
pin_sr_lat = gv.pin_map[12]
except AttributeError:
pass
#### setup GPIO pins as output or input ####
try:
if gv.use_pigpio:
pi.set_mode(pin_sr_noe, pigpio.OUTPUT)
pi.set_mode(pin_sr_clk, pigpio.OUTPUT)
pi.set_mode(pin_sr_dat, pigpio.OUTPUT)
pi.set_mode(pin_sr_lat, pigpio.OUTPUT)
pi.write(pin_sr_noe, 1)
pi.write(pin_sr_clk, 0)
pi.write(pin_sr_dat, 0)
pi.write(pin_sr_lat, 0)
else:
GPIO.setup(pin_sr_noe, GPIO.OUT)
GPIO.setup(pin_sr_clk, GPIO.OUT)
GPIO.setup(pin_sr_dat, GPIO.OUT)
GPIO.setup(pin_sr_lat, GPIO.OUT)
GPIO.output(pin_sr_noe, GPIO.HIGH)
GPIO.output(pin_sr_clk, GPIO.LOW)
GPIO.output(pin_sr_dat, GPIO.LOW)
GPIO.output(pin_sr_lat, GPIO.LOW)
except NameError:
pass
def disableShiftRegisterOutput():
"""Disable output from shift register."""
global pi
try:
pin_sr_noe
except NameError:
if gv.use_gpio_pins:
setup_pins()
try:
if gv.use_pigpio:
pi.write(pin_sr_noe, 1)
else:
GPIO.output(pin_sr_noe, GPIO.HIGH)
except Exception:
pass
def enableShiftRegisterOutput():
"""Enable output from shift register."""
global pi
try:
if gv.use_pigpio:
pi.write(pin_sr_noe, 0)
else:
GPIO.output(pin_sr_noe, GPIO.LOW)
except Exception:
pass
def setShiftRegister(srvals):
"""Set the state of each output pin on the shift register from the srvals list."""
global pi
try:
if gv.use_pigpio:
pi.write(pin_sr_clk, 0)
pi.write(pin_sr_lat, 0)
for s in range(gv.sd['nst']):
pi.write(pin_sr_clk, 0)
if srvals[gv.sd['nst']-1-s]:
pi.write(pin_sr_dat, 1)
else:
pi.write(pin_sr_dat, 0)
pi.write(pin_sr_clk, 1)
pi.write(pin_sr_lat, 1)
else:
GPIO.output(pin_sr_clk, GPIO.LOW)
GPIO.output(pin_sr_lat, GPIO.LOW)
for s in range(gv.sd['nst']):
GPIO.output(pin_sr_clk, GPIO.LOW)
if srvals[gv.sd['nst']-1-s]:
GPIO.output(pin_sr_dat, GPIO.HIGH)
else:
GPIO.output(pin_sr_dat, GPIO.LOW)
GPIO.output(pin_sr_clk, GPIO.HIGH)
GPIO.output(pin_sr_lat, GPIO.HIGH)
except Exception:
pass
def set_output():
"""Activate triacs according to shift register state."""
with gv.output_srvals_lock:
gv.output_srvals = gv.srvals
disableShiftRegisterOutput()
setShiftRegister(gv.output_srvals) # gv.srvals stores shift register state
enableShiftRegisterOutput()
zone_change.send()