kopia lustrzana https://github.com/peterhinch/micropython-samples
encoder_portable.py added.
rodzic
5a03136d8d
commit
8bd0e9cbe9
|
@ -141,6 +141,10 @@ There are other algorithms but this is the simplest and fastest I've encountered
|
|||
These were written for encoders producing TTL outputs. For switches, adapt the pull definition
|
||||
to provide a pull up or pull down as required.
|
||||
|
||||
The `encoder.portable.py` version should work on all MicroPython platforms.
|
||||
Tested on ESP8266. Note that interrupt latency on the ESP8266 limits performance
|
||||
(ESP32 is probably similar).
|
||||
|
||||
# A pseudo random number generator
|
||||
|
||||
On the Pyboard V1.1, true random numbers may be generated rapidly with pyb.rng()
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
# Encoder Support: this version should be portable between MicroPython platforms
|
||||
# Thanks to Evan Widloski for the adaptation to the machine module
|
||||
|
||||
from machine import Pin
|
||||
|
||||
class Encoder(object):
|
||||
def __init__(self, pin_x, pin_y, reverse, scale):
|
||||
self.reverse = reverse
|
||||
self.scale = scale
|
||||
self.forward = True
|
||||
self.pin_x = pin_x
|
||||
self.pin_y = pin_y
|
||||
self._pos = 0
|
||||
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, line):
|
||||
self.forward = self.pin_x.value() ^ self.pin_y.value() ^ self.reverse
|
||||
self._pos += 1 if self.forward else -1
|
||||
|
||||
def y_callback(self, line):
|
||||
self.forward = self.pin_x.value() ^ self.pin_y.value() ^ self.reverse ^ 1
|
||||
self._pos += 1 if self.forward else -1
|
||||
|
||||
@property
|
||||
def position(self):
|
||||
return self._pos * self.scale
|
||||
|
||||
@position.setter
|
||||
def position(self, value):
|
||||
self._pos = value // self.scale
|
||||
|
Ładowanie…
Reference in New Issue