|
@@ -0,0 +1,77 @@
|
|
|
+import time
|
|
|
+import Adafruit_BBIO.GPIO as GPIO
|
|
|
+import Adafruit_BBIO.PWM as PWM
|
|
|
+
|
|
|
+class alarm:
|
|
|
+ _alarm_duration = 60 # in second
|
|
|
+ _alarm_pwm_perc = 50 # in percentage
|
|
|
+ _alarm_pwm_freq = 10000 # in hz
|
|
|
+ _alarm_led_freq = 2 # in hz
|
|
|
+ _config = {}
|
|
|
+ _pins_to_watch = {} # { pname: pin }
|
|
|
+ _window_closed = True
|
|
|
+
|
|
|
+ def __init__(self, config):
|
|
|
+ self._config = config
|
|
|
+
|
|
|
+ for pname,pconfig in self._config.items():
|
|
|
+ if "mode" in pconfig:
|
|
|
+ GPIO.setup(pconfig["pin"], pconfig["mode"])
|
|
|
+ if "event_to_detect" in pconfig:
|
|
|
+ GPIO.add_event_detect(pconfig["pin"], pconfig["event_to_detect"])
|
|
|
+ self._pins_to_watch[pname] = pconfig["pin"]
|
|
|
+
|
|
|
+ def trigger_alarm(self, pled, ppwm):
|
|
|
+ for i in range(0, self._alarm_duration):
|
|
|
+ PWM.start(self._config[ppwm]["pin"], self._alarm_pwm_perc, self._alarm_pwm_freq)
|
|
|
+ GPIO.output(self._config[pled]["pin"],GPIO.HIGH)
|
|
|
+ time.sleep(1/self._alarm_led_freq)
|
|
|
+ GPIO.output(self._config[pled]["pin"],GPIO.LOW)
|
|
|
+ time.sleep(1/self._alarm_led_freq)
|
|
|
+ PWM.stop(self._config[ppwm]["pin"])
|
|
|
+ GPIO.output(self._config[pled]["pin"],GPIO.HIGH)
|
|
|
+ time.sleep(1/self._alarm_led_freq)
|
|
|
+ GPIO.output(self._config[pled]["pin"],GPIO.LOW)
|
|
|
+ time.sleep(1/self._alarm_led_freq)
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ my_config = {
|
|
|
+ "inPinIR": {"pin": "P9_12", "mode": GPIO.IN, "event_to_detect": GPIO.RISING},
|
|
|
+ "inPinPIR": {"pin": "P9_41", "mode": GPIO.IN, "event_to_detect": GPIO.RISING},
|
|
|
+ "inPinMAG": {"pin": "P8_7", "mode": GPIO.IN, "event_to_detect": GPIO.BOTH},
|
|
|
+ "outPinROT": {"pin": "P9_27", "mode": GPIO.OUT},
|
|
|
+ "myPWMLAUT": {"pin":"P8_13"}
|
|
|
+ }
|
|
|
+ try:
|
|
|
+ print("Program start. Initializing with alarm config...")
|
|
|
+ my_alarm = alarm(config=my_config)
|
|
|
+ print("Pins to watch:")
|
|
|
+ print(my_alarm._pins_to_watch)
|
|
|
+ print("Initialization succeeded. Start in 2 seconds.")
|
|
|
+ time.sleep(2)
|
|
|
+ while True:
|
|
|
+ for pname,pin in my_alarm._pins_to_watch.items():
|
|
|
+ if GPIO.event_detected(pin):
|
|
|
+ print("Motion detected from {}".format(pname))
|
|
|
+ if pname == "inPinMAG": # Fenster
|
|
|
+ if my_alarm._window_closed:
|
|
|
+ print("Fenster geoeffnet...")
|
|
|
+ my_alarm._window_closed = False
|
|
|
+ my_alarm.trigger_alarm(pled="outPinROT", ppwm="myPWMLAUT")
|
|
|
+ else: # open
|
|
|
+ print("Fenster zu...")
|
|
|
+ my_alarm._window_closed = True
|
|
|
+ continue
|
|
|
+ if pname == "inPinIR":
|
|
|
+ my_alarm.trigger_alarm(pled="outPinROT", ppwm="myPWMLAUT")
|
|
|
+ continue
|
|
|
+ if pname == "inPinPIR":
|
|
|
+ my_alarm.trigger_alarm(pled="outPinROT", ppwm="myPWMLAUT")
|
|
|
+ continue
|
|
|
+ print("...")
|
|
|
+ time.sleep(2)
|
|
|
+ except Exception as e:
|
|
|
+ print("Exception captured. Clean up and quit...")
|
|
|
+ GPIO.cleanup()
|
|
|
+ PWM.cleanup()
|
|
|
+ print(e)
|