-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshiftpi.py
138 lines (107 loc) · 3.2 KB
/
shiftpi.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
'''
A library that allows simple access to 74HC595 shift registers on a Raspberry Pi using any digital I/O pins.
'''
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
version = "0.2"
version_info = (0, 2)
# Define MODES
ALL = -1
HIGH = 1
LOW = 0
# Define pins
_SER_pin = 26 #25 #pin 14 on the 75HC595
_RCLK_pin = 19 #24 #pin 12 on the 75HC595
_SRCLK_pin = 6 #23 #pin 11 on the 75HC595
# is used to store states of all pins
_registers = list()
#How many of the shift registers - you can change them with shiftRegisters method
_number_of_shiftregisters = 1
def pinsSetup(**kwargs):
'''
Allows the user to define custom pins
'''
global _SER_pin, _RCLK_pin, _SRCLK_pin
custompins = 0
serpin = _SER_pin
rclkpin = _RCLK_pin
srclkpin = _SRCLK_pin
if len(kwargs) > 0:
custompins = 1
_SER_pin = kwargs.get('ser', _SER_pin)
_RCLK_pin = kwargs.get('rclk', _RCLK_pin)
_SRCLK_pin = kwargs.get('srclk', _SRCLK_pin)
if custompins:
if _SER_pin != serpin or _RCLK_pin != rclkpin or _SRCLK_pin != srclkpin:
GPIO.setwarnings(True)
else:
GPIO.setwarnings(False)
GPIO.setup(_SER_pin, GPIO.OUT)
GPIO.setup(_RCLK_pin, GPIO.OUT)
GPIO.setup(_SRCLK_pin, GPIO.OUT)
def startupMode(mode, execute = False):
'''
Allows the user to change the default state of the shift registers outputs
'''
if isinstance(mode, int):
if mode is HIGH or mode is LOW:
_all(mode, execute)
else:
raise ValueError("The mode can be only HIGH or LOW or Dictionary with specific pins and modes")
elif isinstance(mode, dict):
for pin, mode in mode.iteritems():
_setPin(pin, mode)
if execute:
_execute()
else:
raise ValueError("The mode can be only HIGH or LOW or Dictionary with specific pins and modes")
def shiftRegisters(num):
'''
Allows the user to define the number of shift registers are connected
'''
global _number_of_shiftregisters
_number_of_shiftregisters = num
_all(LOW)
def digitalWrite(pin, mode):
#print "digital write"
'''
Allows the user to set the state of a pin on the shift register
'''
if pin == ALL:
_all(mode)
else:
if len(_registers) == 0:
_all(LOW)
_setPin(pin, mode)
_execute()
def delay(millis):
'''
Used for creating a delay between commands
'''
millis_to_seconds = float(millis)/1000
return sleep(millis_to_seconds)
def _all_pins():
return _number_of_shiftregisters * 8
def _all(mode, execute = True):
all_shr = _all_pins()
for pin in range(0, all_shr):
_setPin(pin, mode)
if execute:
_execute()
return _registers
def _setPin(pin, mode):
try:
_registers[pin] = mode
except IndexError:
_registers.insert(pin, mode)
def _execute():
all_pins = _all_pins()
GPIO.output(_RCLK_pin, GPIO.LOW)
for pin in range(all_pins -1, -1, -1):
GPIO.output(_SRCLK_pin, GPIO.LOW)
pin_mode = _registers[pin]
GPIO.output(_SER_pin, pin_mode)
GPIO.output(_SRCLK_pin, GPIO.HIGH)
GPIO.output(_RCLK_pin, GPIO.HIGH)
pinsSetup()