]>
vault307.fbx.one Git - ir_remote.git/blob - primitives/aadc.py
1 # aadc.py AADC (asynchronous ADC) class
3 # Copyright (c) 2020 Peter Hinch
4 # Released under the MIT License (MIT) - see LICENSE file
6 import uasyncio
as asyncio
9 MP_STREAM_POLL_RD
= const(1)
10 MP_STREAM_POLL
= const(3)
11 MP_STREAM_ERROR
= const(-1)
13 class AADC(io
.IOBase
):
14 def __init__(self
, adc
):
20 self
._sreader
= asyncio
.StreamReader(self
)
23 b
= yield from self
._sreader
.read(2)
24 return int.from_bytes(b
, 'little')
27 self
._last
= self
._adc
.read_u16()
30 def read(self
, n
): # For use by StreamReader only
31 return int.to_bytes(self
._last
, 2, 'little')
33 def ioctl(self
, req
, arg
):
35 if req
== MP_STREAM_POLL
:
37 if arg
& MP_STREAM_POLL_RD
:
38 if self
._pol ^
(self
._lower
<= self
._adcread
() <= self
._upper
):
39 ret |
= MP_STREAM_POLL_RD
44 # If normal will pause until ADC value is in range
45 # Otherwise will pause until value is out of range
46 def sense(self
, normal
):
49 def read_u16(self
, last
=False):
52 return self
._adcread
()
54 # Call syntax: set limits for trigger
55 # lower is None: leave limits unchanged.
56 # upper is None: treat lower as relative to current value.
57 # both have values: treat as absolute limits.
58 def __call__(self
, lower
=None, upper
=None):
60 if upper
is None: # Relative limit
61 r
= self
._adcread
() if self
._last
is None else self
._last
62 self
._lower
= r
- lower
63 self
._upper
= r
+ lower
64 else: # Absolute limits