@@ -32,14 +32,55 @@ def wrapper(*args, **kwargs):
32
32
return wrapper
33
33
34
34
35
+ class _AlvikRLock :
36
+ def __init__ (self ):
37
+ """Alvik re-entrant Lock implementation"""
38
+ self ._lock = _thread .allocate_lock ()
39
+ self ._owner = None
40
+ self ._count = 0
41
+
42
+ def acquire (self ):
43
+ tid = _thread .get_ident ()
44
+
45
+ if self ._owner == tid :
46
+ self ._count += 1
47
+ return True
48
+
49
+ self ._lock .acquire ()
50
+ self ._owner = tid
51
+ self ._count = 1
52
+ return True
53
+
54
+ def release (self ):
55
+ tid = _thread .get_ident ()
56
+
57
+ if self ._owner != tid :
58
+ raise RuntimeError ("Cannot release an unowned lock" )
59
+
60
+ self ._count -= 1
61
+ if self ._count == 0 :
62
+ self ._owner = None
63
+ self ._lock .release ()
64
+
65
+ def locked (self ):
66
+ return self ._lock .locked ()
67
+
68
+ def __enter__ (self ):
69
+ self .acquire ()
70
+ return self
71
+
72
+ def __exit__ (self , exc_type , exc_value , traceback ):
73
+ self .release ()
74
+
75
+
35
76
class ArduinoAlvik :
36
77
_update_thread_running = False
37
78
_update_thread_id = None
38
79
_events_thread_running = False
39
80
_events_thread_id = None
40
81
41
- _write_lock = _thread . allocate_lock ()
42
- _read_lock = _thread . allocate_lock ()
82
+ _write_lock = _AlvikRLock ()
83
+ _read_lock = _AlvikRLock ()
43
84
44
85
def __new__ (cls ):
45
86
if not hasattr (cls , '_instance' ):
@@ -189,13 +230,13 @@ def _idle(self, delay_=1, check_on_thread=False, blocking=False) -> None:
189
230
led_val = (led_val + 1 ) % 2
190
231
self .i2c .set_single_thread (False )
191
232
if self .is_on ():
192
- print ("********** Alvik is on **********" )
233
+ print ("\n ********** Alvik is on **********" )
193
234
except KeyboardInterrupt :
194
235
self .stop ()
195
236
sys .exit ()
196
237
except Exception as e :
197
238
pass
198
- print (f'Unable to read SOC: { e } ' )
239
+ print (f'\n Unable to read SOC: { e } ' )
199
240
finally :
200
241
LEDR .value (1 )
201
242
LEDG .value (1 )
@@ -252,7 +293,7 @@ def begin(self) -> int:
252
293
self .set_behaviour (2 )
253
294
self ._set_color_reference ()
254
295
if self ._has_events_registered ():
255
- print ('Starting events thread' )
296
+ print ('\n ********** Starting events thread ********** \n ' )
256
297
self ._start_events_thread ()
257
298
self .set_servo_positions (90 , 90 )
258
299
return 0
0 commit comments