From 5bc273a555cfb1caaf3af5f7736307a01631ed16 Mon Sep 17 00:00:00 2001
From: awatlet <arnaud.watlet@umons.ac.be>
Date: Tue, 3 Oct 2023 16:53:08 +0200
Subject: [PATCH] Updates mb 2023 configs

---
 configs/config_mb_2023_3_mux_2024.py               |  6 +++---
 .../abstract_hardware_components.py                |  9 +++------
 ohmpi/hardware_components/mb_2023_0_X.py           | 14 ++++----------
 ohmpi/hardware_system.py                           |  4 ++--
 4 files changed, 12 insertions(+), 21 deletions(-)

diff --git a/configs/config_mb_2023_3_mux_2024.py b/configs/config_mb_2023_3_mux_2024.py
index cdbcc7a4..93622b6d 100644
--- a/configs/config_mb_2023_3_mux_2024.py
+++ b/configs/config_mb_2023_3_mux_2024.py
@@ -23,11 +23,11 @@ HARDWARE_CONFIG = {
     'tx':  {'model': 'mb_2023_0_X',
              'voltage_max': 12.,  # Maximum voltage supported by the TX board [V]
              'adc_voltage_max': 4800.,  # Maximum voltage read by the current ADC on the TX board [mA]
-             'r_shunt': 2  # Shunt resistance in Ohms
+             'r_shunt': 2.  # Shunt resistance in Ohms
             },
     'rx':  {'model': 'mb_2023_0_X',
              'coef_p2': 2.50,  # slope for conversion for ADS, measurement in V/V
-             'sampling_rate': 50  # number of samples per second
+             'sampling_rate': 50. # number of samples per second
             },
     'mux':  # default properties are system properties that will be
             # overwritten by board properties defined at the board level within the board model file
@@ -61,7 +61,7 @@ HARDWARE_CONFIG = {
                           'cabling': {(i+24, j): ('mux_04', i) for j in ['A', 'B', 'M', 'N'] for i in range(1, 9)},
                           'voltage_max': 12.}
                      },
-             'default': {'connection': 'i2c',
+             'default': {'interface_name': 'i2c',
                          'voltage_max': 100.,
                          'current_max': 3.}
              }
diff --git a/ohmpi/hardware_components/abstract_hardware_components.py b/ohmpi/hardware_components/abstract_hardware_components.py
index 5729623f..965e4744 100644
--- a/ohmpi/hardware_components/abstract_hardware_components.py
+++ b/ohmpi/hardware_components/abstract_hardware_components.py
@@ -9,7 +9,7 @@ from threading import Event, Barrier, BrokenBarrierError
 class CtlAbstract(ABC):
     def __init__(self, **kwargs):
         self.board_name = kwargs.pop('board_name', 'unknown CTL hardware')
-        self.interfaces = None  # TODO: allow for several buses
+        self.interfaces = None
         self.exec_logger = kwargs.pop('exec_logger', None)
         if self.exec_logger is None:
             self.exec_logger = create_stdout_logger('exec_ctl')
@@ -19,7 +19,7 @@ class CtlAbstract(ABC):
         self.exec_logger.debug(f'{self.board_name} Ctl initialization')
         self._cpu_temp_available = False
         self.max_cpu_temp = np.inf
-        self.connection = None
+        self.connection = kwargs.pop('connection', None)
 
     @property
     def cpu_temperature(self):
@@ -56,8 +56,7 @@ class PwrAbstract(ABC):
         self._current_max = kwargs.pop('current_max', 0.)
         self._voltage_min = kwargs.pop('voltage_min', 0.)
         self._voltage_max = kwargs.pop('voltage_max', 0.)
-        self.ctl = kwargs.pop('ctl', None)
-        self.connection = kwargs.pop('io', None)
+        self.connection = kwargs.pop('connection', None)
 
     @property
     @abstractmethod
@@ -256,7 +255,6 @@ class TxAbstract(ABC):
         self.soh_logger = kwargs.pop('soh_logger', None)
         if self.soh_logger is None:
             self.soh_logger = create_stdout_logger('soh_tx')
-        self.ctl = kwargs.pop('ctl', None)
         self.connection = kwargs.pop('connection', None)
         self.pwr = kwargs.pop('pwr', None)
         self._polarity = 0
@@ -374,7 +372,6 @@ class RxAbstract(ABC):
         self.soh_logger = kwargs.pop('soh_logger', None)
         if self.soh_logger is None:
             self.soh_logger = create_stdout_logger('soh_rx')
-        self.ctl = kwargs.pop('ctl', None)
         self.connection = kwargs.pop('connection', None)
         self.board_name = kwargs.pop('board_name', 'unknown RX hardware')
         self._sampling_rate = kwargs.pop('sampling_rate', 1)  # ms
diff --git a/ohmpi/hardware_components/mb_2023_0_X.py b/ohmpi/hardware_components/mb_2023_0_X.py
index 1dd0c329..52a750bf 100644
--- a/ohmpi/hardware_components/mb_2023_0_X.py
+++ b/ohmpi/hardware_components/mb_2023_0_X.py
@@ -6,6 +6,7 @@ from adafruit_ads1x15.analog_in import AnalogIn  # noqa
 from adafruit_ads1x15.ads1x15 import Mode  # noqa
 from adafruit_mcp230xx.mcp23008 import MCP23008  # noqa
 from digitalio import Direction  # noqa
+from busio import I2C  # noqa
 import time
 import numpy as np
 import os
@@ -22,7 +23,6 @@ from ohmpi.utils import enforce_specs
 # voltages are given in mV, currents in mA, sampling rates in Hz and data_rate in S/s
 SPECS = {'rx': {'sampling_rate': {'min': 2., 'default': 10., 'max': 100.},
                 'data_rate': {'default': 860.},
-                'connection': {'default': 'i2c'},
                 'bias':  {'min': -5000., 'default': 0., 'max': 5000.},
                 'coef_p2': {'default': 2.50}},
          'tx': {'adc_voltage_min': {'default': 10.},
@@ -33,7 +33,6 @@ SPECS = {'rx': {'sampling_rate': {'min': 2., 'default': 10., 'max': 100.},
                 'r_shunt':  {'min': 0., 'default': 2. },
                 'activation_delay': {'default': 0.005},
                 'release_delay': {'default': 0.001},
-                'connection': {'default': 'i2c'}
                 }}
 
 # TODO: move low_battery spec in pwr
@@ -111,6 +110,7 @@ class Tx(TxAbstract):
             kwargs = enforce_specs(kwargs, SPECS['tx'], key)
         kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
         super().__init__(**kwargs)
+        assert isinstance(self.connection, I2C)
         kwargs.update({'pwr': kwargs.pop('pwr', SPECS['compatible_power_sources'][0])})
         if kwargs['pwr'] not in SPECS['tx']['compatible_power_sources']:
             self.exec_logger.warning(f'Incompatible power source specified check config')
@@ -121,11 +121,6 @@ class Tx(TxAbstract):
 
         self.voltage_adjustable = False
         self.current_adjustable = False
-        if self.ctl is None:
-            self.ctl = ctl_module.Ctl()
-        # elif isinstance(self.ctl, dict):
-        #     self.ctl = ctl_module.Ctl(**self.ctl)
-        self.connection = self.ctl.interfaces[kwargs['connection']]
 
         # I2C connexion to MCP23008, for current injection
         self.mcp_board = MCP23008(self.connection, address=0x20)
@@ -258,10 +253,9 @@ class Rx(RxAbstract):
             kwargs = enforce_specs(kwargs, SPECS['rx'], key)
         kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
         super().__init__(**kwargs)
+        assert isinstance(self.connection, I2C)
+
         self.exec_logger.event(f'{self.board_name}\trx_init\tbegin\t{datetime.datetime.utcnow()}')
-        if self.ctl is None:
-            self.ctl = ctl_module.Ctl()
-        self.connection = self.ctl.interfaces[kwargs['connection']]
 
         # ADS1115 for voltage measurement (MN)
         self._ads_voltage_address = 0x49
diff --git a/ohmpi/hardware_system.py b/ohmpi/hardware_system.py
index 647267aa..b48eee89 100644
--- a/ohmpi/hardware_system.py
+++ b/ohmpi/hardware_system.py
@@ -110,8 +110,8 @@ class OhmPiHardware:
                 mux_ctl_module = importlib.import_module(f'ohmpi.hardware_components.{mux_config["ctl"]["model"]}')
                 mux_config['ctl'] = mux_ctl_module.Ctl(**mux_config['ctl'])  # (**self.ctl)
             assert issubclass(type(mux_config['ctl']), CtlAbstract)
-            io = mux_config.pop('io', mux_config['ctl'].interfaces[mux_config.pop('connection', 'i2c')])
-            mux_config.update({'io': io})
+            mux_config.update({
+                mux_config.pop('connection', mux_config['ctl'].interfaces[mux_config.pop('interface_name', 'i2c')])})
             mux_config['id'] = mux_id
 
             self.mux_boards[mux_id] = mux_module.Mux(**mux_config)
-- 
GitLab