peterhinch / micropython-micro-gui

A lightweight MicroPython GUI library for display drivers based on framebuf, allows input via pushbuttons. See also micropython-touch.
MIT License
269 stars 40 forks source link

Encoder not working as expected #46

Open alessandrv opened 8 months ago

alessandrv commented 8 months ago

Hello, love your libraries for micropython. I've been testing the encoder only mode for micro-gui but had a lot of issues with getting my encoders to work. I also tried using your encoder library from another repo to test printing the deltas to see if I was getting the right values and it looked like that, so I added a print for the delta also in the ugui.py file on the encoder callback and saw that the callback was not getting called for every dent I was turning (encoder set as 1) but a lot less of that, while the other encoder class of yours has the division ratio at 1 and register every dent perfectly.

Here's my hardware setup:

from machine import Pin, SoftI2C, freq
import gc
from ssd1306 import SSD1306_I2C as SSD
# Create and export an SSD instance
scl_pin = Pin(4)
sda_pin = Pin(0)

# Create an I2C object using the specified pins
i2c = SoftI2C(scl=scl_pin, sda=sda_pin)
gc.collect()  # Precaution before instantiating framebuf
ssd = SSD(128, 64, i2c)

gc.collect()
from gui.core.ugui import Display, quiet
# quiet()
# Create and export a Display instance
# Define control buttons
enc_clk = Pin(32, Pin.IN, Pin.PULL_UP)
enc_dt = Pin(33, Pin.IN, Pin.PULL_UP)
enc_btn = Pin(27, Pin.IN, Pin.PULL_UP)

display = Display(ssd, nxt=enc_clk, sel=enc_btn, prev=enc_dt, incr=False, encoder=1)

And here the simple script that uses the encoder class just to check the delta

def cb(pos, delta):
    print(pos, delta)

def prova():
    print('Running encoder test. Press ctrl-c to teminate.')
    enc = Encoder(px, py, v=0, vmin=None, vmax=None, callback=cb)

    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print('Interrupted')
    finally:
        asyncio.new_event_loop()

prova()

I the encoder behaved randomly in both cases I would think it's my encoder issue but doesn't look like that.

peterhinch commented 7 months ago

I'm not sure what I can do here. Your setup is correct, if it doesn't work I would suspect some kind of hardware issue.