forked from Dan-in-CA/SIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_pins.py
95 lines (83 loc) · 2.94 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
import gv
try:
import RPi.GPIO as GPIO # Required for accessing General Purpose Input Output pins on Raspberry Pi
gv.platform = 'pi'
except ImportError:
try:
import Adafruit_BBIO.GPIO as GPIO # Required for accessing General Purpose Input Output pins on Beagle Bone Black
gv.platform = 'bo'
except ImportError:
gv.platform = '' # if no platform, allows program to still run.
print 'No GPIO module was loaded from GPIO Pins module'
# Makes it runnable on machines other than RPi
#GPIO = None
try:
GPIO.setwarnings(False)
GPIO.cleanup()
except Exception:
pass
#### pin defines ####
try:
if gv.platform == 'pi': # If this will run on Raspberry Pi:
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 = 13
pin_sr_clk = 7
pin_sr_noe = 11
pin_sr_lat = 15
pin_rain_sense = 8
pin_relay = 10
elif gv.platform == 'bo': # If this will run on Beagle Bone Black:
pin_sr_dat = "P9_11"
pin_sr_clk = "P9_13"
pin_sr_noe = "P9_14"
pin_sr_lat = "P9_12"
pin_rain_sense = "P9_15"
pin_relay = "P9_16"
except AttributeError:
pass
#### setup GPIO pins as output or input ####
try:
GPIO.setup(pin_sr_noe, GPIO.OUT)
GPIO.output(pin_sr_noe, GPIO.HIGH)
GPIO.setup(pin_sr_clk, GPIO.OUT)
GPIO.output(pin_sr_clk, GPIO.LOW)
GPIO.setup(pin_sr_dat, GPIO.OUT)
GPIO.output(pin_sr_dat, GPIO.LOW)
GPIO.setup(pin_sr_lat, GPIO.OUT)
GPIO.output(pin_sr_lat, GPIO.LOW)
GPIO.setup(pin_rain_sense, GPIO.IN)
GPIO.setup(pin_relay, GPIO.OUT)
except NameError:
pass
def disableShiftRegisterOutput():
"""Disable output from shift register."""
try:
GPIO.output(pin_sr_noe, GPIO.HIGH)
except NameError:
pass
def enableShiftRegisterOutput():
"""Enable output from shift register."""
try:
GPIO.output(pin_sr_noe, GPIO.LOW)
except NameError:
pass
def setShiftRegister(srvals):
"""Set the state of each output pin on the shift register from the srvals list."""
try:
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 NameError:
pass
def set_output():
"""Activate triacs according to shift register state."""
disableShiftRegisterOutput()
setShiftRegister(gv.srvals) # gv.srvals stores shift register state
enableShiftRegisterOutput()