search working well progam for xpt2046
-
- Posts: 9
- Joined: Fri Oct 22, 2021 6:04 pm
search working well progam for xpt2046
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.
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.
-
- Posts: 9
- Joined: Fri Oct 22, 2021 6:04 pm
Re: search working well progam for xpt2046
"""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
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
-
- Posts: 9
- Joined: Fri Oct 22, 2021 6:04 pm
Re: search working well progam for xpt2046
"""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
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
-
- Posts: 9
- Joined: Fri Oct 22, 2021 6:04 pm
Re: search working well progam for xpt2046
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
#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
Re: search working well progam for xpt2046
Sorry, I forgot to answer. But you found the solution I wanted to suggest: use the synchronous version of the XPT2046 driver.
-
- Posts: 9
- Joined: Fri Oct 22, 2021 6:04 pm
Re: search working well progam for xpt2046
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 ?
Re: search working well progam for xpt2046
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)
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)
-
- Posts: 9
- Joined: Fri Oct 22, 2021 6:04 pm
Re: search working well progam for xpt2046
Super thank you Roberthh ....and also for quick reply SUPER !!!
-
- Posts: 9
- Joined: Fri Oct 22, 2021 6:04 pm
Re: search working well progam for xpt2046
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
>>>
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
>>>
Re: search working well progam for xpt2046
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