Advertisement
gravgun

RPi: read 3.2' TFT touchscreen buttons

May 21st, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # Example script to read button input from Banggood's 3.2' TFT touchscreen
  3. # Cancel script execution with Ctrl+C
  4. import time
  5. import RPi.GPIO as GPIO
  6.  
  7. GPIO.setmode(GPIO.BOARD)
  8. pins = {12: "K1", 16: "K2", 18: "K3"}
  9. for n in pins:
  10.     GPIO.setup(n, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  11.     # The buttons have a little bounce, 20ms seems to be a good threshold
  12.     GPIO.add_event_detect(n, GPIO.BOTH, bouncetime=20)
  13.  
  14. try:
  15.     while True:
  16.         for n, name in pins.iteritems():
  17.             if GPIO.event_detected(n):
  18.                 if not GPIO.input(n): # Pull-up: reversed
  19.                     print "  Press %d / %s" % (n, name)
  20.                 else:
  21.                     print "Release %d / %s" % (n, name)
  22.         time.sleep(0.02) # Polling is a bad thing, let the CPU rest
  23. except KeyboardInterrupt:
  24.     pass
  25.  
  26. for n in pins:
  27.     GPIO.remove_event_detect(n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement