Skip to content

Commit

Permalink
power/gpio: Allow to configure low level triggered relays
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
TobiasSchaffner committed Feb 26, 2024
1 parent bc3eb4f commit f9e15d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,14 @@ supported:
Format: <gpiochipx>@<pin>
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

Expand Down
3 changes: 2 additions & 1 deletion mtda.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions mtda/power/gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('@')))
Expand Down Expand Up @@ -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}")
Expand All @@ -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}")
Expand All @@ -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
Expand Down

0 comments on commit f9e15d5

Please sign in to comment.