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.
pull/3713/head
Damien George 2018-04-11 13:20:58 +10:00
rodzic de9528d12c
commit 0096a4bd00
2 zmienionych plików z 25 dodań i 23 usunięć

Wyświetl plik

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

Wyświetl plik

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