|
|
@ -17,7 +17,7 @@ class Touch(object):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, spi, cs, int_pin=None, int_handler=None,
|
|
|
|
def __init__(self, spi, cs, int_pin=None, int_handler=None,
|
|
|
|
width=240, height=320,
|
|
|
|
width=240, height=320,
|
|
|
|
x_min=100, x_max=1962, y_min=100, y_max=1900):
|
|
|
|
x_min=100, x_max=1962, y_min=100, y_max=1900, rotation=0):
|
|
|
|
"""Initialize touch screen controller.
|
|
|
|
"""Initialize touch screen controller.
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
@ -44,6 +44,7 @@ class Touch(object):
|
|
|
|
self.x_max = x_max
|
|
|
|
self.x_max = x_max
|
|
|
|
self.y_min = y_min
|
|
|
|
self.y_min = y_min
|
|
|
|
self.y_max = y_max
|
|
|
|
self.y_max = y_max
|
|
|
|
|
|
|
|
self.rotation = rotation
|
|
|
|
self.last_touch=0
|
|
|
|
self.last_touch=0
|
|
|
|
self.x_multiplier = width / (x_max - x_min)
|
|
|
|
self.x_multiplier = width / (x_max - x_min)
|
|
|
|
self.x_add = x_min * -self.x_multiplier
|
|
|
|
self.x_add = x_min * -self.x_multiplier
|
|
|
@ -58,6 +59,23 @@ class Touch(object):
|
|
|
|
int_pin.irq(trigger=int_pin.IRQ_FALLING | int_pin.IRQ_RISING,
|
|
|
|
int_pin.irq(trigger=int_pin.IRQ_FALLING | int_pin.IRQ_RISING,
|
|
|
|
handler=self.int_press)
|
|
|
|
handler=self.int_press)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rot(self,x,y,rotation=None): # convert touch locations to possibly-rotated screen locations.
|
|
|
|
|
|
|
|
if rotation is None:
|
|
|
|
|
|
|
|
rotation=self.rotation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x,y = self.normalize(x,y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if rotation == 0:
|
|
|
|
|
|
|
|
return x, self.height - y - 1
|
|
|
|
|
|
|
|
elif rotation == 90:
|
|
|
|
|
|
|
|
return self.height - y - 1, self.width - x - 1
|
|
|
|
|
|
|
|
elif rotation == 180:
|
|
|
|
|
|
|
|
return self.width - x - 1, y
|
|
|
|
|
|
|
|
elif rotation == 270:
|
|
|
|
|
|
|
|
return y , x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_touch(self):
|
|
|
|
def get_touch(self):
|
|
|
|
"""Take multiple samples to get accurate touch reading."""
|
|
|
|
"""Take multiple samples to get accurate touch reading."""
|
|
|
|
timeout = 2 # set timeout to 2 seconds
|
|
|
|
timeout = 2 # set timeout to 2 seconds
|
|
|
@ -107,7 +125,7 @@ class Touch(object):
|
|
|
|
y = int(self.y_multiplier * y + self.y_add)
|
|
|
|
y = int(self.y_multiplier * y + self.y_add)
|
|
|
|
return x, y
|
|
|
|
return x, y
|
|
|
|
|
|
|
|
|
|
|
|
def raw_touch(self,xyonly=False):
|
|
|
|
def raw_touch(self,xyonly=False,nozero=True):
|
|
|
|
"""Read raw X,Y touch values.
|
|
|
|
"""Read raw X,Y touch values.
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
@ -128,12 +146,13 @@ class Touch(object):
|
|
|
|
b = self.send_command(self.GET_BATTERY)
|
|
|
|
b = self.send_command(self.GET_BATTERY)
|
|
|
|
a = self.send_command(self.GET_AUX) # chip aux ADC input
|
|
|
|
a = self.send_command(self.GET_AUX) # chip aux ADC input
|
|
|
|
p = x * (z1 - z2) / z1 if z1>0 else None # touch-pressure is a formula based on x
|
|
|
|
p = x * (z1 - z2) / z1 if z1>0 else None # touch-pressure is a formula based on x
|
|
|
|
|
|
|
|
p2 = x/4096 * (z2/z1-1) if z1>0 else None # touch-pressure is a formula based on x
|
|
|
|
d = None
|
|
|
|
d = None
|
|
|
|
if p is not None:
|
|
|
|
if p is not None:
|
|
|
|
now=time.ticks_ms()
|
|
|
|
now=time.ticks_ms()
|
|
|
|
d = time.ticks_diff(now, self.last_touch) # timediff since last (for debounce)
|
|
|
|
d = time.ticks_diff(now, self.last_touch) # timediff since last (for debounce)
|
|
|
|
self.last_touch=now
|
|
|
|
self.last_touch=now
|
|
|
|
return (p, x, y, z1, z2, t0, t1, b, a, d)
|
|
|
|
return (p, x, y, z1, z2, t0, t1, b, a, d, p2) + tuple(self.normalize(x,y) + tuple(self.rot(x,y))) # cnd
|
|
|
|
|
|
|
|
|
|
|
|
def send_command(self, command):
|
|
|
|
def send_command(self, command):
|
|
|
|
"""Write command to XT2046 (MicroPython).
|
|
|
|
"""Write command to XT2046 (MicroPython).
|
|
|
@ -149,3 +168,20 @@ class Touch(object):
|
|
|
|
self.cs(1)
|
|
|
|
self.cs(1)
|
|
|
|
|
|
|
|
|
|
|
|
return (self.rx_buf[1] << 4) | (self.rx_buf[2] >> 4)
|
|
|
|
return (self.rx_buf[1] << 4) | (self.rx_buf[2] >> 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" Examples
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
from xpt2046 import Touch
|
|
|
|
|
|
|
|
from machine import Pin, SPI, ADC, PWM, SDCard, SoftSPI
|
|
|
|
|
|
|
|
sspi = SoftSPI(baudrate=500000, sck=Pin(25), mosi=Pin(32), miso=Pin(39))
|
|
|
|
|
|
|
|
touch = Touch(sspi, cs=Pin(33)) # a, int_pin=Pin(36), int_handler=self._touch_handler)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
t = touch.raw_touch() # (p, x, y, z1, z2, t0, t1, b, a, d)
|
|
|
|
|
|
|
|
if t[-1] is not None:
|
|
|
|
|
|
|
|
print(t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|