-
Notifications
You must be signed in to change notification settings - Fork 0
/
GpioController.py
134 lines (128 loc) · 4.08 KB
/
GpioController.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
import RPi.GPIO as io
from sys import exit
class GpioController(object):
#constants
BOARD_MODE=""
BCM = "BCM"
BOARD = "BOARD"
ALLOWED_PINS_BCM = [4, 17, 18, 21, 22, 23, 24, 25]
ALLOWED_PINS_BOARD = [0, 1, 2, 3, 4, 5, 6, 7]
ALLOWED_PINS = []
USED_PINS = []
INITIATED = False
NO_INSTANCES = 0
def __init__(self, pins, boardmode="BCM", showWarnings=False):
# we created a new instance
self.pins = []
self.NO_INSTANCES = self.NO_INSTANCES + 1
#set warnings in io
if showWarnings is False:
io.setwarnings(False)
else:
io.setwarnings(True)
#initiate the board
if self.INITIATED is False:
if boardmode=="BCM":
self.BOARD_MODE="BCM"
self.ALLOWED_PINS = self.ALLOWED_PINS_BCM
io.setmode(io.BCM)
self.INITIATED = True
elif boardmode=="BOARD":
self.BOARD_MODE="BOARD"
self.ALLOWED_PINS = self.ALLOWED_PINS_BOARD
io.setmode(io.BOARD)
self.INITIATED = True
else:
self.cleanup()
raise ValueError('%s is not an allowed, use "BCM" or "BOARD"' % boardmode)
else: #INITIATED == TRUE
# check that we dont have two conflicting modes
if(boardmode != self.BOARD_MODE):
self.cleanup()
raise ValueError('Board mode is already set to %s , cannot use another mode' % self.BOARD_MODE)
# Check pins
# save pins to a local variable for cleanup
for pin in pins:
if(pin not in self.ALLOWED_PINS):
self.cleanup()
raise ValueError('%s is not an allowed pin for %s' % (pin, self.BOARD_MODE))
if(pin in self.USED_PINS):
self.cleanup()
raise ValueError('Pin %s is already in use by another device' % pin)
# Pin is ready to use
self.pins.append(pin)
self.USED_PINS.append(pin)
def cleanup(self):
# cleanup pins so that other things can use it
for pin in self.pins:
self.USED_PINS.remove(pin)
self.NO_INSTANCES = self.NO_INSTANCES - 1
if self.NO_INSTANCES == 0:
io.cleanup()
# Tests
if __name__ == '__main__':
print("==== gpioController - tests ====")
# test pins
try:
print("-------------------------------")
print("Wrong pin numbers")
t1 = gpioController([777,444,333])
except ValueError, e:
print("OK")
print(e)
t1 = None
try:
print("-------------------------------")
print("Wrong boardname")
t1 = gpioController([4],"NOTABOARD")
except ValueError, e:
print("OK")
print(e)
t1 = None
try:
print("-------------------------------")
print("Same pin numbers in the same init")
t1 = gpioController([4,4,4])
except ValueError, e:
print("OK")
print(e)
t1 = None
try:
print("-------------------------------")
print("Using the same pin in two different controllers")
print("the second init should fail")
print("First one")
t1 = gpioController([4,17,18])
except ValueError, e:
print("Failed to create first one!")
print(e)
sys.exit(1)
try:
print("Second one")
t2 = gpioController([21,22,4])
except ValueError, e:
print("OK")
print(e)
t1.cleanup()
t1 = None
t2 = None
try:
print("-------------------------------")
print("Try using two different boardmodes")
print("Creating first one with BCM")
t1 = gpioController([4],"BCM")
except ValueError, e:
print("First one failed!")
print(e)
sys.exit(1)
try:
print("Creating second one with BOARD")
t2 = gpioController([1],"BOARD")
except ValueError, e:
print("OK")
print(e)
t1.cleanup()
t1 = None
t2 = None
print("All tests passed")
print("================")