encoder_portable.py added.

pull/7/head
Peter Hinch 2017-07-07 07:00:56 +01:00
rodzic 5a03136d8d
commit 8bd0e9cbe9
2 zmienionych plików z 36 dodań i 0 usunięć

Wyświetl plik

@ -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 These were written for encoders producing TTL outputs. For switches, adapt the pull definition
to provide a pull up or pull down as required. 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 # A pseudo random number generator
On the Pyboard V1.1, true random numbers may be generated rapidly with pyb.rng() On the Pyboard V1.1, true random numbers may be generated rapidly with pyb.rng()

Wyświetl plik

@ -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