Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

2.8" PiTFT Touch with CircuitPython
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

2.8" PiTFT Touch with CircuitPython

by DannyBGoode on Fri Jul 31, 2020 12:32 pm

I have the display portion working between CircuitPython and my PiTFT (attached to a 3B+) using these tutorials:

https://learn.adafruit.com/circuitpytho ... spberry-pi
https://learn.adafruit.com/adafruit-2-8 ... thon-usage

And installing some other missing libraries that are needed by the tutorials, but they don't mention:

Code: Select all | TOGGLE FULL SIZE
sudo apt install libtiff5
sudo apt-get install libopenjp2-7

However, I've found nothing online indicating how to get the touch aspect working... I would prefer to use the capacitive touch version, but I also have the resistive version on hand.

Thanks,

Danny

DannyBGoode
 
Posts: 25
Joined: Wed Nov 06, 2019 11:58 am

Re: 2.8" PiTFT Touch with CircuitPython

by DannyBGoode on Fri Jul 31, 2020 4:14 pm

I got it working (at least the basics)....

Here's what I did with the capacitive display:

Code: Select all | TOGGLE FULL SIZE
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.ili9341 as ili9341
import os
from time import sleep
import busio
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
import adafruit_focaltouch

# definitions
pos = (0,0)
 
#Colors
WHITE = (255,255,255)
RED = (255,0,0)

# Configuration for CS and DC pins (these are PiTFT defaults):
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D24)
 
# Config for display baudrate (default max is 24mhz):
BAUDRATE = 24000000
 
# Setup SPI bus using hardware SPI:
spi = board.SPI()

disp = ili9341.ILI9341(
    spi,
    rotation=90,  # 2.2", 2.4", 2.8", 3.2" ILI9341
    cs=cs_pin,
    dc=dc_pin,
    rst=reset_pin,
    baudrate=BAUDRATE,
)

height = disp.width
width = disp.height

image = Image.new("RGB", (width, height))
   
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
 
# Clear the display - color it white
draw.rectangle((0, 0, width, height), fill=WHITE)
disp.image(image)
 
i2c=board.I2C()
ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c, debug=False)

while True:
   if ft.touched:
      # Clear the display - color it white
      draw.rectangle((0, 0, width, height), fill=WHITE)
      disp.image(image)
      print(ft.touches)
      ts = ft.touches
      point = ts[0]
      # perform transformation
      y = 240 - point["x"]
      x = point["y"]
      print("x:" + str(x) + ", y:" + str(y))
      # Draw the circle
      draw.ellipse((x-10, y-10, x+10, y+10), fill=RED, width=2)
      disp.image(image)

   sleep(.25)


And here's my test of the resistive. I'm getting values, but they don't appear correct. Since I have the capacitive working (and that's my preference), I won't be working on this any longer. But I figured I'd include it for anyone in the future that comes across this...

Code: Select all | TOGGLE FULL SIZE
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.ili9341 as ili9341
import os
from time import sleep
import busio
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
from adafruit_stmpe610 import Adafruit_STMPE610_I2C

# definitions
pos = (0,0)
 
#Colors
WHITE = (255,255,255)
RED = (255,0,0)

# Configuration for CS and DC pins (these are PiTFT defaults):
cs_pin = digitalio.DigitalInOut(board.CE0)
cs1_pin = digitalio.DigitalInOut(board.CE1)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D24)
 
# Config for display baudrate (default max is 24mhz):
BAUDRATE = 24000000
 
# Setup SPI bus using hardware SPI:
spi = board.SPI()

disp = ili9341.ILI9341(
    spi,
    rotation=90,  # 2.2", 2.4", 2.8", 3.2" ILI9341
    cs=cs_pin,
    dc=dc_pin,
    rst=reset_pin,
    baudrate=BAUDRATE,
)

height = disp.width
width = disp.height


image = Image.new("RGB", (width, height))
   
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
 
# Clear the display - color it white
draw.rectangle((0, 0, width, height), fill=WHITE)
disp.image(image)
 
# Draw the circle
draw.ellipse((-10, -10, 10, 10), fill=RED, width=2)
disp.image(image)

#i2c=busio.I2C(board.SCL,board.SDA)
i2c=board.I2C()

st = Adafruit_STMPE610_SPI(spi, cs1_pin)
#st = Adafruit_STMPE610_I2C(i2c, 65)

while True:
   if st.touched:
      print(st.read_data())
   sleep(.25)


Danny

DannyBGoode
 
Posts: 25
Joined: Wed Nov 06, 2019 11:58 am

Please be positive and constructive with your questions and comments.