Skip to content

Commit 2314969

Browse files
committed
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
1 parent 16b8629 commit 2314969

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

arduino_alvik.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
class ArduinoAlvik:
1515

1616
_update_thread_running = False
17+
_update_thread_id = None
18+
19+
def __new__(cls):
20+
if not hasattr(cls, 'instance'):
21+
cls.instance = super(ArduinoAlvik, cls).__new__(cls)
22+
return cls.instance
1723

1824
def __init__(self):
1925
self.packeter = ucPack(200)
@@ -24,8 +30,6 @@ def __init__(self):
2430
rgb_mask=[0b00000100, 0b00001000, 0b00010000])
2531
self.right_led = _ArduinoAlvikRgbLed(self.packeter, 'right', self.led_state,
2632
rgb_mask=[0b00100000, 0b01000000, 0b10000000])
27-
self._update_thread_running = False
28-
self._update_thread_id = None
2933
self.battery_perc = None
3034
self.touch_bits = None
3135
self.behaviour = None
@@ -67,17 +71,17 @@ def _begin_update_thread(self):
6771
:return:
6872
"""
6973

70-
if not ArduinoAlvik._update_thread_running:
71-
ArduinoAlvik._update_thread_running = True
72-
self._update_thread_id = _thread.start_new_thread(self._update, (1,))
74+
if not self.__class__._update_thread_running:
75+
self.__class__._update_thread_running = True
76+
self.__class__._update_thread_id = _thread.start_new_thread(self._update, (1,))
7377

74-
@staticmethod
75-
def _stop_update_thread():
78+
@classmethod
79+
def _stop_update_thread(cls):
7680
"""
7781
Stops the background operations
7882
:return:
7983
"""
80-
ArduinoAlvik._update_thread_running = False
84+
cls._update_thread_running = False
8185

8286
def stop(self):
8387
"""

0 commit comments

Comments
 (0)