PyFTtxx6/pyft5xx6/README.md
2025-05-24 12:44:45 +01:00

137 lines
3.1 KiB
Markdown

# PyFT5xx6
A Python library for interfacing with FT5xx6 Capacitive Touch Panel (CTP) controllers, primarily designed for Raspberry Pi.
## Overview
PyFT5xx6 provides a Python interface for FT5xx6 touch controllers commonly used in touchscreen displays. The library supports:
- Reading touch events and gestures
- Both polling and interrupt-based operation modes
- Multi-touch (up to 5 points)
- Gesture recognition
- Thread-safe operation
This library is based on Arduino library by Owen Lyke and original work by Helge Langehaug, ported to Python for Raspberry Pi usage.
## Installation
### From PyPI
```bash
pip install pyft5xx6
```
### From Source
```bash
git clone https://gitea.tourolle.paris/dtourolle/PyFTtxx6
cd PyFTtxx6/pyft5xx6
pip install -e .
```
## Dependencies
- `RPi.GPIO` (Only required when running on Raspberry Pi)
- `smbus2`
## Usage
### Basic Example
```python
from pyft5xx6 import FT5316, Status, Mode
import time
# Initialize the touch controller
touch = FT5316()
result = touch.begin(i2c_bus=1) # Using I2C bus 1
if result != Status.NOMINAL:
print("Failed to initialize touch controller")
exit(1)
# Set polling mode
touch.set_mode(Mode.POLLING)
# Main loop
try:
while True:
# Update touch data
touch.update()
# Check if there is a new touch event
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.gesture != 0:
print(f"Gesture: {record.gesture}")
time.sleep(0.01) # 10ms delay
except KeyboardInterrupt:
print("Exiting")
```
### Using Interrupts
```python
from pyft5xx6 import FT5316, Status, Mode
import time
import RPi.GPIO as GPIO
# Interrupt pin (BCM numbering)
INT_PIN = 17
# Interrupt handler
def touch_interrupt(channel):
# The actual touch data will be read in the main loop
print("Touch interrupt detected")
# 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")
exit(1)
# Main loop
try:
while True:
# Check if there is a new touch event
if touch.new_data:
# Update touch data
touch.update()
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.gesture != 0:
print(f"Gesture: {record.gesture}")
time.sleep(0.01) # 10ms delay
except KeyboardInterrupt:
print("Exiting")
# Clean up GPIO
GPIO.cleanup()
```
## Supported Controllers
- FT5316 (I2C address 0x38)
- Generic FT5xx6 (configurable address)
## License
MIT License - See the LICENSE file for details.