Hi,
wondering if somebody already has written a routine for wipy to work with a rotary encoder
and likes to share it
Rotary encoder
Re: Rotary encoder
Here is my adapted version for wipy.
Original Code thanks to Christopher Arndt.
corrected typo thanks to pythoncoder
Original Code thanks to Christopher Arndt.
Code: Select all
# -*- coding: utf-8 -*-
"""MicroPython rotary encoder library."""
from machine import Pin
ENC_STATES = (0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0)
class Encoder(object):
def __init__(self, pin_x = 'GP4',pin_y = 'GP5', pin_mode=Pin.PULL_UP,
scale=1,min=0, max=100, reverse=False):
self.pin_x = (pin_x if isinstance(pin_x, Pin) else
Pin(pin_x, mode = Pin.IN, pull = pin_mode))
self.pin_y = (pin_y if isinstance(pin_y, Pin) else
Pin(pin_y, mode = Pin.IN, pull = pin_mode))
self.pin_mode = pin_mode
self.scale = scale
self.min = min
self.max = max
self.reverse = 1 if reverse else -1
# The following variables are assigned to in the interrupt callback,
# so we have to allocate them here.
self._pos = -1
self._readings = 0
self._state = 0
self.set_callbacks(self._callback)
def _callback(self, line):
self._readings = (self._readings << 2 | self.pin_x.value() << 1 |
self.pin_y.value()) & 0x0f
self._state = ENC_STATES[self._readings] * self.reverse
if self._state:
self._pos = min(max(self.min, self._pos + self._state), self.max)
def set_callbacks(self, callback=None):
self.irq_x = self.pin_x.irq(trigger = Pin.IRQ_FALLING | Pin.IRQ_RISING, handler = callback)
self.irq_y = self.pin_y.irq(trigger = Pin.IRQ_FALLING | Pin.IRQ_RISING, handler = callback)
@property
def position(self):
return self._pos * self.scale
def reset(self):
self._pos = 0
Last edited by nui_de on Tue Mar 29, 2016 3:48 pm, edited 2 times in total.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Rotary encoder
Before Encoder Mode was supported on the Pyboard I posted some code here http://forum.micropython.org/viewtopic. ... =368#p1999 which could easily be adapted for the WiPy. The advantage is that it uses every transition on the X and Y signals and therefore offers higher resolution.
Incidentally I think your Pin.IRQ_RAISING is a typo.
Incidentally I think your Pin.IRQ_RAISING is a typo.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Rotary encoder
Pin.IRQ_RISING_FALLING is not supported by the wipy - so I used Pin.IRQ_RAISING
or what should it be?
or what should it be?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Rotary encoder
The docs say "The values can be ORed together, for instance mode=Pin.IRQ_FALLING | Pin.IRQ_RISING" - see http://docs.micropython.org/en/latest/w ... e.Pin.html. This indicates that you can interrupt on every edge. Note that, according to the docs, it's IRQ_RISING not IRQ_RAISING.
I'm basing this on reading the friendly manual - I haven't actually tried it on the WiPy.
I'm basing this on reading the friendly manual - I haven't actually tried it on the WiPy.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Rotary encoder
yep - thank you, I have read the manual but missed that statement and of course its "RISING"
Looked fine to me as I wrote it down and I got no Error with "RAISING".
Re: Rotary encoder
This is an example of why post editing is to be prohibited in some cases. After you altering your typo in the initial post, Pythoncoders response seems to be stupid. Please at least comment your edit accordingly!nui_de wrote:
yep - thank you, I have read the manual but missed that statement and of course its "RISING"
Looked fine to me as I wrote it down and I got no Error with "RAISING".
Re: Rotary encoder
I don't think that the respons seems to be stupid after you read my reply - the fact that there was a typo.
When you see any source in a posting you don't want to read the complete tread (okay, easy in this case)
to find out that this code does not work - just my opinion.
However I added a line in my posting to clearify that I corrected the mistake.
When you see any source in a posting you don't want to read the complete tread (okay, easy in this case)
to find out that this code does not work - just my opinion.
However I added a line in my posting to clearify that I corrected the mistake.