92 lines
2.6 KiB
Python
92 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Interrupt mode example for the PyFT5xx6 library.
|
|
This example demonstrates how to use the FT5316 touch controller with interrupt-based operation.
|
|
"""
|
|
|
|
from pyft5xx6 import FT5316, Status, Mode, Gestures
|
|
import time
|
|
import signal
|
|
import sys
|
|
|
|
try:
|
|
import RPi.GPIO as GPIO
|
|
except ImportError:
|
|
print("RPi.GPIO not available. This example requires a Raspberry Pi.")
|
|
sys.exit(1)
|
|
|
|
# Interrupt pin (BCM numbering)
|
|
INT_PIN = 17 # Change to match your wiring
|
|
|
|
# Flag for cleanup on exit
|
|
cleanup_done = False
|
|
|
|
# Interrupt handler
|
|
def touch_interrupt(channel):
|
|
"""Called when a touch event triggers the interrupt pin"""
|
|
# Just set the controller's interrupt flag
|
|
# The actual data reading is done in the main loop
|
|
touch.interrupt()
|
|
print("Touch interrupt detected")
|
|
|
|
# Signal handler for clean exit
|
|
def signal_handler(sig, frame):
|
|
global cleanup_done
|
|
print("Exiting...")
|
|
if not cleanup_done:
|
|
GPIO.cleanup()
|
|
cleanup_done = True
|
|
sys.exit(0)
|
|
|
|
def main():
|
|
global touch
|
|
|
|
# Register signal handler
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
# Initialize the touch controller
|
|
touch = FT5316()
|
|
result = touch.begin(i2c_bus=1, int_pin=INT_PIN, user_isr=touch_interrupt)
|
|
|
|
if result != Status.NOMINAL:
|
|
print("Failed to initialize touch controller")
|
|
GPIO.cleanup()
|
|
return
|
|
|
|
# Allocate a buffer for 10 touch records
|
|
touch.use_buffer(10)
|
|
|
|
print("Touch controller initialized in interrupt mode.")
|
|
print("Touch the screen to trigger interrupt...")
|
|
|
|
# Main loop
|
|
try:
|
|
while True:
|
|
# Check if new data is available (set by the interrupt handler)
|
|
if touch.new_data:
|
|
# Read touch data
|
|
if touch.new_touch:
|
|
record = touch.read()
|
|
print(f"Number of touches: {record.num_touches}")
|
|
|
|
if record.num_touches > 0:
|
|
print(f"Touch 1: ({record.t1x}, {record.t1y})")
|
|
|
|
if record.num_touches > 1:
|
|
print(f"Touch 2: ({record.t2x}, {record.t2y})")
|
|
|
|
if record.gesture != Gestures.NO_GESTURE:
|
|
print(f"Gesture: {record.gesture.name}")
|
|
|
|
time.sleep(0.01) # 10ms delay
|
|
|
|
except KeyboardInterrupt:
|
|
# This should be caught by the signal handler
|
|
pass
|
|
finally:
|
|
if not cleanup_done:
|
|
GPIO.cleanup()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|