-
Notifications
You must be signed in to change notification settings - Fork 2
/
pinDef.py
225 lines (205 loc) · 6.33 KB
/
pinDef.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
#
# Pin Definitions
#
# Description:
# define what pins do what things - either by specifying a PCB version or defining each pin
#
# Variables:
# pins - dict of all pin names and numbers, this is what is used by other functions
# __pcbVersion - the pcb version specified in __settings file
# __pcbVersionsAvailable - allowable values of __pcbVersion
# pcbPinout - what each pin definition is by PCB version
#
# Functions:
#
# __init__(__systemHandler, __settings, __logger)
# setup the usable pin defs
# get from pcb version
# get from custom - this will overwrite any pcb defaults
# make sure all critical pins are defined
#
# __setByPcb()
# get pin definitions as shown by pcb version in __settings
#
# __setByCustom()
# get pin definitions as defined in the __settings file
#
class pinDef:
#
# default pins
# these are the ones used by the hat
#
pins = {
"doorStrike": None,
"doorbell12": None,
"doorbellCc": None,
"readerLed": None,
"readerBuzz": None,
"doorbellButton": None,
"doorSensor": None,
"piActiveLed": None,
"spareLed": None,
"wiegand0": None,
"wiegand1": None,
"exitButton": None
}
# Pins which MUST be defined in order to have basic DIYAC functionality
# these pins MUST be initialised as 'None' in pins above
__criticalPins = {
"wiegand0",
"wiegand1",
"doorStrike",
"doorbellButton",
"doorbell12"
}
# GP Input pins - not wiegand though
__inputPins = {
"doorbellButton",
"doorSensor",
"exitButton"
}
# GP Output pins
__outputPins = {
"doorStrike",
"doorbell12",
"doorbellCc",
"readerLed",
"readerBuzz",
"piActiveLed",
"spareLed"
}
__pcbVersion = None
__pcbVersionsAvailable = [1, 2.0, 2.1]
__pcbPinouts = {
1: {
"doorStrike": 17,
"doorbell12": 4,
"doorbellCc": 26,
"readerLed": 27,
"readerBuzz": 22,
"doorbellButton": 5,
"doorSensor": 6,
"piActiveLed": 13,
"spareLed": 19,
"wiegand0": 14,
"wiegand1": 15
},
2.0: {
"doorStrike": 27,
"doorbell12": 22,
"doorbellCc": 17,
"readerLed": 6,
"readerBuzz": 5,
"doorbellButton": 26,
"doorSensor": 20,
"piActiveLed": 4,
"spareLed": 3,
"wiegand0": 19,
"wiegand1": 13
},
2.1: {
"doorStrike": 27,
"doorbell12": 22,
"doorbellCc": 17,
"readerLed": 6,
"readerBuzz": 5,
"doorbellButton": 26,
"doorSensor": 20,
"piActiveLed": 4,
"spareLed": 3,
"wiegand0": 19,
"wiegand1": 13,
"exitButton": 10
}
}
#
# function to get pins from settings
#
def __init__(self, systemHandler, settings, logger):
# internalise __settings and logger
self.__systemHandler = systemHandler
del systemHandler
self.__logger = logger
del logger
self.__settings = settings
del settings
# get pins from PCB Version
# get any other pins that have been set
self.__setByPcb()
self.__setByCustom()
# make sure critical pins are set
# crash out if critical pins are still 'None'
missingCriticalPins = []
for p in list(self.pins):
if self.pins[p] is None:
if p in self.__criticalPins:
missingCriticalPins.append(p)
else:
del self.pins[p]
self.__logger.log("WARN", "pin not defined", {"pin": p})
if missingCriticalPins:
self.__systemHandler.quit(1, "Failed - Critical pin/s not defined", "ERRR", "Critical pin/s not defined", missingCriticalPins)
self.__sortInputOutputPins()
# done
return
#
# set pinout by PCB version
#
def __setByPcb(self):
# see if it's set
try:
self.__settings.allSettings["pinDef"]["pcbVersion"]
except KeyError:
pass
else:
self.__pcbVersion = self.__settings.allSettings["pinDef"]["pcbVersion"]
# make sure it's a valid value
if self.__pcbVersion in self.__pcbVersionsAvailable:
# store it
self.__logger.log("DBUG", "PCB Version found", {"version": self.__pcbVersion})
else:
self.__systemHandler.quit(1, "Failed - PCB Version set, but not recognised", "ERRR", "PCB Version set, but not recognised", {"version": self.__pcbVersion})
# if it's defined, set the values
if self.__pcbVersion is not None:
for p in self.pins:
if p in self.__pcbPinouts[self.__pcbVersion]:
self.pins[p] = self.__pcbPinouts[self.__pcbVersion][p]
# self.__logger.log("DBUG", "pin from PCB", self.pins[p])
# done
return
#
# set individual pins from __settings
#
def __setByCustom(self):
# is it set?
try:
self.__settings.allSettings["pinDef"]
except KeyError:
pass
else:
# grab it all in
for p in self.pins:
# but first make sure it's been set in settings
try:
self.__settings.allSettings["pinDef"][p]
except KeyError:
pass
else:
self.pins[p] = self.__settings.allSettings["pinDef"][p]
self.__logger.log("DBUG", "custom pin set", {"name": p, "pin": self.pins[p]})
# done
return
#
# Create two lists, one for input and one for output pins
#
def __sortInputOutputPins(self):
inputPins = {}
outputPins = {}
for pin in self.pins:
if pin in self.__inputPins:
inputPins = {**inputPins, pin: self.pins[pin]}
elif pin in self.__outputPins:
outputPins = {**outputPins, pin: self.pins[pin]}
self.pins["input"] = inputPins
self.pins["output"] = outputPins