Skip to content

Calling hub methods from callback functions blocks further operations #127

Description

@Yakuza-UA

So, I tried to implement a simple demo where press of the button controls some other functions of a MoveHub, i.e. changes LED colour, or spins up a motor. The code looks like this:

from pylgbst.hub import MoveHub
from pylgbst import get_connection_bleak

conn = get_connection_bleak()
hub = MoveHub(conn)

def callback_btn(is_pressed):
    global hub
    print("Btn pressed: %s" % is_pressed)

    hub.motor_external.wait_complete()
    if is_pressed == 0:
        hub.motor_external.timed(5, 1.0, wait_complete=False)

try:
    hub.button.subscribe(callback_btn)
    time.sleep(60)
finally:
    hub.button.unsubscribe(callback_btn)
    hub.disconnect()

Once I release the button for the first time, it spins up the motor for 5 seconds exactly as I expect it to do. However, it does seem to ignore any further button presses... callback function is not called at all as I am not even getting any strings printed. Is this an expected behaviour due to threaded execution?

Is there a way to implement this?

Thanks a lot in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions