Skip to content

Commit 0096a4b

Browse files
committed
tests/pyb/adc.py: Fix test so that it really does test ADC values.
Reading into a bytearray will truncate values to 0xff so the assertions checking read_timed() would previously always succeed. Thanks to @peterhinch for finding this problem and providing the solution.
1 parent de9528d commit 0096a4b

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

tests/pyb/adc.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
from pyb import ADC
2-
from pyb import Pin
1+
from pyb import ADC, Timer
32

4-
pin = Pin('X22', mode=Pin.IN, pull=Pin.PULL_DOWN)
5-
adc = ADC('X22')
6-
print(adc)
3+
adct = ADC(16) # Temperature 930 -> 20C
4+
print(adct)
5+
adcv = ADC(17) # Voltage 1500 -> 3.3V
6+
print(adcv)
77

8-
# read single sample
9-
val = adc.read()
10-
assert val < 500
8+
# read single sample; 2.5V-5V is pass range
9+
val = adcv.read()
10+
assert val > 1000 and val < 2000
1111

1212
# timer for read_timed
13-
tim = pyb.Timer(5, freq=500)
13+
tim = Timer(5, freq=500)
1414

1515
# read into bytearray
16-
buf = bytearray(50)
17-
adc.read_timed(buf, tim)
16+
buf = bytearray(b'\xff' * 50)
17+
adcv.read_timed(buf, tim)
1818
print(len(buf))
1919
for i in buf:
20-
assert i < 500
20+
assert i > 50 and i < 150
2121

2222
# read into arrays with different element sizes
2323
import array
24-
ar = array.array('h', 25 * [0])
25-
adc.read_timed(ar, tim)
26-
print(len(ar))
27-
for i in buf:
28-
assert i < 500
29-
ar = array.array('i', 30 * [0])
30-
adc.read_timed(ar, tim)
31-
print(len(ar))
32-
for i in buf:
33-
assert i < 500
24+
arv = array.array('h', 25 * [0x7fff])
25+
adcv.read_timed(arv, tim)
26+
print(len(arv))
27+
for i in arv:
28+
assert i > 1000 and i < 2000
29+
30+
arv = array.array('i', 30 * [-1])
31+
adcv.read_timed(arv, tim)
32+
print(len(arv))
33+
for i in arv:
34+
assert i > 1000 and i < 2000

tests/pyb/adc.py.exp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<ADC on X22 channel=13>
1+
<ADC on 16 channel=16>
2+
<ADC on 17 channel=17>
23
50
34
25
45
30

0 commit comments

Comments
 (0)