From de979804a975cf12c1cdbeca3f08099ad75637da Mon Sep 17 00:00:00 2001
From: su530201 <olivier.kaufmann@umons.ac.be>
Date: Tue, 26 Sep 2023 12:44:32 +0200
Subject: [PATCH] Updates mux_2024 specs handling

---
 .../abstract_hardware_components.py           |  1 -
 ohmpi/hardware_components/mux_2024_rev_0_0.py | 39 +++++++------------
 ohmpi/hardware_components/ohmpi_card_3_15.py  |  1 +
 ohmpi/hardware_system.py                      |  4 +-
 4 files changed, 18 insertions(+), 27 deletions(-)

diff --git a/ohmpi/hardware_components/abstract_hardware_components.py b/ohmpi/hardware_components/abstract_hardware_components.py
index 193305f2..ab9da2a0 100644
--- a/ohmpi/hardware_components/abstract_hardware_components.py
+++ b/ohmpi/hardware_components/abstract_hardware_components.py
@@ -111,7 +111,6 @@ class MuxAbstract(ABC):
         if self.board_id is None:
             self.exec_logger.error(f'MUX {self.board_name} should have an id !')
         self.exec_logger.debug(f'MUX {self.board_id} ({self.board_name}) initialization')
-        self.ctl = kwargs.pop('ctl', None)
         self.io = kwargs.pop('io', None)
         cabling = kwargs.pop('cabling', None)
         self.cabling = {}
diff --git a/ohmpi/hardware_components/mux_2024_rev_0_0.py b/ohmpi/hardware_components/mux_2024_rev_0_0.py
index a5dd69c6..eb3ec474 100644
--- a/ohmpi/hardware_components/mux_2024_rev_0_0.py
+++ b/ohmpi/hardware_components/mux_2024_rev_0_0.py
@@ -5,25 +5,14 @@ from ohmpi.hardware_components import MuxAbstract
 import adafruit_tca9548a  # noqa
 from adafruit_mcp230xx.mcp23017 import MCP23017  # noqa
 from digitalio import Direction  # noqa
+from busio import I2C
 # import time
 
-# board specs
-voltage_max = 50
-current_max = 3.
-activation_delay = 0.01
-release_delay = 0.005
-
-MUX_CONFIG = HARDWARE_CONFIG['mux'].pop('default', {})
-MUX_CONFIG.update({'voltage_max': max(0.,min(MUX_CONFIG.pop('voltage_max', voltage_max), voltage_max)),
-                   'current_max': max(0.,min(MUX_CONFIG.pop('current_max', voltage_max), voltage_max))})
-
-MUX_CONFIG.update({'activation_delay': max(MUX_CONFIG.pop('activation_delay', activation_delay), activation_delay),
-                   'release_delay': max(MUX_CONFIG.pop('release_delay', release_delay), release_delay)})
+# hardware characteristics and limitations
+SPECS = {'voltage_max': 50., 'current_max': 3., 'activation_delay': 0.01, 'release_delay': 0.005}
 
 # defaults to 4 roles cabling electrodes from 1 to 8
 default_mux_cabling = {(elec, role) : ('mux_1', elec) for role in ['A', 'B', 'M', 'N'] for elec in range(1,9)}
-# defaults to ic connection
-ctl_connection = MUX_CONFIG.pop('connection', 'i2c')
 
 inner_cabling = {'4_roles' : {(1, 'X'): {'MCP': 0, 'MCP_GPIO': 0}, (1, 'Y'): {'MCP': 0, 'MCP_GPIO': 8},
                              (2, 'X'): {'MCP': 0, 'MCP_GPIO': 1}, (2, 'Y'): {'MCP': 0, 'MCP_GPIO': 9},
@@ -63,17 +52,19 @@ inner_cabling = {'4_roles' : {(1, 'X'): {'MCP': 0, 'MCP_GPIO': 0}, (1, 'Y'): {'M
 
 class Mux(MuxAbstract):
     def __init__(self, **kwargs):
-        if 'id' in kwargs.keys():
-            MUX_CONFIG.update(HARDWARE_CONFIG['mux']['boards'][kwargs['id']])
         kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
-        if 'cabling' not in kwargs.keys() or kwargs['cabling']=={}:
-            kwargs.update({'cabling': default_mux_cabling})
-        if 'activation_delay' not in kwargs:
-            kwargs.update({'activation_delay': MUX_CONFIG['activation_delay']})
-        if 'release_delay' not in kwargs:
-            kwargs.update({'release_delay': MUX_CONFIG['release_delay']})
+        kwargs.update({'cabling': kwargs.pop('cabling', default_mux_cabling)})
+        kwargs.update({'activation_delay': max(kwargs.pop('activation_delay', SPECS['activation_delay']),
+                                               SPECS['activation_delay'])})
+        kwargs.update({'release_delay': max(kwargs.pop('release_delay', SPECS['release_delay']),
+                                               SPECS['activation_delay'])})
+        kwargs.update({'voltage_max': max(0., min(kwargs.pop('voltage_max', SPECS['voltage_max']),
+                                                  SPECS['voltage_max']))})
+        kwargs.update({'current_max': max(0., min(kwargs.pop('current_max', SPECS['current_max']),
+                                                  SPECS['current_max']))})
         super().__init__(**kwargs)
-        self.exec_logger.debug(f'configuration: {MUX_CONFIG}')
+        assert isinstance(self.io, I2C)
+        self.exec_logger.debug(f'configuration: {kwargs}')
         tca_address = kwargs.pop('tca_address', None)
         tca_channel = kwargs.pop('tca_channel', 0)
         self._roles = kwargs.pop('roles', None)
@@ -86,7 +77,7 @@ class Mux(MuxAbstract):
         else:
             self.exec_logger.error(f'Invalid role assignment for {self.board_name}: {self._roles} !')
             self._mode = ''
-        self.io = self.ctl.connections[kwargs.pop('connection', ctl_connection)]
+        # self.io = self.ctl.connections[kwargs.pop('connection', ctl_connection)] -> to hardware_system
         if tca_address is None:
             self._tca = self.io
         else:
diff --git a/ohmpi/hardware_components/ohmpi_card_3_15.py b/ohmpi/hardware_components/ohmpi_card_3_15.py
index 643ed23a..b3cee905 100644
--- a/ohmpi/hardware_components/ohmpi_card_3_15.py
+++ b/ohmpi/hardware_components/ohmpi_card_3_15.py
@@ -24,6 +24,7 @@ voltage_adc_voltage_min = 10.  # mV
 voltage_adc_voltage_max = 4500.  # mV
 sampling_rate = 20.  # Hz
 data_rate = 860.  # S/s?
+
 RX_CONFIG['voltage_min'] = np.min([voltage_adc_voltage_min, RX_CONFIG.pop('voltage_min', np.inf)])  # mV
 RX_CONFIG['voltage_max'] = np.min([voltage_adc_voltage_max, RX_CONFIG.pop('voltage_max', np.inf)])  # mV
 RX_CONFIG['sampling_rate'] = RX_CONFIG.pop('sampling_rate', sampling_rate)
diff --git a/ohmpi/hardware_system.py b/ohmpi/hardware_system.py
index 3ee3e023..11843f57 100644
--- a/ohmpi/hardware_system.py
+++ b/ohmpi/hardware_system.py
@@ -108,9 +108,9 @@ class OhmPiHardware:
             if isinstance(ctl, dict):
                 mux_ctl_module = importlib.import_module(f'ohmpi.hardware_components.{mux_config["ctl"]["model"]}')
                 ctl = mux_ctl_module.Ctl(**self.ctl)
-            mux_config.update({'ctl': ctl})
             assert issubclass(type(mux_config['ctl']), CtlAbstract)
-
+            io = mux_module.pop('io', self.ctl.connections[mux_config.pop('connection', 'i2c')])
+            mux_config.update({'io': io})
             mux_config['id'] = mux_id
 
             self.mux_boards[mux_id] = mux_module.Mux(**mux_config)
-- 
GitLab