kopia lustrzana https://github.com/peterhinch/micropython-micro-gui
encoder.py: Simplify.
rodzic
e6d82c7dcb
commit
b1a45c2e82
|
@ -11,7 +11,6 @@
|
||||||
|
|
||||||
import uasyncio as asyncio
|
import uasyncio as asyncio
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
from . import Delay_ms
|
|
||||||
|
|
||||||
|
|
||||||
class Encoder:
|
class Encoder:
|
||||||
|
@ -34,7 +33,7 @@ class Encoder:
|
||||||
self._y = pin_y()
|
self._y = pin_y()
|
||||||
self._v = v * div # Initialise hardware value
|
self._v = v * div # Initialise hardware value
|
||||||
self._cv = v # Current (divided) value
|
self._cv = v # Current (divided) value
|
||||||
self._delay = Delay_ms(duration=delay)
|
self._delay = delay # Time when motion is judged to have stopped
|
||||||
self._timeout = 2 * delay # Continuous rotation timeout
|
self._timeout = 2 * delay # Continuous rotation timeout
|
||||||
# Pause (ms) for motion to stop/limit callback frequency
|
# Pause (ms) for motion to stop/limit callback frequency
|
||||||
|
|
||||||
|
@ -65,23 +64,24 @@ class Encoder:
|
||||||
self._v -= 1 if y ^ self._pin_x() else -1
|
self._v -= 1 if y ^ self._pin_x() else -1
|
||||||
self._tsf.set()
|
self._tsf.set()
|
||||||
|
|
||||||
async def respond(self): # Retrigger the delay until motion/bounce stops
|
async def has_stopped(self): # Encoder has moved
|
||||||
while True:
|
while True: # Return when it has stopped
|
||||||
await self._tsf.wait()
|
v = self._v # Get current position
|
||||||
self._delay.trigger()
|
await asyncio.sleep_ms(self._delay)
|
||||||
|
if v == self._v: # Motion has stopped
|
||||||
|
return
|
||||||
|
|
||||||
async def _run(self, vmin, vmax, div, mod, cb, args):
|
async def _run(self, vmin, vmax, div, mod, cb, args):
|
||||||
pv = self._v # Prior hardware value
|
pv = self._v # Prior hardware value
|
||||||
pcv = self._cv # Prior divided value passed to callback
|
pcv = self._cv # Prior divided value passed to callback
|
||||||
lcv = pcv # Current value after limits applied
|
lcv = pcv # Current value after limits applied
|
||||||
plcv = pcv # Previous value after limits applied
|
plcv = pcv # Previous value after limits applied
|
||||||
self._rspt = asyncio.create_task(self.respond())
|
|
||||||
while True:
|
while True:
|
||||||
|
await self._tsf.wait() # A stationary encoder uses minimal resources
|
||||||
try:
|
try:
|
||||||
await asyncio.wait_for_ms(self._delay.wait(), self._timeout)
|
await asyncio.wait_for_ms(self.has_stopped(), self._timeout)
|
||||||
except asyncio.TimeoutError: # Continuous rotation
|
except asyncio.TimeoutError: # Continuous rotation
|
||||||
pass
|
pass
|
||||||
self._delay.clear()
|
|
||||||
hv = self._v # Sample hardware (atomic read).
|
hv = self._v # Sample hardware (atomic read).
|
||||||
if hv == pv: # A change happened but was negated before
|
if hv == pv: # A change happened but was negated before
|
||||||
continue # this got scheduled. Nothing to do.
|
continue # this got scheduled. Nothing to do.
|
||||||
|
@ -103,5 +103,4 @@ class Encoder:
|
||||||
return self._cv
|
return self._cv
|
||||||
|
|
||||||
def deinit(self):
|
def deinit(self):
|
||||||
self._rspt.cancel()
|
|
||||||
self._runt.cancel()
|
self._runt.cancel()
|
||||||
|
|
Ładowanie…
Reference in New Issue