kopia lustrzana https://github.com/peterhinch/micropython-samples
42 wiersze
1.5 KiB
Python
42 wiersze
1.5 KiB
Python
# encoder_portable.py
|
|
|
|
# Encoder Support: this version should be portable between MicroPython platforms
|
|
# Thanks to Evan Widloski for the adaptation to use the machine module
|
|
|
|
# Copyright (c) 2017-2021 Peter Hinch
|
|
# Released under the MIT License (MIT) - see LICENSE file
|
|
|
|
from machine import Pin
|
|
|
|
class Encoder:
|
|
def __init__(self, pin_x, pin_y, scale=1):
|
|
self.scale = scale
|
|
self.forward = True
|
|
self.pin_x = pin_x
|
|
self.pin_y = pin_y
|
|
self._pos = 0
|
|
try:
|
|
self.x_interrupt = pin_x.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.x_callback, hard=True)
|
|
self.y_interrupt = pin_y.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.y_callback, hard=True)
|
|
except TypeError:
|
|
self.x_interrupt = pin_x.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.x_callback)
|
|
self.y_interrupt = pin_y.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.y_callback)
|
|
|
|
def x_callback(self, pin):
|
|
self.forward = pin() ^ self.pin_y()
|
|
self._pos += 1 if self.forward else -1
|
|
|
|
def y_callback(self, pin):
|
|
self.forward = self.pin_x() ^ pin() ^ 1
|
|
self._pos += 1 if self.forward else -1
|
|
|
|
def position(self, value=None):
|
|
if value is not None:
|
|
self._pos = round(value / self.scale) # # Improvement provided by @IhorNehrutsa
|
|
return self._pos * self.scale
|
|
|
|
def value(self, value=None):
|
|
if value is not None:
|
|
self._pos = value
|
|
return self._pos
|