Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- # Example script to read button input from Banggood's 3.2' TFT touchscreen
- # Cancel script execution with Ctrl+C
- import time
- import RPi.GPIO as GPIO
- GPIO.setmode(GPIO.BOARD)
- pins = {12: "K1", 16: "K2", 18: "K3"}
- for n in pins:
- GPIO.setup(n, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- # The buttons have a little bounce, 20ms seems to be a good threshold
- GPIO.add_event_detect(n, GPIO.BOTH, bouncetime=20)
- try:
- while True:
- for n, name in pins.iteritems():
- if GPIO.event_detected(n):
- if not GPIO.input(n): # Pull-up: reversed
- print " Press %d / %s" % (n, name)
- else:
- print "Release %d / %s" % (n, name)
- time.sleep(0.02) # Polling is a bad thing, let the CPU rest
- except KeyboardInterrupt:
- pass
- for n in pins:
- GPIO.remove_event_detect(n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement