diff --git a/arduino_alvik.py b/arduino_alvik.py index eb59935..de670ae 100644 --- a/arduino_alvik.py +++ b/arduino_alvik.py @@ -13,6 +13,14 @@ 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) self.left_wheel = _ArduinoAlvikWheel(self.packeter, ord('L')) @@ -22,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 @@ -64,15 +70,18 @@ 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,)) - def _stop_update_thread(self): + 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,)) + + @classmethod + def _stop_update_thread(cls): """ Stops the background operations :return: """ - self._update_thread_running = False + cls._update_thread_running = False def stop(self): """ @@ -220,7 +229,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/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/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() + 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()