summaryrefslogtreecommitdiff
path: root/tests/pyb/adc.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-04-11 13:20:58 +1000
committerDamien George <damien.p.george@gmail.com>2018-04-11 13:21:57 +1000
commit0096a4bd005606489a5b7fdfda8e2a60a1709e13 (patch)
tree35c0f8a42db03255261bc58e164c0644269ee403 /tests/pyb/adc.py
parentde9528d12c73d658406d1b11ff44d7a39596af01 (diff)
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.
Diffstat (limited to 'tests/pyb/adc.py')
-rw-r--r--tests/pyb/adc.py45
1 files changed, 23 insertions, 22 deletions
diff --git a/tests/pyb/adc.py b/tests/pyb/adc.py
index 362ca326d..7834c7520 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