Pārlūkot izejas kodu

Dateien hochladen nach ''

Yang Zhang 5 gadi atpakaļ
vecāks
revīzija
9941de5fad
2 mainītis faili ar 225 papildinājumiem un 0 dzēšanām
  1. 225 0
      Alarmsystem_final_edition.py
  2. BIN
      Berufspraktikum_Alarmsystem.pdf

+ 225 - 0
Alarmsystem_final_edition.py

@@ -0,0 +1,225 @@
+import time
+import Adafruit_BBIO.GPIO as GPIO
+import Adafruit_BBIO.PWM as PWM
+
+alarm = None
+
+class Keypad():
+    def __init__(self):
+        self.KEYPAD = [
+            ["1","2","3"],
+            ["4","5","6"],
+            ["7","8","9"],
+            ["*","0","#"]
+        ]
+
+        self.ROW         = ["P8_8", "P8_10", "P8_12", "P8_14"]
+        self.COLUMN      = ["P8_16", "P8_17", "P8_18"]
+        self.PASS = "1234"
+
+    def getKey(self):
+        # Set all columns as output low
+        for i in range(len(self.COLUMN)):
+            GPIO.setup(self.COLUMN[i], GPIO.OUT)
+            GPIO.output(self.COLUMN[i], GPIO.LOW)
+
+        # Set all rows as input
+        for i in range(len(self.ROW)):
+            GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
+
+        # Scan rows for pushed key/button
+        # A valid key press should set "rowVal"  between 0 and 3.
+        rowVal = -1
+        for i in range(len(self.ROW)):
+            tmpRead = GPIO.input(self.ROW[i])
+            if tmpRead == 0:
+                rowVal = i
+
+        if rowVal <0 or rowVal >3:
+            self.reset()
+            return
+
+        # Convert columns to input
+        for j in range(len(self.COLUMN)):
+            GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP)
+
+        # Switch the i-th row found from scan to output
+        GPIO.setup(self.ROW[rowVal], GPIO.OUT)
+        GPIO.output(self.ROW[rowVal], GPIO.LOW)
+
+        # Scan columns for still-pushed key/button
+        # A valid key press should set "colVal"  between 0 and 2.
+        colVal = -1
+        for j in range(len(self.COLUMN)):
+            tmpRead = GPIO.input(self.COLUMN[j])
+            if tmpRead == 0:
+                colVal=j
+
+        if colVal <0 or colVal >2:
+            self.reset()
+            return
+
+        # Return the value of the key pressed
+        self.reset()
+        return self.KEYPAD[rowVal][colVal]
+
+    def reset(self):
+        # Reinitialize all rows and columns
+        for i in range(len(self.ROW)):
+            GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
+        for j in range(len(self.COLUMN)):
+            GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP)
+
+    def verify_pass(self, pass_entered):
+        return pass_entered == self.PASS
+
+class IR:
+    def __init__(self, pin):         # einrichten IR Sensor Element(pin, mode,callback function, Verzögerungszeit)
+        self.pin = pin
+        GPIO.setup(self.pin, GPIO.IN)
+        GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.cb_ir, bouncetime=50)
+
+    def __del__(self): #Elemente lösen
+        print("IR {} deleted.".format(self.pin))
+        GPIO.remove_event_detect(self.pin)
+
+    def cb_ir(self, pin): #in globale Variable, wenn anderer Sensor schon detektiert hat, start dieser Sensor nicht
+        global alarm
+        print("cb_ir called.")
+        if not alarm.triggered:
+            alarm.trigger()
+
+class PIR:       # Wie IR Sensor
+    def __init__(self, pin):
+        self.pin = pin
+        GPIO.setup(self.pin, GPIO.IN)
+        GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.cb_pir, bouncetime=50)
+
+    def __del__(self):
+        print("PIR {} deleted.".format(self.pin))
+        GPIO.remove_event_detect(self.pin)
+
+    def cb_pir(self, pin):
+        global alarm
+        print("cb_pir called")
+        if not alarm.triggered:
+            alarm.trigger()
+
+class MAG:   # Zwei Zustand fenster zu und an, Anfangswert sollte mit Zu sein
+    def __init__(self, pin):
+        self.pin = pin
+        self.window_closed = True
+        GPIO.setup(self.pin, GPIO.IN)
+        GPIO.add_event_detect(self.pin, GPIO.BOTH, callback=self.cb_mag, bouncetime=50)
+
+    def __del__(self): # delete Elemente
+        print("MAG {} deleted.".format(self.pin))
+        GPIO.remove_event_detect(self.pin)
+
+    def cb_mag(self, pin):
+        global alarm
+        if self.window_closed:
+            print("Fenster geoeffnet...")
+            self.window_closed = False
+            if not alarm.triggered:
+                alarm.trigger()
+        else:
+            print("Fenster zu...")
+            self.window_closed = True
+
+class LAUT:   # Variable
+    def __init__(self, pin, pwm_perc=50, pwm_freq=2000):
+        self.pin = pin
+        self.pwm_perc = pwm_perc
+        self.pwm_freq = pwm_freq
+        GPIO.setup(self.pin, GPIO.OUT)
+
+    def __del__(self):
+        print("PWM {} deleted.".format(self.pin))
+
+    def start(self):
+        PWM.start(self.pin, self.pwm_perc, self.pwm_freq)
+
+    def stop(self):
+        PWM.stop(self.pin)
+
+class ROT(): #ähnlich wie Lautsprecher
+    def __init__(self, pin, pwm_perc=50, pwm_freq=1):
+        self.pin = pin
+        self.pwm_perc = pwm_perc
+        self.pwm_freq = pwm_freq
+
+    def __del__(self):
+        print("ROT {} deleted.".format(self.pin))
+
+    def start(self):
+        PWM.start(self.pin, self.pwm_perc, self.pwm_freq)
+
+    def stop(self):
+        PWM.stop(self.pin)
+
+class ALARM:
+    def __init__(self, rot, laut, keypad):
+        self.triggered = False   # irgendeiner detektierte Sensor
+        self.rot = rot
+        self.laut = laut
+        self.keypad = keypad
+
+    def trigger(self):
+        self.rot.start()
+        time.sleep(1)          # Hardware hat niedrige Qualität, muss mit sleeptime warten
+        self.laut.start()
+        time.sleep(1)
+        print("pls enter pass to stop alarm")
+        start_time = time.time()  #Zeile 199
+        pass_entered = ""
+        digit = None
+        while True:
+            while not digit:
+                digit = self.keypad.getKey()   # überprüfen, ob die Taste gedrückt hat
+            pass_entered = pass_entered + digit   #Wenn Ja, digit wurde als ein Zahl hinzufügen, 'pass_entered' ist ein string(Zeichenkette字符串)
+            print("{} digit(s) already entered".format(len(pass_entered))) # zeigen, wieviele digit schon eingeben hat
+            digit = None   # nach eingeben, reset digit als None
+            if len(pass_entered) >= 4:  # wenn im besonderen Fall, mehr als 4 Zahle eingetastet hat, nutzt man die lezteten 4 Zahlen
+                if self.keypad.verify_pass(pass_entered[-4:]):
+                    print("alarm cancelled")
+                    time.sleep(1)
+                    self.rot.stop()
+                    time.sleep(1)
+                    self.laut.stop()
+                    time.sleep(1)
+                    self.triggered = False   # False meint, nachdem korrekt Eingabe,sollte ausgelöste Variable zurücksetzen
+                    return
+                else:
+                    print("wrong password")
+                pass_entered = ""
+            if time.time() - start_time > 50:   # du hast jeder mal nur maximale 50 Sekunden, um Passwort einzugeben
+                print("took too long, reset")
+                pass_entered = ""
+            if time.time() - start_time > 60:  # du hast insgesamt von maxi. 60 Sekenden, um Passwort einzugeben
+                print("60s passed, quit for test")
+                time.sleep(1)
+                self.rot.stop()
+                time.sleep(1)
+                self.laut.stop()
+                time.sleep(1)
+                self.triggered = False
+                return
+
+if __name__ == "__main__":    #durchführen von allen Sensor und Ausgabenelementen
+    global alarm
+    rot = ROT("P9_14")
+    laut = LAUT("P8_13")
+    keypad = Keypad()
+    alarm = ALARM(rot, laut, keypad)
+    ir = IR("P9_12")
+    pir = PIR("P9_41")
+    mag = MAG("P8_7")
+    try:
+        while True:
+            print("...")
+            time.sleep(2)
+    except Exception as e:     #zurücksetzen diesen zwei typ pin
+        GPIO.cleanup()
+        PWM.cleanup()
+        print(e)

BIN
Berufspraktikum_Alarmsystem.pdf