Skip to content

Commit

Permalink
Garage Door State Machine initial version complete
Browse files Browse the repository at this point in the history
It now tells you if the door is open or closed. And by making
the door a class, in the future I could design this to give state
of all doors.
  • Loading branch information
Eric Mesa committed Mar 29, 2020
1 parent 6813b82 commit 2be96db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
14 changes: 8 additions & 6 deletions raspigaragealert/alert.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import RPi.GPIO as GPIO
from raspigaragealert import garage_door as door


def main():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print("Printing a 1 if garage is closed and a 0 if open")
while True:
print(GPIO.input(16))
my_door = door.door(16)
loop = True
while loop:
door_state_changed, state_in_words = my_door.has_state_changed()
if door_state_changed:
print(f"Garage door is now {state_in_words}.")
#loop = False


if __name__ == "__main__":
Expand Down
23 changes: 20 additions & 3 deletions raspigaragealert/garage_door.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
"""Class representing a Garage Door alarm."""

import RPi.GPIO as GPIO


class door():
def __init__(self, pin: int):
self.state = "closed"
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
self.state: int = 1
self.pin = pin
self.state_definition = {1:"closed", 0:"open"}


def _check_pin(self):
current_state = GPIO.input(self.pin)
return current_state


def has_state_changed() -> bool:
return True
def has_state_changed(self) -> tuple:
current_state = self._check_pin()
#print(f"For debugging: \nCurrent state: {current_state}. \nself.state: {self.state}")
if current_state != self.state:
self.state = current_state
state_in_words = self.state_definition[self.state]
return True, state_in_words
else:
return False, "unchanged"

0 comments on commit 2be96db

Please sign in to comment.