A library for reading rotary encoders (ESP-8266 and Pyboard/stmhal)

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
User avatar
dhylands
Posts: 2494
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: A library for reading rotary encoders (ESP-8266 and Pyboard/stmhal)

Post by dhylands » Wed Jan 18, 2017 6:40 am

Interrupt callbacks can be class methods. This is a pyboard example, but see here:
https://github.com/dhylands/upy-example ... eat_irq.py
heartbeat_cb is an interrupt callback (and it's bound to the Heartbeat object created on line 19)

mattyt
Posts: 37
Joined: Mon Jan 23, 2017 6:39 am

Re: A library for reading rotary encoders (ESP-8266 and Pyboard/stmhal)

Post by mattyt » Wed May 24, 2017 1:22 pm

I just wanted to give a shoutout to SpotlightKid for submitting his Rotary Encoder library; I just cloned it, deployed it to my ESP8266 and it worked great! Admittedly not perfect debouncing but pretty decent and more than good enough for my use.

In the space of a night or so I now have a working(ish) LED strip light where the brightness is controlled by a rotary encoder - pushing it turns the strip on/off (with a fade). Less than 50 lines of hacky python code. Much appreciated @SpotlightKid!

evidlo
Posts: 5
Joined: Tue Jul 04, 2017 5:47 pm

Re: A library for reading rotary encoders (ESP-8266 and Pyboard/stmhal)

Post by evidlo » Fri Jul 07, 2017 3:51 am

[quote="pythoncoder"]Given the observed rates of contact bounce events and the simplicity of the ISR this strikes me as improbable and I didn't observe it in testing.[/quote]

I believe this is happening to me. I looked at one of the encoder channels on my scope and saw that my pulses are about 600μs wide.

[img]http://i.imgur.com/vB9ofHb.png[/img]

I measured the callback routine time in both pythoncoder and SpotlightKid's encoder classes and got around 800μs for both.

[code]
import time
from machine import Pin
from encoder import Encoder
enc = Encoder(Pin(4, Pin.IN), Pin(5, Pin.IN), False, 1)
a = time.ticks_us()
enc.x_callback(_)
b = time.ticks_us()
print(b - a)
[/code]

I was able to get around the issue by stepping up my clock rate to 160MHz, but I think encoder pulse widths that are too short may not be so uncommon.

User avatar
pythoncoder
Posts: 1960
Joined: Fri Jul 18, 2014 8:01 am

Re: A library for reading rotary encoders (ESP-8266 and Pyboard/stmhal)

Post by pythoncoder » Fri Jul 07, 2017 4:58 am

See my response here viewtopic.php?f=6&t=1735&start=10.

tl;dr The problem is almost certainly the dire interrupt latency on the ESP8266.
Peter Hinch

Post Reply