alarm_with_callback_ohne_Tast.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import time
  2. import Adafruit_BBIO.GPIO as GPIO
  3. import Adafruit_BBIO.PWM as PWM
  4. class IR:
  5. _name = ""
  6. _pin = ""
  7. _mode = GPIO.IN
  8. _rot = None
  9. _laut = None
  10. def __init__(self, name, pin, rot, laut):
  11. self._name = name
  12. self._pin = pin
  13. self._rot = rot
  14. self._laut = laut
  15. GPIO.setup(self._pin, self._mode)
  16. GPIO.add_event_detect(self._pin, GPIO.RISING, callback=self.cb_ir)
  17. def __del__(self):
  18. print("IR {} deleted.".format(self._name))
  19. GPIO.remove_event_detect(self._pin)
  20. def cb_ir(self, pin):
  21. print("cb_ir called")
  22. for i in range(self._laut._duration * self._rot._blink_freq):
  23. self._laut.start()
  24. self._rot.start()
  25. time.sleep(1/self._rot._blink_freq)
  26. self._rot.stop()
  27. self._laut.stop()
  28. class PIR:
  29. _name = ""
  30. _pin = ""
  31. _mode = GPIO.IN
  32. _rot = None
  33. _laut = None
  34. def __init__(self, name, pin, rot, laut):
  35. self._name = name
  36. self._pin = pin
  37. self._rot = rot
  38. self._laut = laut
  39. GPIO.setup(self._pin, self._mode)
  40. GPIO.add_event_detect(self._pin, GPIO.RISING, callback=self.cb_pir)
  41. def __del__(self):
  42. print("PIR {} deleted.".format(self._name))
  43. GPIO.remove_event_detect(self._pin)
  44. def cb_pir(self, pin):
  45. print("cb_pir called")
  46. for i in range(self._laut._duration * self._rot._blink_freq):
  47. self._laut.start()
  48. self._rot.start()
  49. time.sleep(1/self._rot._blink_freq)
  50. self._rot.stop()
  51. self._laut.stop()
  52. class MAG:
  53. _name = ""
  54. _pin = ""
  55. _mode = GPIO.IN
  56. _window_closed = True
  57. _rot = None
  58. _laut = None
  59. def __init__(self, name, pin, rot, laut):
  60. self._name = name
  61. self._pin = pin
  62. self._rot = rot
  63. self._laut = laut
  64. GPIO.setup(self._pin, self._mode)
  65. GPIO.add_event_detect(self._pin, GPIO.BOTH, callback=self.cb_mag)
  66. def __del__(self):
  67. print("MAG {} deleted.".format(self._name))
  68. GPIO.remove_event_detect(self._pin)
  69. def cb_mag(self, pin):
  70. if self._window_closed:
  71. print("Fenster geoeffnet...")
  72. self._window_closed = False
  73. for i in range(self._laut._duration * self._rot._blink_freq):
  74. self._laut.start()
  75. self._rot.start()
  76. time.sleep(1/self._rot._blink_freq)
  77. self._rot.stop()
  78. self._laut.stop()
  79. else:
  80. print("Fenster zu...")
  81. self._window_closed = True
  82. class ROT:
  83. _name = ""
  84. _pin = ""
  85. _mode = GPIO.OUT
  86. _blink_freq = 2
  87. def __init__(self, name, pin, blink_freq=2):
  88. self._name = name
  89. self._pin = pin
  90. self._blink_freq = blink_freq
  91. GPIO.setup(self._pin, self._mode)
  92. def __del__(self):
  93. print("ROT {} deleted.".format(self._name))
  94. def start(self):
  95. GPIO.output(self._pin, GPIO.HIGH)
  96. def stop(self):
  97. GPIO.output(self._pin, GPIO.LOW)
  98. class LAUT:
  99. _name = ""
  100. _pin = ""
  101. _mode = GPIO.OUT
  102. _duration = 60 # in second
  103. _pwm_perc = 50 # in percentage
  104. _pwm_freq = 10000 # in hz
  105. def __init__(self, name, pin, duration=60, pwm_perc=50, pwm_freq=10000):
  106. self._name = name
  107. self._pin = pin
  108. self._duration = duration
  109. self._pwm_perc = pwm_perc
  110. self._pwn_freq = pwm_freq
  111. GPIO.setup(self._pin, self._mode)
  112. def __del__(self):
  113. print("PWM {} deleted.".format(self._name))
  114. def start(self):
  115. PWM.start(self._pin, self._pwm_perc, self._pwm_freq)
  116. def stop(self):
  117. PWM.stop(self._pin)
  118. if __name__ == "__main__":
  119. try:
  120. print("Program start. Initializing with alarm config...")
  121. rot = ROT("outPinROT", "P9_27")
  122. laut = LAUT("myPWMLAUT", "P8_13", duration=4)
  123. ir = IR("inPinIR", "P9_12", rot, laut)
  124. pir = PIR("inPinPIR", "P9_41", rot, laut)
  125. mag = MAG("inPinMAG", "P8_7", rot, laut)
  126. print("Initialization succeeded. Start in 2 seconds.")
  127. time.sleep(2)
  128. while True:
  129. print("...")
  130. time.sleep(2)
  131. except Exception as e:
  132. print("Exception captured. Clean up and quit...")
  133. GPIO.cleanup()
  134. PWM.cleanup()
  135. print(e)