diff --git a/tools/odrive/dfu.py b/tools/odrive/dfu.py index aad9a3d8..339d2554 100755 --- a/tools/odrive/dfu.py +++ b/tools/odrive/dfu.py @@ -13,6 +13,7 @@ import array import fractions import usb.core import odrive.discovery +from odrive.utils import Event from odrive.dfuse import * try: @@ -244,8 +245,7 @@ def launch_dfu(args, app_shutdown_token): serial_number = args.serial_number - find_odrive_cancellation_token = threading.Event() - app_shutdown_token.subscribe(lambda: find_odrive_cancellation_token.set()) + find_odrive_cancellation_token = Event(app_shutdown_token) print("Waiting for ODrive...") diff --git a/tools/odrive/utils.py b/tools/odrive/utils.py index 5834ff85..efc15acb 100755 --- a/tools/odrive/utils.py +++ b/tools/odrive/utils.py @@ -9,6 +9,7 @@ import threading import platform import subprocess import os +from odrive.utils import Event try: if platform.system() == 'Windows': @@ -33,7 +34,7 @@ def start_liveplotter(get_var_callback): import matplotlib.pyplot as plt - cancellation_token = threading.Event() + cancellation_token = Event() global vals vals = [] @@ -175,7 +176,7 @@ class Event(): handler() finally: self._mutex.release() - return lambda: self.unsubscribe(handler) + return handler def unsubscribe(self, handler): self._mutex.acquire() @@ -201,16 +202,16 @@ class Event(): def wait_any(*events, timeout=None): """ Blocks until any of the specified events are triggered. - Returns the number of the event that was triggerd or raises + Returns the index of the event that was triggerd or raises a TimeoutException """ or_event = threading.Event() - unsubscribe_functions = [] + subscriptions = [] for event in events: - unsubscribe_functions.append(event.subscribe(lambda: or_event.set())) + subscriptions.append((event, event.subscribe(lambda: or_event.set()))) or_event.wait(timeout=timeout) - for unsubscribe_function in unsubscribe_functions: - unsubscribe_function() + for event, sub in subscriptions: + event.unsubscribe(sub) for i in range(len(events)): if events[i].is_set(): return i