Skip to content

Commit

Permalink
Revert "Using active_low prop of LineSettings for addressing inverted…
Browse files Browse the repository at this point in the history
… relays."

This reverts commit 544df8a.
  • Loading branch information
RobinTail committed May 19, 2024
1 parent 544df8a commit ada3ff4
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions octoprint_octorelay/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
from gpiod import request_lines, LineSettings
from gpiod.line import Direction, Value

def xor(left: bool, right: bool) -> bool:
return left is not right

class Relay():
def __init__(self, pin: int, inverted: bool):
self.request = request_lines(
"/dev/gpiochip0",
consumer = "OctoRelay",
config = {
pin: LineSettings(direction = Direction.OUTPUT, active_low = inverted)
pin: LineSettings(direction=Direction.OUTPUT)
}
)
self.pin = pin # GPIO pin
self.inverted = inverted # for serialization purposes only
self.inverted = inverted # marks the relay as normally closed

def __repr__(self) -> str:
return f"{type(self).__name__}(pin={self.pin},inverted={self.inverted},closed={self.is_closed()})"
Expand All @@ -29,7 +32,7 @@ def open(self):
def is_closed(self) -> bool:
"""Returns the logical state of the relay."""
pin_state = self.request.get_value(self.pin) == Value.ACTIVE
return pin_state
return xor(self.inverted, pin_state)

def toggle(self, desired_state: Optional[bool] = None) -> bool:
"""
Expand All @@ -39,5 +42,5 @@ def toggle(self, desired_state: Optional[bool] = None) -> bool:
"""
if desired_state is None:
desired_state = not self.is_closed()
self.request.set_value(self.pin, Value.ACTIVE if desired_state else Value.INACTIVE)
self.request.set_value(self.pin, Value.ACTIVE if xor(self.inverted, desired_state) else Value.INACTIVE)
return desired_state

0 comments on commit ada3ff4

Please sign in to comment.