final_new_keypad_bad.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import time
  2. import Adafruit_BBIO.GPIO as GPIO
  3. import Adafruit_BBIO.PWM as PWM
  4. class Keypad():
  5. def __init__(self):
  6. # CONSTANTS
  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. for r in self.ROW:
  17. GPIO.setup(r, GPIO.OUT)
  18. for c in self.COLUMN:
  19. GPIO.setup(c, GPIO.IN)
  20. def getKey(self):
  21. for i in self.ROW:
  22. GPIO.output(i, GPIO.HIGH)
  23. for c in self.COLUMN:
  24. GPIO.add_event_detect(c, GPIO.RISING)
  25. time.sleep(1)
  26. print("pls enter key")
  27. while True:
  28. for c in self.COLUMN:
  29. if GPIO.event_detected(c):
  30. key = self.identifyKey(c)
  31. if key:
  32. for c in self.COLUMN:
  33. GPIO.remove_event_detect(c)
  34. return key
  35. print("refreshing")
  36. time.sleep(0.1)
  37. def identifyKey(self, pin):
  38. print("identifyKey called")
  39. col = COLUMN.index(pin)
  40. key = None
  41. # GPIO.output(self.ROW[0], GPIO.HIGH)
  42. GPIO.output(self.ROW[1], GPIO.LOW)
  43. GPIO.output(self.ROW[2], GPIO.LOW)
  44. GPIO.output(self.ROW[3], GPIO.LOW)
  45. if GPIO.input(pin):
  46. key=self.KEYPAD[0][col]
  47. GPIO.output(self.ROW[0], GPIO.LOW)
  48. GPIO.output(self.ROW[1], GPIO.HIGH)
  49. # GPIO.output(self.ROW[2], GPIO.LOW)
  50. # GPIO.output(self.ROW[3], GPIO.LOW)
  51. if GPIO.input(pin):
  52. key=KEYPAD[1][col]
  53. # GPIO.output(self.ROW[0], GPIO.LOW)
  54. GPIO.output(self.ROW[1], GPIO.LOW)
  55. GPIO.output(self.ROW[2], GPIO.HIGH)
  56. # GPIO.output(self.ROW[3], GPIO.LOW)
  57. if GPIO.input(pin):
  58. key=KEYPAD[2][col]
  59. # GPIO.output(self.ROW[0], GPIO.LOW)
  60. # GPIO.output(self.ROW[1], GPIO.LOW)
  61. GPIO.output(self.ROW[2], GPIO.LOW)
  62. GPIO.output(self.ROW[3], GPIO.HIGH)
  63. if GPIO.input(pin):
  64. key=KEYPAD[3][col]
  65. GPIO.output(self.ROW[0], GPIO.HIGH)
  66. GPIO.output(self.ROW[1], GPIO.HIGH)
  67. GPIO.output(self.ROW[2], GPIO.HIGH)
  68. GPIO.output(self.ROW[3], GPIO.HIGH)
  69. if key:
  70. return key
  71. def verify_pass(self, pass_entered):
  72. return pass_entered == self.PASS
  73. class IR:
  74. def __init__(self, pin, rot, laut, keypad):
  75. self.pin = pin
  76. self.rot = rot
  77. self.laut = laut
  78. self.keypad = keypad
  79. GPIO.setup(self.pin, GPIO.IN)
  80. GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.cb_ir)
  81. def __del__(self):
  82. print("IR {} deleted.".format(self.pin))
  83. GPIO.remove_event_detect(self.pin)
  84. def cb_ir(self, pin):
  85. print("cb_ir called.")
  86. self.rot.start()
  87. time.sleep(1)
  88. self.laut.start()
  89. time.sleep(1)
  90. print("pls enter pass to stop alarm")
  91. start_time = time.time()
  92. pass_entered = ""
  93. digit = None
  94. while True:
  95. while not digit and time.time()-start_time < 50:
  96. digit = self.keypad.getKey()
  97. if digit:
  98. pass_entered = pass_entered + digit
  99. print("{} digit(s) already entered".format(len(pass_entered)))
  100. digit = None
  101. if len(pass_entered) >= 4:
  102. if self.keypad.verify_pass(pass_entered[-4:]):
  103. print("alarm cancelled")
  104. time.sleep(1)
  105. self.rot.stop()
  106. time.sleep(1)
  107. self.laut.stop()
  108. time.sleep(1)
  109. else:
  110. print("wrong password")
  111. pass_entered = ""
  112. if time.time() - start_time > 50:
  113. print("took too long, reset")
  114. pass_entered = ""
  115. if time.time() - start_time > 60:
  116. print("60s passed, quit for test")
  117. time.sleep(1)
  118. self.rot.stop()
  119. time.sleep(1)
  120. self.laut.stop()
  121. time.sleep(1)
  122. break
  123. class LAUT:
  124. def __init__(self, pin, pwm_perc=50, pwm_freq=10000):
  125. self.pin = pin
  126. self.pwm_perc = pwm_perc
  127. self.pwm_freq = pwm_freq
  128. GPIO.setup(self.pin, GPIO.OUT)
  129. def __del__(self):
  130. print("PWM {} deleted.".format(self.pin))
  131. def start(self):
  132. PWM.start(self.pin, self.pwm_perc, self.pwm_freq)
  133. def stop(self):
  134. PWM.stop(self.pin)
  135. class ROT():
  136. def __init__(self, pin, pwm_perc=50, pwm_freq=1):
  137. self.pin = pin
  138. self.pwm_perc = pwm_perc
  139. self.pwm_freq = pwm_freq
  140. def __del__(self):
  141. print("ROT {} deleted.".format(self.pin))
  142. def start(self):
  143. PWM.start(self.pin, self.pwm_perc, self.pwm_freq)
  144. def stop(self):
  145. PWM.stop(self.pin)
  146. if __name__ == "__main__":
  147. rot = ROT("P9_14")
  148. laut = LAUT("P8_13")
  149. keypad = Keypad()
  150. ir = IR("P9_12", rot, laut, keypad)
  151. try:
  152. while True:
  153. print("...")
  154. time.sleep(2)
  155. except Exception as e:
  156. GPIO.cleanup()
  157. PWM.cleanup()
  158. print(e)