From 525bccc6a192070e6a8847b527a67bea5c752960 Mon Sep 17 00:00:00 2001
From: Gijs Schoonderbeek <schoonderbeek@astron.nl>
Date: Tue, 6 Dec 2022 18:20:22 +0100
Subject: [PATCH] Moved EEPROM to separate class

---
 apspu_lib.py        | 89 +++++++++++++++++++++++++--------------------
 production_apspu.py |  4 +-
 2 files changed, 51 insertions(+), 42 deletions(-)

diff --git a/apspu_lib.py b/apspu_lib.py
index 2113ff0..3bc4afa 100644
--- a/apspu_lib.py
+++ b/apspu_lib.py
@@ -41,47 +41,8 @@ class ApspuClass:
         self.pols = []
         for pol in list(CTR_POLS.keys()):
             self.pols.append(PolClass(pol))
-        self.dev_i2c_eeprom = I2C(EEPROM)
-        self.dev_i2c_eeprom.bus_nr = I2CBUSNR
         self.fans = FanmonitorClass()
-
-    def write_eeprom(self, data="APSPU", address=0):
-        #
-        # Write the EEPROM with the serial number etc.
-        #
-        ret_ack, ret_value = self.dev_i2c_eeprom.read_bytes(0)
-        if ret_ack < 1:
-            print("EEPROM not found during write")
-            return False
-        else:
-            wr_data = bytearray(data.encode("utf-8", errors="ignore"))
-            for loc, data_byte in enumerate(wr_data):
-                self.dev_i2c_eeprom.write_bytes(address+loc, data_byte)
-                sleep(0.1)
-            return True
-
-    def read_eeprom(self, address=0, nof_bytes=5):
-        #
-        # Read the EEPROM with the serial number etc.
-        #
-        ret_ack, ret_value = self.dev_i2c_eeprom.read_last_reg(1)
-        if ret_ack < 1:
-            print("no EEPROM found during read")
-            return False
-        else:
-            ret_ack, ret_value = self.dev_i2c_eeprom.read_bytes(address, nof_bytes)
-            str_return = bytes.fromhex(ret_value[:nof_bytes*2]).decode('UTF-8')
-            return str_return
-
-    def wr_rd_eeprom(self, value="APSPU-1", address = 0):
-        #
-        # Write and Read the EEPROM to check functionality
-        #
-        if self.write_eeprom(value, address=0):
-            ret_value = self.read_eeprom(address=0, nof_bytes=len(value))
-            stri = "Wrote to EEPROM register 0x{2:x} : {0}, Read from EEPROM: {1}".format(value, ret_value, address)
-            print(stri)
-        return True
+        self.eeprom = EepromClass()
 
     def read_all(self):
         #
@@ -131,6 +92,54 @@ class ApspuClass:
             pol.on_off(on)
         return True
         
+class EepromClass:
+    #
+    # Class to handle EEPROM communication 
+    #
+    def __init__(self):
+        #
+        # All monitoring points Point of Load DC/DC converter
+        #
+        self.dev_i2c_eeprom = I2C(EEPROM)
+        self.dev_i2c_eeprom.bus_nr = I2CBUSNR
+
+    def write_eeprom(self, data="APSPU", address=0):
+        #
+        # Write the EEPROM with the serial number etc.
+        #
+        ret_ack, ret_value = self.dev_i2c_eeprom.read_bytes(0)
+        if ret_ack < 1:
+            print("EEPROM not found during write")
+            return False
+        else:
+            wr_data = bytearray(data.encode("utf-8", errors="ignore"))
+            for loc, data_byte in enumerate(wr_data):
+                self.dev_i2c_eeprom.write_bytes(address+loc, data_byte)
+                sleep(0.1)
+            return True
+
+    def read_eeprom(self, address=0, nof_bytes=5):
+        #
+        # Read the EEPROM with the serial number etc.
+        #
+        ret_ack, ret_value = self.dev_i2c_eeprom.read_last_reg(1)
+        if ret_ack < 1:
+            print("no EEPROM found during read")
+            return False
+        else:
+            ret_ack, ret_value = self.dev_i2c_eeprom.read_bytes(address, nof_bytes)
+            str_return = bytes.fromhex(ret_value[:nof_bytes*2]).decode('UTF-8')
+            return str_return
+
+    def wr_rd_eeprom(self, value="APSPU-1", address = 0):
+        #
+        # Write and Read the EEPROM to check functionality
+        #
+        if self.write_eeprom(value, address=0):
+            ret_value = self.read_eeprom(address=0, nof_bytes=len(value))
+            stri = "Wrote to EEPROM register 0x{2:x} : {0}, Read from EEPROM: {1}".format(value, ret_value, address)
+            print(stri)
+        return True
 
 
 class PolClass:
diff --git a/production_apspu.py b/production_apspu.py
index 6e9bd49..0d23249 100644
--- a/production_apspu.py
+++ b/production_apspu.py
@@ -46,5 +46,5 @@ apspu.print_status()
 #apspu.apspu_on_off(True)
 id = "APSPU-" + sys.argv[1]
 serial = sys.argv[2]
-apspu.wr_rd_eeprom(id, address=0)
-apspu.wr_rd_eeprom(serial, address=0x20)
+apspu.eeprom.wr_rd_eeprom(id, address=0)
+apspu.eeprom.wr_rd_eeprom(serial, address=0x20)
-- 
GitLab