Rotary encoder

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
nui_de
Posts: 40
Joined: Fri Oct 23, 2015 3:27 pm

Rotary encoder

Post by nui_de » Sat Mar 26, 2016 1:59 pm

Hi,

wondering if somebody already has written a routine for wipy to work with a rotary encoder
and likes to share it ;)

nui_de
Posts: 40
Joined: Fri Oct 23, 2015 3:27 pm

Re: Rotary encoder

Post by nui_de » Mon Mar 28, 2016 1:20 pm

Here is my adapted version for wipy.
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
 
corrected typo thanks to pythoncoder
Last edited by nui_de on Tue Mar 29, 2016 3:48 pm, edited 2 times in total.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Rotary encoder

Post by pythoncoder » Tue Mar 29, 2016 7:11 am

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.
Peter Hinch
Index to my micropython libraries.

nui_de
Posts: 40
Joined: Fri Oct 23, 2015 3:27 pm

Re: Rotary encoder

Post by nui_de » Tue Mar 29, 2016 8:22 am

Pin.IRQ_RISING_FALLING is not supported by the wipy - so I used Pin.IRQ_RAISING
or what should it be?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Rotary encoder

Post by pythoncoder » Tue Mar 29, 2016 2:07 pm

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.
Peter Hinch
Index to my micropython libraries.

nui_de
Posts: 40
Joined: Fri Oct 23, 2015 3:27 pm

Re: Rotary encoder

Post by nui_de » Tue Mar 29, 2016 2:21 pm

:oops:
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".

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: Rotary encoder

Post by kfricke » Tue Mar 29, 2016 3:02 pm

nui_de wrote::oops:
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".
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
Posts: 40
Joined: Fri Oct 23, 2015 3:27 pm

Re: Rotary encoder

Post by nui_de » Tue Mar 29, 2016 3:55 pm

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.

Post Reply