From 0096a4bd005606489a5b7fdfda8e2a60a1709e13 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 11 Apr 2018 13:20:58 +1000 Subject: [PATCH] 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. --- tests/pyb/adc.py | 45 ++++++++++++++++++++++---------------------- tests/pyb/adc.py.exp | 3 ++- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/tests/pyb/adc.py b/tests/pyb/adc.py index 362ca326d2..7834c7520b 100644 --- a/tests/pyb/adc.py +++ b/tests/pyb/adc.py @@ -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 diff --git a/tests/pyb/adc.py.exp b/tests/pyb/adc.py.exp index 76f3914b03..1aae16fb01 100644 --- a/tests/pyb/adc.py.exp +++ b/tests/pyb/adc.py.exp @@ -1,4 +1,5 @@ - + + 50 25 30