Skip to content

Commit 1cd2b3a

Browse files
committed
move i2c_mutex in script tools and adjust install scripts accordingly
1 parent d8dce52 commit 1cd2b3a

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

i2c_mutex.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fcntl
2+
import time
3+
4+
class Mutex(object):
5+
6+
DexterLockI2C_handle = None
7+
8+
def __init__(self, debug = False):
9+
self.mutex_debug = debug
10+
11+
def acquire(self):
12+
if self.mutex_debug:
13+
print("I2C mutex acquire")
14+
15+
acquired = False
16+
while not acquired:
17+
try:
18+
self.DexterLockI2C_handle = open('/run/lock/DexterLockI2C', 'w')
19+
# lock
20+
fcntl.lockf(self.DexterLockI2C_handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
21+
acquired = True
22+
except IOError: # already locked by a different process
23+
time.sleep(0.001)
24+
except Exception as e:
25+
print(e)
26+
if self.mutex_debug:
27+
print("I2C mutex acquired {}".format(time.time()))
28+
29+
30+
def release(self):
31+
if self.mutex_debug:
32+
print("I2C mutex release: {}".format(time.time()))
33+
if self.DexterLockI2C_handle is not None and self.DexterLockI2C_handle is not True:
34+
self.DexterLockI2C_handle.close()
35+
self.DexterLockI2C_handle = None
36+
time.sleep(0.001)
37+
38+
def enableDebug(self):
39+
self.mutex_debug = True
40+
41+
def disableDebug(self):
42+
self.mutex_debug = False
43+
44+
def __enter__(self):
45+
if self.mutex_debug:
46+
print("I2C mutex enter")
47+
return self
48+
49+
def __exit__(self, exception_type, exception_value, traceback):
50+
if self.mutex_debug:
51+
print("I2C mutex exit")
52+
self.release()

install_script_tools.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ else
4848
fi
4949

5050
cd $PIHOME/$DEXTER/$LIB/$DEXTER/$SCRIPT
51-
sudo python autodetect_setup.py install
51+
sudo python setup.py install
5252

5353
popd > /dev/null
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
description="Dexter Industries Robot Autodetection",
2828
author="Dexter Industries",
2929
url="http://www.dexterindustries.com/GoPiGo/",
30-
py_modules=['auto_detect_robot', 'auto_detect_rpi'],
30+
py_modules=['auto_detect_robot', 'auto_detect_rpi', 'i2c_mutex'],
3131
#install_requires=open('requirements.txt').readlines(),
3232
)

0 commit comments

Comments
 (0)