From e6c3cb636670deae5784771b894b8475d04998a2 Mon Sep 17 00:00:00 2001
From: Gijs <schoonderbeek@astron.nl>
Date: Wed, 3 Jan 2024 17:34:14 +0100
Subject: [PATCH] Added I2C_serial_iss.py and modified apspu_lib.py to be used
 for the production test setup. We might have to update the apspu_lib.py again
 for the final test testup.

---
 I2C_serial_iss.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++
 apspu_lib.py      | 36 +++++++++++++++++------------
 2 files changed, 80 insertions(+), 15 deletions(-)
 create mode 100644 I2C_serial_iss.py

diff --git a/I2C_serial_iss.py b/I2C_serial_iss.py
new file mode 100644
index 0000000..281d24c
--- /dev/null
+++ b/I2C_serial_iss.py
@@ -0,0 +1,59 @@
+"""
+I2C Class
+
+"""
+from usb_iss import UsbIss, defs
+from time import *
+import sys
+sys.path.insert(0, 'c:\python34\lib\site-packages')
+
+DEBUG = False
+port = 'COM3'
+iss = UsbIss()
+try:
+    iss.open(port)
+    iss.setup_i2c(400, True, None, None)
+except:
+    print("already in use")
+
+
+class I2C:
+
+    def __init__(self, address=0x40):
+        self.I2C_Address = address
+        self.BUS_NR = 1    # not used for Laptop but to enable Pi multiple busses.
+
+    def read_bytes(self, register, bytes_to_read=2):
+        ret_value = ''
+        read_values = iss.i2c.read(self.I2C_Address, register, bytes_to_read)
+        for x in read_values:
+            ret_value += hex(x)[2:]
+        ret_ack = True
+        ret_value = ret_value #.decode("utf-8")
+        return ret_ack, ret_value
+
+    def read_last_reg(self, bytes_to_read):
+        ret_value = []
+        for cnt in range(bytes_to_read):
+            ret_value += hex(iss.i2c.read_single(self.I2C_Address))[2:]
+        ret_value = ret_value
+        ret_ack = 1
+        return ret_ack, ret_value
+  
+    def write_bytes(self, register, data):
+        if len([data]) < 2:
+            data = [data]
+        iss.i2c.write(self.I2C_Address, register, data)
+        ret_ack = 1
+        return ret_ack
+
+    def write_pointer(self, register):
+        iss.i2c.write_single(self.I2C_Address, register)
+        return ret_ack
+
+
+if __name__ == "__main__":
+    I2C_Device = I2C(0x40)
+    I2C_Device.write_bytes(0x00, 0x00)
+    ret_ack, ret_value = I2C_Device.read_bytes(0x8C, 2)
+    print(ret_value)
diff --git a/apspu_lib.py b/apspu_lib.py
index be387ca..7d4fa82 100644
--- a/apspu_lib.py
+++ b/apspu_lib.py
@@ -24,8 +24,10 @@ import math
 from APSPU_I2C import *
 
 
-if os.name == "posix":
+if 0: #os.name == "posix":
     from I2C_serial_pi3 import *
+elif 1:
+    from I2C_serial_iss import *
 else:
     from I2C_serial import *
 
@@ -515,8 +517,12 @@ class FanmonitorClass:
             if DEBUG:
                 stri = "Device {0} at address 0x{1:X} is found ".format("MAX6620", MAX6620)
                 print(stri)
-            self.set_active()
-            self.status = True
+            try:
+                self.set_active()
+            except:
+                self.status = False
+            else:
+                self.status = True
 
     def set_active(self):
         #
@@ -595,20 +601,20 @@ def main():
     # Function to test the class, read all info and dump on the screen
     #
     apspu = ApspuClass()
-    apspu.apspu_on_off(False)
-    sleep(5)
-    apspu.set_pols()
-    apspu.apspu_on_off(True)
-    sleep(10)
+#    apspu.apspu_on_off(False)
+#    sleep(5)
+#    apspu.set_pols()
+#    apspu.apspu_on_off(True)
+#    sleep(10)
+#    apspu.read_all()
+#    apspu.print_status()
+#    apspu.check_apspu()
+#    apspu.apspu_on_off(False)
+#    sleep(10)
     apspu.read_all()
     apspu.print_status()
-    apspu.check_apspu()
-    apspu.apspu_on_off(False)
-    sleep(10)
-    apspu.read_all()
-    apspu.print_status()
-    apspu.apspu_on_off(True)
-
+#   apspu.apspu_on_off(True)
+#    apspu.eeprom.wr_rd_eeprom(value="APSPU-2", address=0)
 
 if __name__ == "__main__":
     main()
-- 
GitLab