Tastenfeld.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import Adafruit_BBIO.GPIO as GPIO
  2. import time
  3. import sys
  4. KEYPAD = [
  5. ["1","2","3"],
  6. ["4","5","6"],
  7. ["7","8","9"],
  8. ["*","0","#"]
  9. ]
  10. ROW = ["P8_8", "P8_10", "P8_12", "P8_14"]
  11. COLUMN = ["P8_16", "P8_17", "P8_18"]
  12. pin_r1 = "P8_8"
  13. pin_r2 = "P8_10"
  14. pin_r3 = "P8_12"
  15. pin_r4 = "P8_14"
  16. pin_c1 = "P8_16"
  17. pin_c2 = "P8_17"
  18. pin_c3 = "P8_18"
  19. GPIO.setup(pin_r1, GPIO.OUT)
  20. GPIO.setup(pin_r2, GPIO.OUT)
  21. GPIO.setup(pin_r3, GPIO.OUT)
  22. GPIO.setup(pin_r4, GPIO.OUT)
  23. GPIO.setup(pin_c1, GPIO.IN)
  24. GPIO.setup(pin_c2, GPIO.IN)
  25. GPIO.setup(pin_c3, GPIO.IN)
  26. def cb_keypressed(pin):
  27. col = COLUMN.index(pin)
  28. key = None
  29. # GPIO.output(pin_r1, GPIO.HIGH)
  30. GPIO.output(pin_r2, GPIO.LOW)
  31. GPIO.output(pin_r3, GPIO.LOW)
  32. GPIO.output(pin_r4, GPIO.LOW)
  33. if GPIO.input(pin):
  34. key=KEYPAD[0][col]
  35. GPIO.output(pin_r1, GPIO.LOW)
  36. GPIO.output(pin_r2, GPIO.HIGH)
  37. # GPIO.output(pin_r3, GPIO.LOW)
  38. # GPIO.output(pin_r4, GPIO.LOW)
  39. if GPIO.input(pin):
  40. key=KEYPAD[1][col]
  41. # GPIO.output(pin_r1, GPIO.LOW)
  42. GPIO.output(pin_r2, GPIO.LOW)
  43. GPIO.output(pin_r3, GPIO.HIGH)
  44. # GPIO.output(pin_r4, GPIO.LOW)
  45. if GPIO.input(pin):
  46. key=KEYPAD[2][col]
  47. # GPIO.output(pin_r1, GPIO.LOW)
  48. # GPIO.output(pin_r2, GPIO.LOW)
  49. GPIO.output(pin_r3, GPIO.LOW)
  50. GPIO.output(pin_r4, GPIO.HIGH)
  51. if GPIO.input(pin):
  52. key=KEYPAD[3][col]
  53. if key:
  54. print(key)
  55. GPIO.output(pin_r1, GPIO.HIGH)
  56. GPIO.output(pin_r2, GPIO.HIGH)
  57. GPIO.output(pin_r3, GPIO.HIGH)
  58. GPIO.output(pin_r4, GPIO.HIGH)
  59. GPIO.add_event_detect(pin_c1, GPIO.RISING, callback=cb_keypressed, bouncetime=50)
  60. GPIO.add_event_detect(pin_c2, GPIO.RISING, callback=cb_keypressed, bouncetime=50)
  61. GPIO.add_event_detect(pin_c3, GPIO.RISING, callback=cb_keypressed, bouncetime=50)
  62. try:
  63. GPIO.output(pin_r1, GPIO.HIGH)
  64. GPIO.output(pin_r2, GPIO.HIGH)
  65. GPIO.output(pin_r3, GPIO.HIGH)
  66. GPIO.output(pin_r4, GPIO.HIGH)
  67. time.sleep(1)
  68. print("...")
  69. while True:
  70. time.sleep(2)
  71. print("...")
  72. except Exception as e:
  73. print("Exception captured. Clean up and quit...")
  74. GPIO.cleanup()
  75. print(e)