Skip to content

Commit f721920

Browse files
authored
Merge pull request #17 from CleoQc/master
Move I2C_mutex out of GoPiGo and GoPiGo3 and into here
2 parents 531fc63 + aeadfd4 commit f721920

3 files changed

Lines changed: 57 additions & 4 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fi
4949

5050
cd $PIHOME/$DEXTER/$LIB/$DEXTER/$SCRIPT
5151
sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev -y
52-
python autodetect_setup.py install
52+
python setup.py install
53+
python3 setup.py install
5354

5455
popd > /dev/null
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
'''
2424
import setuptools
2525
setuptools.setup(
26-
name="Dexter AutoDetection",
27-
description="Dexter Industries Robot Autodetection",
26+
name="Dexter AutoDetection and I2C Mutex",
27+
description="Dexter Industries Robot Autodetection and I2C Mutex Security",
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=['smbus-cffi', 'pyserial'],
3232
)

0 commit comments

Comments
 (0)