]> vault307.fbx.one Git - ir_remote.git/blob - primitives/tests/adctest.py
infrared remote
[ir_remote.git] / primitives / tests / adctest.py
1 # adctest.py
2
3 # Copyright (c) 2020 Peter Hinch
4 # Released under the MIT License (MIT) - see LICENSE file
5
6 import uasyncio as asyncio
7 from machine import ADC
8 import pyb
9 from primitives import AADC
10
11 async def signal(): # Could use write_timed but this prints values
12 dac = pyb.DAC(1, bits=12, buffering=True)
13 v = 0
14 while True:
15 if not v & 0xf:
16 print('write', v << 4) # Make value u16 as per ADC read
17 dac.write(v)
18 v += 1
19 v %= 4096
20 await asyncio.sleep_ms(50)
21
22 async def adctest():
23 asyncio.create_task(signal())
24 adc = AADC(ADC(pyb.Pin.board.X1))
25 await asyncio.sleep(0)
26 adc.sense(normal=False) # Wait until ADC gets to 5000
27 value = await adc(5000, 10000)
28 print('Received', value, adc.read_u16(True)) # Reduce to 12 bits
29 adc.sense(normal=True) # Now print all changes > 2000
30 while True:
31 value = await adc(2000) # Trigger if value changes by 2000
32 print('Received', value, adc.read_u16(True))
33
34 st = '''This test requires a Pyboard with pins X1 and X5 linked.
35 A sawtooth waveform is applied to the ADC. Initially the test waits
36 until the ADC value reaches 5000. It then reports whenever the value
37 changes by 2000.
38 Issue test() to start.
39 '''
40 print(st)
41
42 def test():
43 try:
44 asyncio.run(adctest())
45 except KeyboardInterrupt:
46 print('Interrupted')
47 finally:
48 asyncio.new_event_loop()
49 print()
50 print(st)