-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRig.py
269 lines (210 loc) · 7.31 KB
/
TestRig.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import sys
import time
import DetectPlatform
import ClydeLog
import RemoteArduino
TEST_FIRMWARE_VERSION = 0.60
class ArduinoPin:
def __init__(self, name, number, net = None, suppressHigh = False, suppressLow = False):
self.name = name
self.number = number
if(net == None):
self.net = name
else:
self.net = net
self.suppressHigh = suppressHigh
self.suppressLow = suppressLow
def __str__(self):
return self.name
class MeasurementPin():
def __init__(self, name, number, multiplier, base):
self.name = name
self.number = number
self.multiplier = multiplier
self.base = base
def __str__(self):
return self.name
class TestRig:
def __init__(self):
self.log = ClydeLog.getLogger()
self.board = None;
self.measurementPins = []
self.relayPins = []
self.shortTestPins = []
self.port = None
self.ports = []
def setPins(self, measurementPins, relayPins, shortTestPins):
self.measurementPins = measurementPins
self.relayPins = relayPins
self.shortTestPins = shortTestPins
def connect(self, ports):
serialPorts = DetectPlatform.ListSerialPorts()
self.ports = ports
self.port = None
self.log.debug("Found %d serial ports:" % len(serialPorts))
for serialPort in serialPorts:
self.log.debug(" - " + serialPort)
for port in ports:
if serialPorts.count(port) > 0:
self.port = port
break
if self.port != None:
self.arduino = RemoteArduino.RemoteArduino(self.port)
else:
raise Exception("No Arduino found on port " + port)
self.arduino.debug('OFF')
arduinoVersion = self.arduino.version()
if arduinoVersion < TEST_FIRMWARE_VERSION:
self.log.debug("Remote version '%.2f' too low, upgrade the Arduino sketch" % arduinoVersion)
sys.exit()
self.board = self.arduino.getBoard()
self.log.info("Found test rig: %s" % self.board);
self.reset()
def connectFirmware(self, ports):
serialPorts = DetectPlatform.ListSerialPorts()
self.ports = ports
self.port = None
self.log.debug("Found %d serial ports:" % len(serialPorts))
for serialPort in serialPorts:
self.log.debug(" - " + serialPort)
for port in ports:
if serialPorts.count(port) > 0:
self.port = port
break
if self.port != None:
self.arduino = RemoteArduino.RemoteArduino(self.port)
else:
raise Exception("No Arduino found on port " + port)
self.board = 'CLYDE'
self.log.info("Found board: %s" % self.board);
self.reset()
def disconnect(self):
self.reset()
self.arduino = None
self.board = None
def connected(self):
return self.arduino is not None
def disconnected(self):
return self.arduino is None
def reset(self):
if self.board is 'CLYDE':
return
#""" Turn off LEDs. """
self.arduino.led('GREEN', 'OFF');
self.arduino.led('RED', 'OFF');
self.arduino.led('WHITE', 'OFF');
#""" Reset pins to default state. """
# disable relays
for pin in self.relayPins:
self.arduino.relay(pin.number, 'OFF')
# TODO
# for pin in self.shortTestPins:
# self.arduino.pinMode(pin.number, 'INPUT')
# TODO: reset analog/measurement pins?
def led(self, color, status):
if self.board is not 'CLYDE':
self.arduino.led(color, status)
def enableRelay(self, relayName):
""" Enable an output relay """
for pin in self.relayPins:
if pin.name == relayName:
self.arduino.relay(pin.number, 'ON')
return
raise Exception("Relay " + relayName + " not found!")
def disableRelay(self, relayName):
""" Disable an output relay """
for pin in self.relayPins:
if pin.name == relayName:
self.arduino.relay(pin.number, 'OFF')
return
raise Exception("Relay " + relayName + " not found!")
def output(self, pinName, state):
""" Set a digital pin to output LOW or HIGH """
for pin in self.shortTestPins:
if pin.name == pinName:
self.arduino.pinMode(pin.number, 'OUTPUT')
self.arduino.digitalWrite(pin.number, state)
return
raise Exception("Pin" + pinName + " not found!")
def pwm(self, pinName, value):
""" Set a digital pin to output a PWM value """
for pin in self.shortTestPins:
if pin.name == pinName:
self.arduino.pinMode(pin.number, 'OUTPUT')
self.arduino.analogWrite(pin.number, value)
return
raise Exception("Pin" + pinName + " not found!")
def setInput(self, pinName):
""" Set up a pin as input """
for pin in self.shortTestPins:
if pin.name == pinName:
self.arduino.pinMode(pin.number, 'INPUT')
return
raise Exception("Pin" + pinName + "not found!")
#def setInputPullup(self, pinName):
# """ Set up a pin as input in pullup mode """
# for pin in self.shortTestPins:
# if pin.name == pinName:
# self.arduino.pinMode(pin.number, 'INPUT_PULLUP')
# return
# raise Exception("Pin" + pinName + "not found!")
#def readInput(self, pinName):
# """ Set up a pin as input in pullup mode """
# for pin in self.shortTestPins:
# if pin.name == pinName:
# return self.arduino.digitalRead(pin.number)
# raise Exception("Pin" + pinName + "not found!")
def measure(self, measurementName):
""" Read a measurement pin """
for pin in self.measurementPins:
if pin.name == measurementName:
return pin.multiplier*(self.arduino.analogRead(pin.number) + pin.base)
raise Exception("Measurement pin " + measurementName + " not found!")
def current(self, voltage='5V'):
""" Read current from current sensor """
return self.arduino.current(voltage)
def initMPR121(self, address):
""" Initialize the MPR121 using default values """
return self.arduino.initMPR121(address)
def waitForTouch(self, address, pinName, timeout):
""" Returns the touch states of the MPR121 as soon as a touch is detected, or until timeout. """
for pin in self.shortTestPins:
if pin.name == pinName:
return self.arduino.waitForTouch(address, pin.number, timeout)
raise Exception("Pin" + pinName + " not found!")
def resetEEPROM(self):
return self.arduino.resetEEPROM()
def writeEEPROM(self, addr, value):
return self.arduino.writeEEPROM(addr, value)
def readEEPROM(self, addr):
return self.arduino.readEEPROM(addr)
def writeSerial(self, addr, serial):
if len(serial) > 6:
return False
for c in serial:
success = self.arduino.writeEEPROM(addr, ord(c))
if success == False:
return False
addr += 1
return True
def readSerial(self, addr, length):
serial = ""
for i in xrange(0, length):
serial += chr(self.arduino.readEEPROM(addr+i))
return serial
def idModule(self, dpinName, apinName):
dpin = None;
apin = None;
for pin in self.shortTestPins:
if pin.name == dpinName:
dpin = pin.number
for pin in self.measurementPins:
if pin.name == apinName:
apin = pin.number
if apin == None:
raise Exception("Pin" + apinName + " not found!")
elif dpin == None:
raise Exception("Pin" + dpinName + " not found!")
else:
return self.arduino.idModule(dpin, apin)
rig = TestRig();