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.
This commit is contained in:
Damien George 2018-04-11 13:20:58 +10:00
parent de9528d12c
commit 0096a4bd00
2 changed files with 25 additions and 23 deletions

View File

@ -1,33 +1,34 @@
from pyb import ADC
from pyb import Pin
from pyb import ADC, Timer
pin = Pin('X22', mode=Pin.IN, pull=Pin.PULL_DOWN)
adc = ADC('X22')
print(adc)
adct = ADC(16) # Temperature 930 -> 20C
print(adct)
adcv = ADC(17) # Voltage 1500 -> 3.3V
print(adcv)
# read single sample
val = adc.read()
assert val < 500
# read single sample; 2.5V-5V is pass range
val = adcv.read()
assert val > 1000 and val < 2000
# timer for read_timed
tim = pyb.Timer(5, freq=500)
tim = Timer(5, freq=500)
# read into bytearray
buf = bytearray(50)
adc.read_timed(buf, tim)
buf = bytearray(b'\xff' * 50)
adcv.read_timed(buf, tim)
print(len(buf))
for i in buf:
assert i < 500
assert i > 50 and i < 150
# read into arrays with different element sizes
import array
ar = array.array('h', 25 * [0])
adc.read_timed(ar, tim)
print(len(ar))
for i in buf:
assert i < 500
ar = array.array('i', 30 * [0])
adc.read_timed(ar, tim)
print(len(ar))
for i in buf:
assert i < 500
arv = array.array('h', 25 * [0x7fff])
adcv.read_timed(arv, tim)
print(len(arv))
for i in arv:
assert i > 1000 and i < 2000
arv = array.array('i', 30 * [-1])
adcv.read_timed(arv, tim)
print(len(arv))
for i in arv:
assert i > 1000 and i < 2000

View File

@ -1,4 +1,5 @@
<ADC on X22 channel=13>
<ADC on 16 channel=16>
<ADC on 17 channel=17>
50
25
30