gpio_inpput_manager.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import RPi.GPIO as GPIO
  2. import logging
  3. import pygame
  4. logger = logging.getLogger(__name__)
  5. class GPIOManager():
  6. def __init__(self):
  7. GPIO.setmode(GPIO.BCM)
  8. # Left Button
  9. GPIO.setup(4, GPIO.IN, pull_up_down = GPIO.PUD_UP)
  10. GPIO.add_event_detect(4, GPIO.BOTH, callback=left, bouncetime=30)
  11. # Right Button
  12. GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP)
  13. GPIO.add_event_detect(21, GPIO.BOTH, callback=right, bouncetime=30)
  14. # Up Button
  15. GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_UP)
  16. GPIO.add_event_detect(22, GPIO.BOTH, callback=up, bouncetime=30)
  17. # Down Button
  18. GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
  19. GPIO.add_event_detect(23, GPIO.BOTH, callback=right, bouncetime=30)
  20. # Enter Button
  21. GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
  22. GPIO.add_event_detect(24, GPIO.BOTH, callback=right, bouncetime=30)
  23. def right(channel):
  24. dict = {}
  25. if GPIO.input(channel) == 1:
  26. type = pygame.KEYUP
  27. else:
  28. type = pygame.KEYDOWN
  29. dict['key'] = pygame.K_RIGHT
  30. event = pygame.event.Event(type, dict)
  31. pygame.event.post(event)
  32. def left(channel):
  33. dict = {}
  34. if GPIO.input(channel) == 1:
  35. type = pygame.KEYUP
  36. else:
  37. type = pygame.KEYDOWN
  38. dict['key'] = pygame.K_RIGHT
  39. event = pygame.event.Event(type, dict)
  40. pygame.event.post(event)
  41. def down(channel):
  42. dict = {}
  43. if GPIO.input(channel) == 1:
  44. type = pygame.KEYUP
  45. else:
  46. type = pygame.KEYDOWN
  47. dict['key'] = pygame.K_DOWN
  48. event = pygame.event.Event(type, dict)
  49. pygame.event.post(event)
  50. def up(channel):
  51. dict = {}
  52. if GPIO.input(channel) == 1:
  53. type = pygame.KEYUP
  54. else:
  55. type = pygame.KEYDOWN
  56. dict['key'] = pygame.K_UP
  57. event = pygame.event.Event(type, dict)
  58. pygame.event.post(event)
  59. def enter(channel):
  60. dict = {}
  61. if GPIO.input(channel) == 1:
  62. type = pygame.KEYUP
  63. else:
  64. type = pygame.KEYDOWN
  65. dict['key'] = pygame.K_RETURN
  66. event = pygame.event.Event(type, dict)
  67. pygame.event.post(event)