From f9e15d5eb796f3618632d0fb059b9abf0571ccb1 Mon Sep 17 00:00:00 2001 From: Tobias Schaffner Date: Mon, 26 Feb 2024 17:57:31 +0100 Subject: [PATCH] power/gpio: Allow to configure low level triggered relays Introduce an optional `enable` config key for the power gpio. This config defaults to `high` for high level triggered relays but can be set to `low` for low level triggered relays. Signed-off-by: Tobias Schaffner --- docs/config.rst | 4 ++++ mtda.ini | 3 ++- mtda/power/gpio.py | 18 +++++++++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index 553aeb84..663fab43 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -262,10 +262,14 @@ supported: Format: @ If multiple GPIO lines and pins are used separate the entries using ','. +* ``enable``: string [optional] + If the relay enable trigger is ``high`` or ``low``. Defaults to ``high``. + Example:: # For single GPIO line gpio = gpiochip0@201 + enable = high # For multiple GPIO lines gpio = gpiochip0@201,gpiochip1@11,gpiochip0@203 diff --git a/mtda.ini b/mtda.ini index 7d9dd4c8..3b6e77dc 100644 --- a/mtda.ini +++ b/mtda.ini @@ -132,7 +132,8 @@ time-until = login: variant=aviosys_8800 # variant=gpio -# pins=48 +# gpio=gpiochip0@203 +# enable=high # variant=pduclient # daemon=134.86.60.40 diff --git a/mtda/power/gpio.py b/mtda/power/gpio.py index f81e79e8..12db34f8 100644 --- a/mtda/power/gpio.py +++ b/mtda/power/gpio.py @@ -19,17 +19,29 @@ class GpioPowerController(PowerController): + HIGH = 1 + LOW = 0 + def __init__(self, mtda): self.dev = None self.mtda = mtda self.lines = [] self.gpiopair = [] + self.trigger = self.HIGH def configure(self, conf): self.mtda.debug(3, "power.gpio.configure()") result = None + if 'enable' in conf: + if conf['enable'] == 'high': + self.trigger = self.HIGH + elif conf['enable'] == 'low': + self.trigger = self.LOW + else: + raise ValueError("'enable' shall be either 'high' or 'low'!") + if 'gpio' in conf: for gpio in conf['gpio'].split(','): self.gpiopair.append(itemgetter(0, 1)(gpio.split('@'))) @@ -83,7 +95,7 @@ def on(self): result = False for line in self.lines: - line.set_value(1) + line.set_value(self.trigger) result = self.status() == self.POWER_ON self.mtda.debug(3, f"power.gpio.on(): {result}") @@ -95,7 +107,7 @@ def off(self): result = False for line in self.lines: - line.set_value(0) + line.set_value(self.trigger ^ 1) result = self.status() == self.POWER_OFF self.mtda.debug(3, f"power.gpio.off(): {result}") @@ -109,7 +121,7 @@ def status(self): for line in self.lines: value = line.get_value() - if value == 1: + if value == self.trigger: value = self.POWER_ON else: value = self.POWER_OFF