From b364630c5e66ccaea87aa29086f37d22b7f43a62 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 24 Jan 2024 16:52:25 +0100 Subject: [PATCH 1/4] fix: thread must be singleton --- arduino_alvik.py | 12 ++++++++---- examples/message_reader.py | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/arduino_alvik.py b/arduino_alvik.py index eb59935..795062d 100644 --- a/arduino_alvik.py +++ b/arduino_alvik.py @@ -13,6 +13,8 @@ class ArduinoAlvik: + _update_thread_running = False + def __init__(self): self.packeter = ucPack(200) self.left_wheel = _ArduinoAlvikWheel(self.packeter, ord('L')) @@ -64,15 +66,17 @@ def _begin_update_thread(self): Runs robot background operations (e.g. threaded update) :return: """ - self._update_thread_running = True - self._update_thread_id = _thread.start_new_thread(self._update, (1,)) + + if not ArduinoAlvik._update_thread_running: + ArduinoAlvik._update_thread_running = True + self._update_thread_id = _thread.start_new_thread(self._update, (1,)) def _stop_update_thread(self): """ Stops the background operations :return: """ - self._update_thread_running = False + ArduinoAlvik._update_thread_running = False def stop(self): """ @@ -220,7 +224,7 @@ def _update(self, delay_=1): :return: """ while True: - if not self._update_thread_running: + if not ArduinoAlvik._update_thread_running: break if self._read_message(): self._parse_message() diff --git a/examples/message_reader.py b/examples/message_reader.py index ac5b49b..dae58ef 100644 --- a/examples/message_reader.py +++ b/examples/message_reader.py @@ -23,5 +23,5 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - break -sys.exit() + sys.exit() + From 6d72cfa8eb1758938c2851aeb31da1d361ffa30f Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 24 Jan 2024 16:55:51 +0100 Subject: [PATCH 2/4] fix: _stop_update_thread as staticmethod --- arduino_alvik.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arduino_alvik.py b/arduino_alvik.py index 795062d..7cb1484 100644 --- a/arduino_alvik.py +++ b/arduino_alvik.py @@ -71,7 +71,8 @@ def _begin_update_thread(self): ArduinoAlvik._update_thread_running = True self._update_thread_id = _thread.start_new_thread(self._update, (1,)) - def _stop_update_thread(self): + @staticmethod + def _stop_update_thread(): """ Stops the background operations :return: From 16b86292dc18b9c0003ee2b79e0510cb3c41c6eb Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 24 Jan 2024 17:40:40 +0100 Subject: [PATCH 3/4] fix: some examples not calling alvik.stop() before sys.exit --- examples/leds_setting.py | 1 + examples/read_color_sensor.py | 4 ++-- examples/read_touch.py | 4 ++-- examples/set_pid.py | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/leds_setting.py b/examples/leds_setting.py index 07c5781..9c1a462 100644 --- a/examples/leds_setting.py +++ b/examples/leds_setting.py @@ -37,4 +37,5 @@ sleep_ms(1000) except KeyboardInterrupt as e: print('over') + alvik.stop() sys.exit() diff --git a/examples/read_color_sensor.py b/examples/read_color_sensor.py index 5bf597c..3fafa12 100644 --- a/examples/read_color_sensor.py +++ b/examples/read_color_sensor.py @@ -13,5 +13,5 @@ sleep_ms(100) except KeyboardInterrupt as e: print('over') - break -sys.exit() + alvik.stop() + sys.exit() diff --git a/examples/read_touch.py b/examples/read_touch.py index abc0d5c..9f7e183 100644 --- a/examples/read_touch.py +++ b/examples/read_touch.py @@ -27,5 +27,5 @@ sleep_ms(100) except KeyboardInterrupt as e: print('over') - break -sys.exit() + alvik.stop() + sys.exit() diff --git a/examples/set_pid.py b/examples/set_pid.py index ad480f3..f59f6f4 100644 --- a/examples/set_pid.py +++ b/examples/set_pid.py @@ -14,5 +14,5 @@ sleep_ms(100) except KeyboardInterrupt as e: print('over') - break -sys.exit() + alvik.stop() + sys.exit() From 2314969eaffaa623dd67aa615b24c80f1260e370 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 26 Jan 2024 14:59:39 +0100 Subject: [PATCH 4/4] feat: ArduinoAlvik as singleton class fix: _update_thread_id should be class param fix: removed instance vars _update_thread_running and _update_thread_id mod: _stop_update_thread is a classmethod --- arduino_alvik.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/arduino_alvik.py b/arduino_alvik.py index 7cb1484..de670ae 100644 --- a/arduino_alvik.py +++ b/arduino_alvik.py @@ -14,6 +14,12 @@ class ArduinoAlvik: _update_thread_running = False + _update_thread_id = None + + def __new__(cls): + if not hasattr(cls, 'instance'): + cls.instance = super(ArduinoAlvik, cls).__new__(cls) + return cls.instance def __init__(self): self.packeter = ucPack(200) @@ -24,8 +30,6 @@ def __init__(self): rgb_mask=[0b00000100, 0b00001000, 0b00010000]) self.right_led = _ArduinoAlvikRgbLed(self.packeter, 'right', self.led_state, rgb_mask=[0b00100000, 0b01000000, 0b10000000]) - self._update_thread_running = False - self._update_thread_id = None self.battery_perc = None self.touch_bits = None self.behaviour = None @@ -67,17 +71,17 @@ def _begin_update_thread(self): :return: """ - if not ArduinoAlvik._update_thread_running: - ArduinoAlvik._update_thread_running = True - self._update_thread_id = _thread.start_new_thread(self._update, (1,)) + if not self.__class__._update_thread_running: + self.__class__._update_thread_running = True + self.__class__._update_thread_id = _thread.start_new_thread(self._update, (1,)) - @staticmethod - def _stop_update_thread(): + @classmethod + def _stop_update_thread(cls): """ Stops the background operations :return: """ - ArduinoAlvik._update_thread_running = False + cls._update_thread_running = False def stop(self): """