Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

power/gpio: Allow to configure low level triggered relays #404

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading