Procházet zdrojové kódy

Dateien hochladen nach ''

Yang Zhang před 5 roky
rodič
revize
30ab3bf050
2 změnil soubory, kde provedl 247 přidání a 0 odebrání
  1. 91 0
      Tastenfeld.py
  2. 156 0
      alarm_with_callback_ohne_Tast.py

+ 91 - 0
Tastenfeld.py

@@ -0,0 +1,91 @@
+import Adafruit_BBIO.GPIO as GPIO
+import time 
+import sys
+
+KEYPAD = [
+    ["1","2","3"],
+    ["4","5","6"],
+    ["7","8","9"],
+    ["*","0","#"]
+]
+
+ROW         = ["P8_8", "P8_10", "P8_12", "P8_14"]
+COLUMN      = ["P8_16", "P8_17", "P8_18"]
+
+pin_r1 = "P8_8"
+pin_r2 = "P8_10"
+pin_r3 = "P8_12"
+pin_r4 = "P8_14"
+
+pin_c1 = "P8_16"
+pin_c2 = "P8_17"
+pin_c3 = "P8_18"
+
+GPIO.setup(pin_r1, GPIO.OUT)
+GPIO.setup(pin_r2, GPIO.OUT)
+GPIO.setup(pin_r3, GPIO.OUT)
+GPIO.setup(pin_r4, GPIO.OUT)
+GPIO.setup(pin_c1, GPIO.IN)
+GPIO.setup(pin_c2, GPIO.IN)
+GPIO.setup(pin_c3, GPIO.IN)
+
+def cb_keypressed(pin):
+    col = COLUMN.index(pin)
+    key = None
+
+    # GPIO.output(pin_r1, GPIO.HIGH)
+    GPIO.output(pin_r2, GPIO.LOW)
+    GPIO.output(pin_r3, GPIO.LOW)
+    GPIO.output(pin_r4, GPIO.LOW)
+    if GPIO.input(pin):
+        key=KEYPAD[0][col]
+
+    GPIO.output(pin_r1, GPIO.LOW)
+    GPIO.output(pin_r2, GPIO.HIGH)
+    # GPIO.output(pin_r3, GPIO.LOW)
+    # GPIO.output(pin_r4, GPIO.LOW)
+    if GPIO.input(pin):
+        key=KEYPAD[1][col]
+    
+
+    # GPIO.output(pin_r1, GPIO.LOW)
+    GPIO.output(pin_r2, GPIO.LOW)
+    GPIO.output(pin_r3, GPIO.HIGH)
+    # GPIO.output(pin_r4, GPIO.LOW)
+    if GPIO.input(pin):
+        key=KEYPAD[2][col]
+
+    # GPIO.output(pin_r1, GPIO.LOW)
+    # GPIO.output(pin_r2, GPIO.LOW)
+    GPIO.output(pin_r3, GPIO.LOW)
+    GPIO.output(pin_r4, GPIO.HIGH)
+    if GPIO.input(pin):
+        key=KEYPAD[3][col]
+
+    if key:
+        print(key)
+    
+    GPIO.output(pin_r1, GPIO.HIGH)
+    GPIO.output(pin_r2, GPIO.HIGH)
+    GPIO.output(pin_r3, GPIO.HIGH)
+    GPIO.output(pin_r4, GPIO.HIGH)
+
+GPIO.add_event_detect(pin_c1, GPIO.RISING, callback=cb_keypressed, bouncetime=50)
+GPIO.add_event_detect(pin_c2, GPIO.RISING, callback=cb_keypressed, bouncetime=50)
+GPIO.add_event_detect(pin_c3, GPIO.RISING, callback=cb_keypressed, bouncetime=50)
+    
+try:
+    GPIO.output(pin_r1, GPIO.HIGH)
+    GPIO.output(pin_r2, GPIO.HIGH)
+    GPIO.output(pin_r3, GPIO.HIGH)
+    GPIO.output(pin_r4, GPIO.HIGH)
+    time.sleep(1)
+    print("...")
+    while True:
+        time.sleep(2)
+        print("...")
+
+except Exception as e:
+    print("Exception captured. Clean up and quit...")
+    GPIO.cleanup()
+    print(e)

+ 156 - 0
alarm_with_callback_ohne_Tast.py

@@ -0,0 +1,156 @@
+import time
+import Adafruit_BBIO.GPIO as GPIO
+import Adafruit_BBIO.PWM as PWM
+
+
+
+class IR:
+    _name = ""
+    _pin = ""
+    _mode = GPIO.IN
+    _rot = None
+    _laut = None
+    def __init__(self, name, pin, rot, laut):
+        self._name = name
+        self._pin = pin
+        self._rot = rot
+        self._laut = laut
+        GPIO.setup(self._pin, self._mode)
+        GPIO.add_event_detect(self._pin, GPIO.RISING, callback=self.cb_ir)
+    
+    def __del__(self):
+        print("IR {} deleted.".format(self._name))
+        GPIO.remove_event_detect(self._pin)
+
+    def cb_ir(self, pin):
+        print("cb_ir called")
+        for i in range(self._laut._duration * self._rot._blink_freq):
+            self._laut.start()
+            self._rot.start()
+            time.sleep(1/self._rot._blink_freq)
+            self._rot.stop()
+            self._laut.stop()
+
+class PIR:
+    _name = ""
+    _pin = ""
+    _mode = GPIO.IN
+    _rot = None
+    _laut = None
+    def __init__(self, name, pin, rot, laut):
+        self._name = name
+        self._pin = pin
+        self._rot = rot
+        self._laut = laut
+        GPIO.setup(self._pin, self._mode)
+        GPIO.add_event_detect(self._pin, GPIO.RISING, callback=self.cb_pir)
+    
+    def __del__(self):
+        print("PIR {} deleted.".format(self._name))
+        GPIO.remove_event_detect(self._pin)
+
+    def cb_pir(self, pin):
+        print("cb_pir called")
+        for i in range(self._laut._duration * self._rot._blink_freq):
+            self._laut.start()
+            self._rot.start()
+            time.sleep(1/self._rot._blink_freq)
+            self._rot.stop()
+            self._laut.stop()
+        
+class MAG:
+    _name = ""
+    _pin = ""
+    _mode = GPIO.IN
+    _window_closed = True
+    _rot = None
+    _laut = None
+    def __init__(self, name, pin, rot, laut):
+        self._name = name
+        self._pin = pin
+        self._rot = rot
+        self._laut = laut
+        GPIO.setup(self._pin, self._mode)
+        GPIO.add_event_detect(self._pin, GPIO.BOTH, callback=self.cb_mag)
+    
+    def __del__(self):
+        print("MAG {} deleted.".format(self._name))
+        GPIO.remove_event_detect(self._pin)
+
+    def cb_mag(self, pin):
+        if self._window_closed:
+            print("Fenster geoeffnet...")
+            self._window_closed = False
+            for i in range(self._laut._duration * self._rot._blink_freq):
+                self._laut.start()
+                self._rot.start()
+                time.sleep(1/self._rot._blink_freq)
+                self._rot.stop()
+                self._laut.stop()
+        else:
+            print("Fenster zu...")
+            self._window_closed = True
+
+class ROT:
+    _name = ""
+    _pin = ""
+    _mode = GPIO.OUT
+    _blink_freq = 2
+    def __init__(self, name, pin, blink_freq=2):
+        self._name = name
+        self._pin = pin
+        self._blink_freq = blink_freq
+        GPIO.setup(self._pin, self._mode)
+    
+    def __del__(self):
+        print("ROT {} deleted.".format(self._name))
+
+    def start(self):
+        GPIO.output(self._pin, GPIO.HIGH)
+    
+    def stop(self):
+        GPIO.output(self._pin, GPIO.LOW)
+
+class LAUT:
+    _name = ""
+    _pin = ""
+    _mode = GPIO.OUT
+    _duration = 60 # in second
+    _pwm_perc = 50 # in percentage
+    _pwm_freq = 10000 # in hz
+    def __init__(self, name, pin, duration=60, pwm_perc=50, pwm_freq=10000):
+        self._name = name
+        self._pin = pin
+        self._duration = duration
+        self._pwm_perc = pwm_perc
+        self._pwn_freq = pwm_freq
+        GPIO.setup(self._pin, self._mode)
+    
+    def __del__(self):
+        print("PWM {} deleted.".format(self._name))
+
+    def start(self):
+        PWM.start(self._pin, self._pwm_perc, self._pwm_freq)
+    
+    def stop(self):
+        PWM.stop(self._pin)
+
+if __name__ == "__main__":
+    try:
+        print("Program start. Initializing with alarm config...")
+        rot = ROT("outPinROT", "P9_27")
+        laut = LAUT("myPWMLAUT", "P8_13", duration=4)
+        ir = IR("inPinIR", "P9_12", rot, laut)
+        pir = PIR("inPinPIR", "P9_41", rot, laut)
+        mag = MAG("inPinMAG", "P8_7", rot, laut)
+
+        print("Initialization succeeded. Start in 2 seconds.")
+        time.sleep(2)
+        while True:
+            print("...")
+            time.sleep(2)
+    except Exception as e:
+        print("Exception captured. Clean up and quit...")
+        GPIO.cleanup()
+        PWM.cleanup()
+        print(e)