-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
243 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
.vscode | ||
__pycache__ | ||
OctoRelay.egg-info | ||
.coverage | ||
coverage.lcov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
from typing import Optional | ||
from RPi import GPIO | ||
|
||
# The driver operated BCM mode of pins enumeration | ||
GPIO.setmode(GPIO.BCM) | ||
|
||
def xor(left: bool, right: bool) -> bool: | ||
return left is not right | ||
|
||
class Relay(): | ||
def __init__(self, pin: int, inverted: bool): | ||
self.pin = pin # GPIO pin | ||
self.inverted = inverted # marks the relay as normally closed, None - when only going to read its pin state | ||
|
||
def __repr__(self) -> str: | ||
return f"{type(self).__name__}(pin={self.pin},inverted={self.inverted},closed={self.is_closed()})" | ||
|
||
def close(self): | ||
"""Activates the current flow through the relay.""" | ||
self.toggle(True) | ||
|
||
def open(self): | ||
"""Deactivates the current flow through the relay.""" | ||
self.toggle(False) | ||
|
||
def is_closed(self) -> bool: | ||
"""Returns the logical state of the relay.""" | ||
GPIO.setwarnings(False) | ||
GPIO.setup(self.pin, GPIO.OUT) | ||
pin_state = bool(GPIO.input(self.pin)) | ||
GPIO.setwarnings(True) | ||
return xor(self.inverted, pin_state) | ||
|
||
def toggle(self, desired_state: Optional[bool] = None) -> bool: | ||
""" | ||
Switches the relay state to the desired one specified as an optional argument. | ||
If the argument is not specified then switches based on the current state. | ||
Returns the new logical state of the relay. | ||
""" | ||
if desired_state is None: | ||
desired_state = not self.is_closed() | ||
GPIO.setwarnings(False) | ||
GPIO.setup(self.pin, GPIO.OUT) | ||
GPIO.output(self.pin, xor(self.inverted, desired_state)) | ||
GPIO.setwarnings(True) | ||
return desired_state |
Oops, something went wrong.