peterhinch / micropython-async

Application of uasyncio to hardware interfaces. Tutorial and code.
MIT License
742 stars 168 forks source link

Could I get some usage advice on Encoder please #127

Closed AndyThirtover closed 2 months ago

AndyThirtover commented 2 months ago

Dear Peter. I've got your encoder and test_encoder running by applying the correct pins for my board (double thanks). However in my own program I run into the issue of the encoder being "silent" or not triggering the callback.

Here's a snippet:

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

px = Pin(19, Pin.OUT, Pin.PULL_UP)
py = Pin(18, Pin.OUT, Pin.PULL_UP)

async def enc_main():
    while True:
        await asyncio.sleep(1)

counter1 = Counter6(tm1, "Counter50", ws_loop.actionQueue)
counter1.set_value(0)

my_loop = asyncio.get_event_loop()
encoder = Encoder(px, py, v=0, vmin=None, vmax=None, callback=ecb)
my_loop.create_task(enc_main())
my_loop.create_task(counter1.run())
my_loop.create_task(ws_loop.ws_read_loop())
my_loop.create_task(ws_loop.ws_send_loop())
my_loop.create_task(handle_actions())
my_loop.create_task(ws_loop.heartbeat())
my_loop.run_forever()

I can see in encoder you have: asyncio.create_task(self._run(vmin, vmax, div, mod, callback, args))

So I added:

if loop is None:
            asyncio.create_task(self._run(vmin, vmax, div, mod, callback, args))
        else:
            loop.create_task(self._run(vmin, vmax, div, mod, callback, args))

then tried passing in 'my_loop'.

I'm fairly sure that I have a misunderstanding somewhere - please might you point me to the error of my ways!

Double Regards

Andy