Skip to content

Thread fix #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions arduino_alvik.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions examples/leds_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
sleep_ms(1000)
except KeyboardInterrupt as e:
print('over')
alvik.stop()
sys.exit()
4 changes: 2 additions & 2 deletions examples/message_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
except KeyboardInterrupt as e:
print('over')
alvik.stop()
break
sys.exit()
sys.exit()

4 changes: 2 additions & 2 deletions examples/read_color_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
sleep_ms(100)
except KeyboardInterrupt as e:
print('over')
break
sys.exit()
alvik.stop()
sys.exit()
4 changes: 2 additions & 2 deletions examples/read_touch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
sleep_ms(100)
except KeyboardInterrupt as e:
print('over')
break
sys.exit()
alvik.stop()
sys.exit()
4 changes: 2 additions & 2 deletions examples/set_pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
sleep_ms(100)
except KeyboardInterrupt as e:
print('over')
break
sys.exit()
alvik.stop()
sys.exit()