From a8c3a697cd5712823b8c96d32e306f5f4d66b4f7 Mon Sep 17 00:00:00 2001
From: kruger <kruger@astron.nl>
Date: Mon, 1 Mar 2021 14:57:30 +0000
Subject: [PATCH] I2C using python periphery

---
 i2c/I2Cv2.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 i2c/I2Cv2.py

diff --git a/i2c/I2Cv2.py b/i2c/I2Cv2.py
new file mode 100644
index 0000000..81cbb13
--- /dev/null
+++ b/i2c/I2Cv2.py
@@ -0,0 +1,63 @@
+#import pylibi2c;
+#import 
+from periphery import I2C;
+
+import time
+import logging
+#bus = pylibi2c.I2CDevice('/dev/i2c-1'
+#read=0: write to register
+#read=1: read from register
+#read=2: write to register (common in group)
+#read=3: wait ms second
+I2Ccounter=0;
+
+
+i2c1=I2C("/dev/i2c-1")
+
+def I2C1server(addr,data,reg=None,read=0):
+#  print("I2C",addr,reg,data,read)
+  try:
+       if read==3:
+           time.sleep(data[0]/1000.)
+           return True
+       logging.debug(str(("I2C",addr,reg,data,read)))
+
+       if read==1:
+          if not(reg is None):  i2c1.transfer(addr,[I2C.Message(reg)])
+          msgs=[I2C.Message(data,read=True)]
+          i2c1.transfer(addr,msgs)
+          data[:]=msgs[0].data
+       else:
+            if reg is None: 
+               msgs=[I2C.Message(data)]
+            else:
+               msgs=[I2C.Message(reg),I2C.Message(data)]
+            i2c1.transfer(addr,msgs)
+       return True;
+  except:
+       return False;
+
+i2c4=I2C("/dev/i2c-1")
+
+def I2C1server(addr,data,reg=None,read=0):
+#  print("I2C",addr,reg,data,read)
+  try:
+       if read==3:
+           time.sleep(data[0]/1000.)
+           return True
+       logging.debug(str(("I2C",addr,reg,data,read)))
+
+       if read==1:
+          if not(reg is None):  i2c4.transfer(addr,[I2C.Message(reg)])
+          msgs=[I2C.Message(data,read=True)]
+          i2c4.transfer(addr,msgs)
+          data[:]=msgs[0].data
+       else:
+            if reg is None: 
+               msgs=[I2C.Message(data)]
+            else:
+               msgs=[I2C.Message(reg),I2C.Message(data)]
+            i2c4.transfer(addr,msgs)
+       return True;
+  except:
+       return False;
-- 
GitLab