final_old_keypad_good.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. 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, rot, laut, keypad):
  63. self.pin = pin
  64. self.rot = rot
  65. self.laut = laut
  66. self.keypad = keypad
  67. GPIO.setup(self.pin, GPIO.IN)
  68. GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.cb_ir)
  69. def __del__(self):
  70. print("IR {} deleted.".format(self.pin))
  71. GPIO.remove_event_detect(self.pin)
  72. def cb_ir(self, pin):
  73. print("cb_ir called.")
  74. self.rot.start()
  75. time.sleep(1)
  76. self.laut.start()
  77. time.sleep(1)
  78. print("pls enter pass to stop alarm")
  79. start_time = time.time()
  80. pass_entered = ""
  81. digit = None
  82. while True:
  83. while not digit and time.time()-start_time < 50:
  84. digit = self.keypad.getKey()
  85. pass_entered = pass_entered + digit
  86. print("{} digit(s) already entered".format(len(pass_entered)))
  87. digit = None
  88. if len(pass_entered) >= 4:
  89. if self.keypad.verify_pass(pass_entered[-4:]):
  90. print("alarm cancelled")
  91. time.sleep(1)
  92. self.rot.stop()
  93. time.sleep(1)
  94. self.laut.stop()
  95. time.sleep(1)
  96. else:
  97. print("wrong password")
  98. pass_entered = ""
  99. if time.time() - start_time > 50:
  100. print("took too long, reset")
  101. pass_entered = ""
  102. if time.time() - start_time > 60:
  103. print("60s passed, quit for test")
  104. time.sleep(1)
  105. self.rot.stop()
  106. time.sleep(1)
  107. self.laut.stop()
  108. time.sleep(1)
  109. break
  110. class LAUT:
  111. def __init__(self, pin, pwm_perc=50, pwm_freq=10000):
  112. self.pin = pin
  113. self.pwm_perc = pwm_perc
  114. self.pwm_freq = pwm_freq
  115. GPIO.setup(self.pin, GPIO.OUT)
  116. def __del__(self):
  117. print("PWM {} deleted.".format(self.pin))
  118. def start(self):
  119. PWM.start(self.pin, self.pwm_perc, self.pwm_freq)
  120. def stop(self):
  121. PWM.stop(self.pin)
  122. class ROT():
  123. def __init__(self, pin, pwm_perc=50, pwm_freq=1):
  124. self.pin = pin
  125. self.pwm_perc = pwm_perc
  126. self.pwm_freq = pwm_freq
  127. def __del__(self):
  128. print("ROT {} deleted.".format(self.pin))
  129. def start(self):
  130. PWM.start(self.pin, self.pwm_perc, self.pwm_freq)
  131. def stop(self):
  132. PWM.stop(self.pin)
  133. if __name__ == "__main__":
  134. rot = ROT("P9_14")
  135. laut = LAUT("P8_13")
  136. keypad = Keypad()
  137. ir = IR("P9_12", rot, laut, keypad)
  138. try:
  139. while True:
  140. print("...")
  141. time.sleep(2)
  142. except Exception as e:
  143. GPIO.cleanup()
  144. PWM.cleanup()
  145. print(e)