13
13
14
14
class ArduinoAlvik :
15
15
16
+ _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
23
+
16
24
def __init__ (self ):
17
25
self .packeter = ucPack (200 )
18
26
self .left_wheel = _ArduinoAlvikWheel (self .packeter , ord ('L' ))
@@ -22,8 +30,6 @@ def __init__(self):
22
30
rgb_mask = [0b00000100 , 0b00001000 , 0b00010000 ])
23
31
self .right_led = _ArduinoAlvikRgbLed (self .packeter , 'right' , self .led_state ,
24
32
rgb_mask = [0b00100000 , 0b01000000 , 0b10000000 ])
25
- self ._update_thread_running = False
26
- self ._update_thread_id = None
27
33
self .battery_perc = None
28
34
self .touch_bits = None
29
35
self .behaviour = None
@@ -64,15 +70,18 @@ def _begin_update_thread(self):
64
70
Runs robot background operations (e.g. threaded update)
65
71
:return:
66
72
"""
67
- self ._update_thread_running = True
68
- self ._update_thread_id = _thread .start_new_thread (self ._update , (1 ,))
69
73
70
- def _stop_update_thread (self ):
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 ,))
77
+
78
+ @classmethod
79
+ def _stop_update_thread (cls ):
71
80
"""
72
81
Stops the background operations
73
82
:return:
74
83
"""
75
- self ._update_thread_running = False
84
+ cls ._update_thread_running = False
76
85
77
86
def stop (self ):
78
87
"""
@@ -220,7 +229,7 @@ def _update(self, delay_=1):
220
229
:return:
221
230
"""
222
231
while True :
223
- if not self ._update_thread_running :
232
+ if not ArduinoAlvik ._update_thread_running :
224
233
break
225
234
if self ._read_message ():
226
235
self ._parse_message ()
0 commit comments