search working well progam for xpt2046

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
yellowpowerplay
Posts: 9
Joined: Fri Oct 22, 2021 6:04 pm

search working well progam for xpt2046

Post by yellowpowerplay » Fri Oct 22, 2021 6:43 pm

Hello everyone . I am new on this forum and also micropython is new to me. I'm looking for a working program for the xpt2046 driver .
Work with a ESP32 devkit1 and display ili9341 with touch xpt2046. The display is not a problem but the touch part . I have already tested some examples from Roberthh and Jerremy09 all about xpt2046 , but it doesn't work.

yellowpowerplay
Posts: 9
Joined: Fri Oct 22, 2021 6:04 pm

Re: search working well progam for xpt2046

Post by yellowpowerplay » Sun Oct 24, 2021 8:30 pm

"""ILI9341 demo (fonts)."""
from time import sleep
from ili9341 import Display, color565
from machine import Pin,SPI
from xglcd_font import XglcdFont
from xpt2046_syn import XPT2046


spi = SPI(2, baudrate=40000000, sck=Pin(18), mosi=Pin(23),miso=Pin(19))
display = Display(spi, dc=Pin(27), cs=Pin(14), rst=Pin(33))

print('Loading fonts...')

print('Loading espresso_dolce')

espresso_dolce = XglcdFont('font/EspressoDolse18x24.c',18 , 24)

print('Fonts loaded.')


display.draw_text(0, 20, 'Try working touch xpt ', espresso_dolce , color565(255,255,0))

sleep(2)
display.clear()

cs = Pin(22,Pin.OUT)
spi2 = SPI(1, baudrate=2000000,cs =22)
xpt = XPT2046(spi2)

while True:
p = xpt.get_touch(raw=True)
if p is not None:
print(p)
sleep(0.1)
if p[0] < 500 and p[1] < 500:
break

yellowpowerplay
Posts: 9
Joined: Fri Oct 22, 2021 6:04 pm

Re: search working well progam for xpt2046

Post by yellowpowerplay » Sun Oct 24, 2021 8:34 pm

"""ILI9341 demo (fonts)."""
from time import sleep
from ili9341 import Display, color565
from machine import Pin,SPI
from xglcd_font import XglcdFont
from xpt2046_syn import XPT2046


spi = SPI(2, baudrate=40000000, sck=Pin(18), mosi=Pin(23),miso=Pin(19))
display = Display(spi, dc=Pin(27), cs=Pin(14), rst=Pin(33))

print('Loading fonts...')

print('Loading espresso_dolce')

espresso_dolce = XglcdFont('font/EspressoDolse18x24.c',18 , 24)

print('Fonts loaded.')


display.draw_text(0, 20, 'Try working touch xpt ', espresso_dolce , color565(255,255,0))

sleep(2)
display.clear()

cs = Pin(22,Pin.OUT)
spi2 = SPI(1, baudrate=2000000,cs =22)
xpt = XPT2046(spi2)

while True:
p = xpt.get_touch(raw=True)
if p is not None:
print(p)
sleep(0.1)
if p[0] < 500 and p[1] < 500:
break

yellowpowerplay
Posts: 9
Joined: Fri Oct 22, 2021 6:04 pm

Re: search working well progam for xpt2046

Post by yellowpowerplay » Sun Oct 24, 2021 9:15 pm

because no one wouldn't answer me !! Then look self for a solution WORKING !!


#The xpt2046 touch working on the ili9346

import machine
import xpt2046_syn
import time

spi = machine.SPI(1, baudrate=10000000, miso=machine.Pin(19), mosi=machine.Pin(23), sck=machine.Pin(18))
ts_cs = machine.Pin(12, machine.Pin.OUT)

touch = xpt2046_syn.XPT2046(spi = spi, confidence=5, margin=50, delay=10, calibration=None)
ts_cs.value(0)

while True:
bstisk = touch.get_touch(initial=False, wait=True, raw=False, timeout=300)
stisk2 = touch.raw_touch()
print(str(stisk2)) # print the x, y on shell

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: search working well progam for xpt2046

Post by Roberthh » Mon Oct 25, 2021 6:27 am

Sorry, I forgot to answer. But you found the solution I wanted to suggest: use the synchronous version of the XPT2046 driver.

yellowpowerplay
Posts: 9
Joined: Fri Oct 22, 2021 6:04 pm

Re: search working well progam for xpt2046

Post by yellowpowerplay » Thu Oct 28, 2021 2:34 pm

Thanks Roberthh for answer. Another question. Now I read the x and y in a sting print(str(stisk2)) Can I also read the x and y separately from the xpt2046_syn ?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: search working well progam for xpt2046

Post by Roberthh » Thu Oct 28, 2021 3:08 pm

Both get_touch() and raw_touch() return a (x,y) tuple. so

x_value = stisk2[0]
y_value = stisk2[1]

or

x_value = bstisk[0]
y_value = stisk2[1]

or simply:

x_value, y_value = touch.raw_touch()

or

x_value, y_value = touch.get_touch(initial=False, wait=True, raw=False, timeout=300)

yellowpowerplay
Posts: 9
Joined: Fri Oct 22, 2021 6:04 pm

Re: search working well progam for xpt2046

Post by yellowpowerplay » Thu Oct 28, 2021 3:48 pm

Super ;-) ;-) thank you Roberthh ....and also for quick reply SUPER !!!

yellowpowerplay
Posts: 9
Joined: Fri Oct 22, 2021 6:04 pm

Re: search working well progam for xpt2046

Post by yellowpowerplay » Thu Oct 28, 2021 5:02 pm

What am I still doing wrong?
Also tested the other solutions but also error

34 import machine
35 spi = machine.SPI(1, baudrate=1000000, miso=machine.Pin(12), mosi=machine.Pin(13), sck=machine.Pin(14))
36 ts_cs = machine.Pin(15, machine.Pin.OUT)
37
38 touch = xpt2046_syn.XPT2046(spi = spi, confidence=5, margin=50, delay=10, calibration=None)
39 ts_cs.value(0)
40
41 while True:
42 x_value,y_value = touch.get_touch(initial=False, wait=True, raw=False, timeout=300)
43 #stisk = touch.get_touch(initial=False, wait=True, raw=False, timeout=300)
44 #stisk2 = touch.raw_touch()
45 #print(str(stisk2)) # print the x, y on shell
46
47 #x_value=stisk2[0]
48 #y_value=stisk2[1]
49 #x_value, y_value = touch.get_touch(initial=False, wait=True, raw=False, timeout=300)
50 #x_value, y_value = touch.raw_touch()
51
52 print(str(x_value))
53 print(str(y_value))

Traceback (most recent call last):
File "<stdin>", line 42, in <module>
TypeError: 'NoneType' object isn't iterable
>>>

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: search working well progam for xpt2046

Post by Roberthh » Thu Oct 28, 2021 6:11 pm

both get_touch() and raw_touch() return None if no key is pressed. SO you have to code something like:

Code: Select all

res = touch.get_touch(initial=False, wait=True, raw=False, timeout=300) 
if res is not None:
    x_value,y_value = res
else:
    continue

Post Reply