From f6304b8d71a4b5529a8f75713e8537ba67327d05 Mon Sep 17 00:00:00 2001
From: su530201 <olivier.kaufmann@umons.ac.be>
Date: Sun, 30 Apr 2023 14:16:47 +0200
Subject: [PATCH] Rewrites MUX config handling

---
 config_mb_2023_mux_2024.py              | 21 +++++++++++---------
 hardware_components/dummy_mux.py        |  6 ++++--
 hardware_components/mux_2024_rev_0_0.py | 26 +++++++++++++------------
 hardware_system.py                      |  9 +++++++--
 4 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/config_mb_2023_mux_2024.py b/config_mb_2023_mux_2024.py
index 5a280c27..572b3532 100644
--- a/config_mb_2023_mux_2024.py
+++ b/config_mb_2023_mux_2024.py
@@ -30,15 +30,18 @@ HARDWARE_CONFIG = {
              'sampling_rate': 10., # ms
              'nb_samples': 20,  # Max value 10 # was named integer before...
             },
-    '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...)
-             'mcp_1' : '0x23',  # TODO : Replace this with pos of jumper on MUX board (address doesn't mean anything for the average user...)
-             # 'addresses': './hardware_components/mux_2024_22_23_4_roles_addressing_table.json',
-             'voltage_max': 100,
-             'current_max': 3
-            }}
+    'mux':  # default properties are system properties that will be
+            # overwritten by board properties defined at the board level within the board model file
+            # both will be overwritten by properties specified in the board dict below. Use with caution...
+        {'boards':
+                {'mux_1': {'model' : 'mux_2024_rev_0_0', # 'ohmpi_i2c_mux64_v1.01',
+                 'tca_address': None,
+                 'tca_channel': 0,
+                 'mcp_0' : '0x22',  # TODO : Replace this with pos of jumper on MUX board (address doesn't mean anything for the average user...)
+                 'mcp_1' : '0x23',  # TODO : Replace this with pos of jumper on MUX board (address doesn't mean anything for the average user...)
+                 'voltage_max': 12.
+                }},
+            'default': {'voltage_max': 100., 'current_max': 3.}}
 }
 
 # SET THE LOGGING LEVELS, MQTT BROKERS AND MQTT OPTIONS ACCORDING TO YOUR NEEDS
diff --git a/hardware_components/dummy_mux.py b/hardware_components/dummy_mux.py
index eb07b97e..8a95fea5 100644
--- a/hardware_components/dummy_mux.py
+++ b/hardware_components/dummy_mux.py
@@ -1,13 +1,15 @@
 from OhmPi.config import HARDWARE_CONFIG
 import os
 from OhmPi.hardware_components import MuxAbstract
-MUX_CONFIG = HARDWARE_CONFIG['mux']
+MUX_CONFIG = HARDWARE_CONFIG['mux'].pop('default', {})
 
 class Mux(MuxAbstract):
     def __init__(self, **kwargs):
         kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
         super().__init__(**kwargs)
-        self.max_elec = MUX_CONFIG['max_elec']
+
+    def _get_addresses(self):
+        pass
 
     def reset(self):
         pass
diff --git a/hardware_components/mux_2024_rev_0_0.py b/hardware_components/mux_2024_rev_0_0.py
index 904c019f..e2d5964c 100644
--- a/hardware_components/mux_2024_rev_0_0.py
+++ b/hardware_components/mux_2024_rev_0_0.py
@@ -6,10 +6,12 @@ import adafruit_tca9548a  # noqa
 from adafruit_mcp230xx.mcp23017 import MCP23017  # noqa
 from digitalio import Direction  # noqa
 
-MUX_CONFIG = HARDWARE_CONFIG['mux']
-MUX_CONFIG['default_mux_cabling'] = {(elec, role) : ('mux_1', elec) for role in ['A', 'B', 'M', 'N'] for elec in range(1,9)} # 4 roles cabling electrodes from 1 to 8
+MUX_CONFIG = HARDWARE_CONFIG['mux'].pop('default', {})
+MUX_CONFIG.update({'voltage_max': 50., 'current_max': 3.})  # board default values that overwrite system default values
+default_mux_cabling = {(elec, role) : ('mux_1', elec) for role in ['A', 'B', 'M', 'N'] for elec in range(1,9)} # 4 roles cabling electrodes from 1 to 8
 
-inner_cabling ={'4_roles' : {(1, 'X'): {'MCP': 0, 'MCP_GPIO': 0}, (1, 'Y'): {'MCP': 0, 'MCP_GPIO': 8},
+
+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},
                              (3, 'X'): {'MCP': 0, 'MCP_GPIO': 2}, (3, 'Y'): {'MCP': 0, 'MCP_GPIO': 10},
                              (4, 'X'): {'MCP': 0, 'MCP_GPIO': 3}, (4, 'Y'): {'MCP': 0, 'MCP_GPIO': 11},
@@ -25,7 +27,8 @@ inner_cabling ={'4_roles' : {(1, 'X'): {'MCP': 0, 'MCP_GPIO': 0}, (1, 'Y'): {'MC
                              (6, 'XX'): {'MCP': 1, 'MCP_GPIO': 2}, (6, 'YY'): {'MCP': 1, 'MCP_GPIO': 10},
                              (7, 'XX'): {'MCP': 1, 'MCP_GPIO': 1}, (7, 'YY'): {'MCP': 1, 'MCP_GPIO': 9},
                              (8, 'XX'): {'MCP': 1, 'MCP_GPIO': 0}, (8, 'YY'): {'MCP': 1, 'MCP_GPIO': 8}},
-                '2_roles':  {(1, 'X'): {'MCP': 0, 'MCP_GPIO': 0}, (1, 'Y'): {'MCP': 0, 'MCP_GPIO': 8}, # TODO: WARNING check 2_roles table, it has not been verified yet !!!
+                '2_roles': # TODO: WARNING check 2_roles table, it has not been verified yet !!!
+                            {(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},
                              (3, 'X'): {'MCP': 0, 'MCP_GPIO': 2}, (3, 'Y'): {'MCP': 0, 'MCP_GPIO': 10},
                              (4, 'X'): {'MCP': 0, 'MCP_GPIO': 3}, (4, 'Y'): {'MCP': 0, 'MCP_GPIO': 11},
@@ -46,6 +49,8 @@ inner_cabling ={'4_roles' : {(1, 'X'): {'MCP': 0, 'MCP_GPIO': 0}, (1, 'Y'): {'MC
 
 class Mux(MuxAbstract):
     def __init__(self, **kwargs):
+        if 'id' in kwargs.keys():
+            MUX_CONFIG.update(MUX_CONFIG[kwargs['id']])
         kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
         if 'cabling' not in kwargs.keys():
             kwargs.update({'cabling': MUX_CONFIG['default_mux_cabling']})
@@ -67,7 +72,7 @@ class Mux(MuxAbstract):
             self._tca = self.controller.bus
         else:
             self._tca = adafruit_tca9548a.TCA9548A(self.controller.bus, tca_address)[tca_channel]
-        self._mcp_addresses = (kwargs.pop('mcp_0', '0x22'), kwargs.pop('mcp_1', '0x23'))  # TODO add assert on valid addresses..
+        self._mcp_addresses = (kwargs.pop('mcp_0', '0x22'), kwargs.pop('mcp_1', '0x23'))  # TODO: add assert on valid addresses..
         self._mcp = [None, None]
         self.reset()
         if self.addresses is None:
@@ -90,16 +95,13 @@ class Mux(MuxAbstract):
     def switch_one(self, elec=None, role=None, state=None):
         MuxAbstract.switch_one(self, elec=elec, role=role, state=state)
 
-        def set_relay_state(mcp, mcp_pin, state=True):
+        def activate_relay(mcp, mcp_pin, value=True):
             pin_enable = mcp.get_pin(mcp_pin)
             pin_enable.direction = Direction.OUTPUT
-            pin_enable.value = state
+            pin_enable.value = value
 
         d = self.addresses[elec, role]
         if state == 'on':
-            set_relay_state(self._mcp[d['MCP']], d['MCP_GPIO'], True)
+            activate_relay(self._mcp[d['MCP']], d['MCP_GPIO'], True)
         if state == 'off':
-            set_relay_state(self._mcp[d['MCP']], d['MCP_GPIO'], False)
-
-    def test(self, *args):
-        MuxAbstract.test(self, *args)
\ No newline at end of file
+            activate_relay(self._mcp[d['MCP']], d['MCP_GPIO'], False)
\ No newline at end of file
diff --git a/hardware_system.py b/hardware_system.py
index 4b59290d..9fa70722 100644
--- a/hardware_system.py
+++ b/hardware_system.py
@@ -3,16 +3,21 @@ import datetime
 import time
 import numpy as np
 from OhmPi.logging_setup import create_stdout_logger
+from OhmPi.utils import update_dict
 from OhmPi.config import HARDWARE_CONFIG
 from threading import Thread, Event, Barrier
 
 controller_module = importlib.import_module(f'OhmPi.hardware_components.{HARDWARE_CONFIG["controller"]["model"]}')
 tx_module = importlib.import_module(f'OhmPi.hardware_components.{HARDWARE_CONFIG["tx"]["model"]}')
 rx_module = importlib.import_module(f'OhmPi.hardware_components.{HARDWARE_CONFIG["rx"]["model"]}')
-mux_module = importlib.import_module(f'OhmPi.hardware_components.{HARDWARE_CONFIG["mux"]["model"]}')
+MUX_CONFIG = {}
+for mux_id, mux_config in HARDWARE_CONFIG['mux']['boards'].items():
+    mux_module = importlib.import_module(f'OhmPi.hardware_components.{mux_config["model"]}')
+    update_dict(MUX_CONFIG[mux_id], mux_module.MUX_CONFIG)
+    update_dict(MUX_CONFIG, mux_config)
+    update_dict(MUX_CONFIG[mux_id], HARDWARE_CONFIG['mux']['common'])
 TX_CONFIG = tx_module.TX_CONFIG
 RX_CONFIG = rx_module.RX_CONFIG
-MUX_CONFIG = mux_module.MUX_CONFIG
 
 current_max = np.min([TX_CONFIG['current_max'], MUX_CONFIG['current_max']])
 voltage_max = np.min([TX_CONFIG['voltage_max'], MUX_CONFIG['voltage_max']])
-- 
GitLab