1from pyb import ADC, Timer
2
3adct = ADC(16)  # Temperature 930 -> 20C
4print(str(adct)[:19])
5adcv = ADC(17)  # Voltage 1500 -> 3.3V
6print(adcv)
7
8# read single sample; 2.5V-5V is pass range
9val = adcv.read()
10assert val > 1000 and val < 2000
11
12# timer for read_timed
13tim = Timer(5, freq=500)
14
15# read into bytearray
16buf = bytearray(b"\xff" * 50)
17adcv.read_timed(buf, tim)
18print(len(buf))
19for i in buf:
20    assert i > 50 and i < 150
21
22# read into arrays with different element sizes
23import array
24
25arv = array.array("h", 25 * [0x7FFF])
26adcv.read_timed(arv, tim)
27print(len(arv))
28for i in arv:
29    assert i > 1000 and i < 2000
30
31arv = array.array("i", 30 * [-1])
32adcv.read_timed(arv, tim)
33print(len(arv))
34for i in arv:
35    assert i > 1000 and i < 2000
36
37# Test read_timed_multi
38arv = bytearray(b"\xff" * 50)
39art = bytearray(b"\xff" * 50)
40ADC.read_timed_multi((adcv, adct), (arv, art), tim)
41for i in arv:
42    assert i > 60 and i < 125
43# Wide range: unsure of accuracy of temp sensor.
44for i in art:
45    assert i > 15 and i < 200
46
47arv = array.array("i", 25 * [-1])
48art = array.array("i", 25 * [-1])
49ADC.read_timed_multi((adcv, adct), (arv, art), tim)
50for i in arv:
51    assert i > 1000 and i < 2000
52# Wide range: unsure of accuracy of temp sensor.
53for i in art:
54    assert i > 50 and i < 2000
55
56arv = array.array("h", 25 * [0x7FFF])
57art = array.array("h", 25 * [0x7FFF])
58ADC.read_timed_multi((adcv, adct), (arv, art), tim)
59for i in arv:
60    assert i > 1000 and i < 2000
61# Wide range: unsure of accuracy of temp sensor.
62for i in art:
63    assert i > 50 and i < 2000
64