Alarmsystem_final_edition.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import time
  2. import Adafruit_BBIO.GPIO as GPIO
  3. import Adafruit_BBIO.PWM as PWM
  4. alarm = None
  5. class Keypad():
  6. def __init__(self):
  7. self.KEYPAD = [
  8. ["1","2","3"],
  9. ["4","5","6"],
  10. ["7","8","9"],
  11. ["*","0","#"]
  12. ]
  13. self.ROW = ["P8_8", "P8_10", "P8_12", "P8_14"]
  14. self.COLUMN = ["P8_16", "P8_17", "P8_18"]
  15. self.PASS = "1234"
  16. def getKey(self):
  17. # Set all columns as output low
  18. for i in range(len(self.COLUMN)):
  19. GPIO.setup(self.COLUMN[i], GPIO.OUT)
  20. GPIO.output(self.COLUMN[i], GPIO.LOW)
  21. # Set all rows as input
  22. for i in range(len(self.ROW)):
  23. GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  24. # Scan rows for pushed key/button
  25. # A valid key press should set "rowVal" between 0 and 3.
  26. rowVal = -1
  27. for i in range(len(self.ROW)):
  28. tmpRead = GPIO.input(self.ROW[i])
  29. if tmpRead == 0:
  30. rowVal = i
  31. if rowVal <0 or rowVal >3:
  32. self.reset()
  33. return
  34. # Convert columns to input
  35. for j in range(len(self.COLUMN)):
  36. GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  37. # Switch the i-th row found from scan to output
  38. GPIO.setup(self.ROW[rowVal], GPIO.OUT)
  39. GPIO.output(self.ROW[rowVal], GPIO.LOW)
  40. # Scan columns for still-pushed key/button
  41. # A valid key press should set "colVal" between 0 and 2.
  42. colVal = -1
  43. for j in range(len(self.COLUMN)):
  44. tmpRead = GPIO.input(self.COLUMN[j])
  45. if tmpRead == 0:
  46. colVal=j
  47. if colVal <0 or colVal >2:
  48. self.reset()
  49. return
  50. # Return the value of the key pressed
  51. self.reset()
  52. return self.KEYPAD[rowVal][colVal]
  53. def reset(self):
  54. # Reinitialize all rows and columns
  55. for i in range(len(self.ROW)):
  56. GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  57. for j in range(len(self.COLUMN)):
  58. GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  59. def verify_pass(self, pass_entered):
  60. return pass_entered == self.PASS
  61. class IR:
  62. def __init__(self, pin): # einrichten IR Sensor Element(pin, mode,callback function, Verzögerungszeit)
  63. self.pin = pin
  64. GPIO.setup(self.pin, GPIO.IN)
  65. GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.cb_ir, bouncetime=50)
  66. def __del__(self): #Elemente lösen
  67. print("IR {} deleted.".format(self.pin))
  68. GPIO.remove_event_detect(self.pin)
  69. def cb_ir(self, pin): #in globale Variable, wenn anderer Sensor schon detektiert hat, start dieser Sensor nicht
  70. global alarm
  71. print("cb_ir called.")
  72. if not alarm.triggered:
  73. alarm.trigger()
  74. class PIR: # Wie IR Sensor
  75. def __init__(self, pin):
  76. self.pin = pin
  77. GPIO.setup(self.pin, GPIO.IN)
  78. GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.cb_pir, bouncetime=50)
  79. def __del__(self):
  80. print("PIR {} deleted.".format(self.pin))
  81. GPIO.remove_event_detect(self.pin)
  82. def cb_pir(self, pin):
  83. global alarm
  84. print("cb_pir called")
  85. if not alarm.triggered:
  86. alarm.trigger()
  87. class MAG: # Zwei Zustand fenster zu und an, Anfangswert sollte mit Zu sein
  88. def __init__(self, pin):
  89. self.pin = pin
  90. self.window_closed = True
  91. GPIO.setup(self.pin, GPIO.IN)
  92. GPIO.add_event_detect(self.pin, GPIO.BOTH, callback=self.cb_mag, bouncetime=50)
  93. def __del__(self): # delete Elemente
  94. print("MAG {} deleted.".format(self.pin))
  95. GPIO.remove_event_detect(self.pin)
  96. def cb_mag(self, pin):
  97. global alarm
  98. if self.window_closed:
  99. print("Fenster geoeffnet...")
  100. self.window_closed = False
  101. if not alarm.triggered:
  102. alarm.trigger()
  103. else:
  104. print("Fenster zu...")
  105. self.window_closed = True
  106. class LAUT: # Variable
  107. def __init__(self, pin, pwm_perc=50, pwm_freq=2000):
  108. self.pin = pin
  109. self.pwm_perc = pwm_perc
  110. self.pwm_freq = pwm_freq
  111. GPIO.setup(self.pin, GPIO.OUT)
  112. def __del__(self):
  113. print("PWM {} deleted.".format(self.pin))
  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. class ROT(): #ähnlich wie Lautsprecher
  119. def __init__(self, pin, pwm_perc=50, pwm_freq=1):
  120. self.pin = pin
  121. self.pwm_perc = pwm_perc
  122. self.pwm_freq = pwm_freq
  123. def __del__(self):
  124. print("ROT {} deleted.".format(self.pin))
  125. def start(self):
  126. PWM.start(self.pin, self.pwm_perc, self.pwm_freq)
  127. def stop(self):
  128. PWM.stop(self.pin)
  129. class ALARM:
  130. def __init__(self, rot, laut, keypad):
  131. self.triggered = False # irgendeiner detektierte Sensor
  132. self.rot = rot
  133. self.laut = laut
  134. self.keypad = keypad
  135. def trigger(self):
  136. self.rot.start()
  137. time.sleep(1) # Hardware hat niedrige Qualität, muss mit sleeptime warten
  138. self.laut.start()
  139. time.sleep(1)
  140. print("pls enter pass to stop alarm")
  141. start_time = time.time() #Zeile 199
  142. pass_entered = ""
  143. digit = None
  144. while True:
  145. while not digit:
  146. digit = self.keypad.getKey() # überprüfen, ob die Taste gedrückt hat
  147. pass_entered = pass_entered + digit #Wenn Ja, digit wurde als ein Zahl hinzufügen, 'pass_entered' ist ein string(Zeichenkette字符串)
  148. print("{} digit(s) already entered".format(len(pass_entered))) # zeigen, wieviele digit schon eingeben hat
  149. digit = None # nach eingeben, reset digit als None
  150. if len(pass_entered) >= 4: # wenn im besonderen Fall, mehr als 4 Zahle eingetastet hat, nutzt man die lezteten 4 Zahlen
  151. if self.keypad.verify_pass(pass_entered[-4:]):
  152. print("alarm cancelled")
  153. time.sleep(1)
  154. self.rot.stop()
  155. time.sleep(1)
  156. self.laut.stop()
  157. time.sleep(1)
  158. self.triggered = False # False meint, nachdem korrekt Eingabe,sollte ausgelöste Variable zurücksetzen
  159. return
  160. else:
  161. print("wrong password")
  162. pass_entered = ""
  163. if time.time() - start_time > 50: # du hast jeder mal nur maximale 50 Sekunden, um Passwort einzugeben
  164. print("took too long, reset")
  165. pass_entered = ""
  166. if time.time() - start_time > 60: # du hast insgesamt von maxi. 60 Sekenden, um Passwort einzugeben
  167. print("60s passed, quit for test")
  168. time.sleep(1)
  169. self.rot.stop()
  170. time.sleep(1)
  171. self.laut.stop()
  172. time.sleep(1)
  173. self.triggered = False
  174. return
  175. if __name__ == "__main__": #durchführen von allen Sensor und Ausgabenelementen
  176. global alarm
  177. rot = ROT("P9_14")
  178. laut = LAUT("P8_13")
  179. keypad = Keypad()
  180. alarm = ALARM(rot, laut, keypad)
  181. ir = IR("P9_12")
  182. pir = PIR("P9_41")
  183. mag = MAG("P8_7")
  184. try:
  185. while True:
  186. print("...")
  187. time.sleep(2)
  188. except Exception as e: #zurücksetzen diesen zwei typ pin
  189. GPIO.cleanup()
  190. PWM.cleanup()
  191. print(e)