Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #!/usr/bin/python
  2. import RPi.GPIO as GPIO
  3. import time
  4. import os
  5. class detector(object):
  6. def __init__(self, sensor):
  7. self.callBacks = []
  8. self.sensor = sensor
  9. self.currState = False
  10. self.prevState = False
  11. GPIO.setmode(GPIO.BOARD)
  12. GPIO.setup(self.sensor, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  13. def read(self):
  14. self.prevState = self.currState
  15. self.currState = GPIO.input(self.sensor)
  16. def printState(self):
  17. print( "GPIO pin {0} is {1}".format(self.sensor, "HIGH" if self.currState else "LOW"))
  18. def subscribe(self, callBack):
  19. self.callBacks.append(callBack)
  20. def callBack(self, state):
  21. for fn in self.callBacks:
  22. fn(state)
  23. def start(self):
  24. try:
  25. self.read()
  26. self.printState()
  27. while True:
  28. self.read()
  29. if self.currState != self.prevState:
  30. self.printState()
  31. self.callBack(self.currState)
  32. time.sleep(.1)
  33. #Since fbi doesn't restore the console correctly when the application is exited we do a little clean up and handle the KeyboardInterrupt event.
  34. except (KeyboardInterrupt, SystemExit):
  35. os.system('stty sane')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement