From 5cd5c22b8afb3ba7d64204fbf397caf988922d19 Mon Sep 17 00:00:00 2001
From: su530201 <olivier.kaufmann@umons.ac.be>
Date: Thu, 27 Apr 2023 12:22:29 +0200
Subject: [PATCH] Works on issue #101

---
 ...i_card_3_15.py => config_mb_2023_mux_2024.py |  4 ++--
 .../abstract_hardware_components.py             | 17 +++++++++++++----
 hardware_components/mux_2024_rev_0_0.py         |  3 ++-
 hardware_system.py                              |  8 ++++++--
 test_measure_with_ohmpi_card_3_15.py            |  2 +-
 test_mux_2024.py                                |  2 +-
 6 files changed, 25 insertions(+), 11 deletions(-)
 rename config_ohmpi_card_3_15.py => config_mb_2023_mux_2024.py (97%)

diff --git a/config_ohmpi_card_3_15.py b/config_mb_2023_mux_2024.py
similarity index 97%
rename from config_ohmpi_card_3_15.py
rename to config_mb_2023_mux_2024.py
index e5ff45a1..d3f4d8be 100644
--- a/config_ohmpi_card_3_15.py
+++ b/config_mb_2023_mux_2024.py
@@ -30,7 +30,7 @@ HARDWARE_CONFIG = {
              'sampling_rate': 10., # ms
              'nb_samples': 20,  # Max value 10 # was named integer before...
             },
-    'mux': {'model' : 'mux_2024_rev_0_0', # 'ohmpi_i2c_mux64_v1.01',
+    'mux': {'mux_1':{'model' : 'mux_2024_rev_0_0', # 'ohmpi_i2c_mux64_v1.01',
              'tca_address': None,  # TODO: This should be part of the system config (cabling of several mux boards)
              'tca_channel': 0,  # TODO: This should be part of the system config (cabling of several mux boards)
              'mcp_0' : '0x22',  # TODO : Replace this with pos of jumper on MUX board (address doesn't mean anything for the average user...)
@@ -38,7 +38,7 @@ HARDWARE_CONFIG = {
              # 'addresses': './hardware_components/mux_2024_22_23_4_roles_addressing_table.json',
              'voltage_max': 100,
              'current_max': 3
-            }
+            }}
 }
 
 # SET THE LOGGING LEVELS, MQTT BROKERS AND MQTT OPTIONS ACCORDING TO YOUR NEEDS
diff --git a/hardware_components/abstract_hardware_components.py b/hardware_components/abstract_hardware_components.py
index 76550cd3..db86866d 100644
--- a/hardware_components/abstract_hardware_components.py
+++ b/hardware_components/abstract_hardware_components.py
@@ -41,8 +41,17 @@ class MuxAbstract(ABC):
         self.soh_logger = kwargs.pop('soh_logger', None)
         if self.soh_logger is None:
             self.soh_logger = create_stdout_logger('soh_mux')
-        self.exec_logger.debug(f'{self.board_name} MUX initialization')
+        self.board_id = kwargs.pop('id', None)
+        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.controller = kwargs.pop('controller', None)
+        cabling = kwargs.pop('cabling', None)
+        if cabling is not None:
+            self._cabling = {}
+            for k, v in cabling:
+                if v[0]==self.board_id:
+                    self._cabling.update({k: v[1]})
         self.addresses = kwargs.pop('addresses', None)
 
     @abstractmethod
@@ -82,8 +91,8 @@ class MuxAbstract(ABC):
                         or np.in1d(elec_dict['M'], elec_dict['B']).any()
                         or np.in1d(elec_dict['N'], elec_dict['A']).any()
                         or np.in1d(elec_dict['N'], elec_dict['B']).any()) and state=='on':
-                    self.exec_logger.error('Trying to switch on some electrodes with both M or N roles and A or B roles.'
-                                           'This would create an over-voltage in the RX! Switching aborted.')
+                    self.exec_logger.error('Trying to switch on some electrodes with both M or N role and A or B role. '
+                                           'This could create an over-voltage in the RX! Switching aborted.')
                     return
 
             # if all ok, then switch the electrodes
@@ -208,7 +217,7 @@ class TxAbstract(ABC):
 
     @voltage.setter
     @abstractmethod
-    def voltage(self, value, **kwargs):
+    def voltage(self, value):
         # add actions to set the DPS voltage
         pass
 
diff --git a/hardware_components/mux_2024_rev_0_0.py b/hardware_components/mux_2024_rev_0_0.py
index 18063456..cc53429b 100644
--- a/hardware_components/mux_2024_rev_0_0.py
+++ b/hardware_components/mux_2024_rev_0_0.py
@@ -103,10 +103,11 @@ class Mux(MuxAbstract):
         self.exec_logger.debug(f'addresses: {self.addresses}')
 
     def _get_addresses(self):
+        """ Converts inner cabling addressing into (electrodes, role) addressing """
         d = inner_cabling[self._mode]
         self.addresses = {}
         for k, v in d.items():
-            self.addresses.update({(k[0], self._roles[k[1]]): v})
+            self.addresses.update({(self._cabling(k[0], self._roles[k[1]])): v})
         print(f'addresses: {self.addresses}')
 
     def reset(self):
diff --git a/hardware_system.py b/hardware_system.py
index 2eff82fe..4f254b5f 100644
--- a/hardware_system.py
+++ b/hardware_system.py
@@ -19,6 +19,8 @@ current_max = np.min([TX_CONFIG['current_max'], MUX_CONFIG['current_max']])
 voltage_max = np.min([TX_CONFIG['voltage_max'], MUX_CONFIG['voltage_max']])
 voltage_min = RX_CONFIG['voltage_min']
 
+default_mux_cabling = {(i, j) : ('mux_1', i) for j in ['A', 'B', 'M', 'N'] for i in range(1,9)}
+
 def elapsed_seconds(start_time):
     lap = datetime.datetime.utcnow() - start_time
     return lap.total_seconds()
@@ -47,10 +49,12 @@ class OhmPiHardware:
                                                  data_logger=self.data_logger,
                                                  soh_logger=self.soh_logger,
                                                  controller=self.controller))
-        self.mux = kwargs.pop('mux', mux_module.Mux(exec_logger=self.exec_logger,
+        self._cabling = kwargs.pop('cabling', default_mux_cabling)
+        self.mux = kwargs.pop('mux', {'mux_1': mux_module.Mux(exec_logger=self.exec_logger,
                                                     data_logger=self.data_logger,
                                                     soh_logger=self.soh_logger,
-                                                    controller=self.controller))
+                                                    controller=self.controller,
+                                                    cabling = self._cabling)})
         self.readings = np.array([])  # time series of acquired data
         self._start_time = None  # time of the beginning of a readings acquisition
         self._pulse = 0  # pulse number
diff --git a/test_measure_with_ohmpi_card_3_15.py b/test_measure_with_ohmpi_card_3_15.py
index cfb073ad..c7cba220 100644
--- a/test_measure_with_ohmpi_card_3_15.py
+++ b/test_measure_with_ohmpi_card_3_15.py
@@ -2,7 +2,7 @@ import numpy as np
 import logging
 import matplotlib.pyplot as plt
 from utils import change_config
-change_config('config_ohmpi_card_3_15.py', verbose=False)
+change_config('config_mb_2023_mux_2024.py', verbose=False)
 from OhmPi.hardware_system import OhmPiHardware
 
 k = OhmPiHardware()
diff --git a/test_mux_2024.py b/test_mux_2024.py
index 1306fb4b..33b32e07 100644
--- a/test_mux_2024.py
+++ b/test_mux_2024.py
@@ -1,6 +1,6 @@
 import time
 from utils import change_config
-change_config('config_ohmpi_card_3_15.py', verbose=False)
+change_config('config_mb_2023_mux_2024.py', verbose=False)
 from OhmPi.hardware_components.mux_2024_rev_0_0 import Mux, MUX_CONFIG
 from OhmPi.hardware_components import raspberry_pi as controller_module
 
-- 
GitLab