-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestADC.py
128 lines (96 loc) · 3.44 KB
/
testADC.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
MILLIVOLTS_PER_VOLT = 1000
def V2mV(volts):
return int(volts * MILLIVOLTS_PER_VOLT)
#
# All values in millivolts to make everything integers
#
MAX_VOLTAGE = V2mV(13.3)
MIN_VOLTAGE = V2mV(10.5)
DEFAULT_STEP = 50
#
# Use pytest to run all functions that contain test_ in the name
#
# pytest testADC.py -- runs test without print output (unless it fails)
# pytest -s testADC.py -- runs test always containing print output
#
# Check out pytest options to up your game
"""
Acts as a fake MPC3008. A list of numbers passed through the
constructor provides the readings to be returned on each call.
"""
class fake0MPC3008:
"""
If DATA is omitted, or is empty, the voltage() call always returns MAX_VOLTAGE
DATA should be an indexable list of voltage values.
"""
def __init__(self, data=None):
self.reading_index = 0
self.data = data
def voltage(self):
print("Getting voltage index:", self.reading_index)
try:
current_reading = self.data[self.reading_index]
except:
current_reading = MAX_VOLTAGE
self.reading_index+=1
print("Got reading: ", current_reading)
return current_reading
def test_fake0MPC3008():
print("Testing fake0 (data driven) ADC")
a = fake0MPC3008()
assert(a.voltage() == MAX_VOLTAGE)
assert(a.voltage() == MAX_VOLTAGE)
test_data = [ MAX_VOLTAGE, 12000, 11000, 10800, 11000, 12000, MAX_VOLTAGE]
b = fake0MPC3008(test_data)
for t in test_data:
assert(b.voltage() == t)
print("End Testing fake0")
class fake1MPC3008():
"""
This fake data source accepts a MIN, MAX, and STEP and serves up
the voltage values starting with max down STEP wise until hitting MIN,
and then STEP wise back up to MAX.
"""
def __init__(self, Vmin=MIN_VOLTAGE, Vmax=MAX_VOLTAGE, step=DEFAULT_STEP):
self.vmax = Vmax
self.vmin = Vmin
self.step = step
self.reading = Vmax
self.charge_direction = -1
self.seqnum = 0 # count calls, label output groups
def voltage(self):
self.seqnum+=1
current_reading = self.reading
if current_reading <= self.vmin:
print(self.seqnum, ": Bottomed out at:", current_reading)
current_reading = self.vmin
self.reading = self.vmin
self.charge_direction=1 # switch to charging
# use the step and direction to compute next reading
self.reading+=(self.charge_direction * self.step)
print(self.seqnum, ": Next reading computed:", self.reading)
# give steady MAX_VOLTAGE when done
if self.charge_direction==1 and self.reading >= self.vmax:
self.reading=MAX_VOLTAGE
print(self.seqnum, ": Reading:", current_reading)
return current_reading
def test_fake1MPC3008():
print("Testing fake1 (generated ADC)")
# create with no args (use defaults)
a = fake1MPC3008()
r = MAX_VOLTAGE
# check it on the way down
while r >= MIN_VOLTAGE:
assert(r == a.voltage())
r-=DEFAULT_STEP
else:
r+=DEFAULT_STEP
#
# The a.voltage() call above already turned around with an increase
# And it also decremented the r. We have to catch up with
# another increment. Probably a small bug here.
r+=DEFAULT_STEP
while r <= MAX_VOLTAGE:
assert(r == a.voltage())
r+=DEFAULT_STEP
print("End Testing fake1")