touch abstract
xpt rotation
This commit is contained in:
parent
7f17d04546
commit
b1a314c60e
21
gfxlcd/abstract/touch.py
Normal file
21
gfxlcd/abstract/touch.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
"""Touch panel interface"""
|
||||||
|
import abc
|
||||||
|
|
||||||
|
|
||||||
|
class Touch(metaclass=abc.ABCMeta):
|
||||||
|
"""Touch class"""
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def init(self):
|
||||||
|
"""some additional init"""
|
||||||
|
return
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def get_position(self):
|
||||||
|
"""returns pressed position"""
|
||||||
|
return
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def close(self):
|
||||||
|
"""close functions"""
|
||||||
|
return
|
@ -15,13 +15,9 @@ lcd_tft.init()
|
|||||||
def callback(position):
|
def callback(position):
|
||||||
print('(x,y)', position)
|
print('(x,y)', position)
|
||||||
|
|
||||||
touch = XPT2046(320, 480, 17, callback, 7)
|
touch = XPT2046(480, 320, 17, callback, 7)
|
||||||
# touch.correction = {
|
#touch.rotate = 270
|
||||||
# 'x': 1,#3394,#364,
|
|
||||||
# 'y': 1,#3350,#430,
|
|
||||||
# 'ratio_x': 1,
|
|
||||||
# 'ratio_y': 1
|
|
||||||
# }
|
|
||||||
touch.init()
|
touch.init()
|
||||||
|
|
||||||
while True:
|
while True:
|
@ -1,8 +1,9 @@
|
|||||||
import spidev # pylint: disable=I0011,F0401
|
import spidev # pylint: disable=I0011,F0401
|
||||||
import RPi.GPIO
|
import RPi.GPIO
|
||||||
|
from gfxlcd.abstract.touch import Touch
|
||||||
|
|
||||||
|
|
||||||
class AD7843(object):
|
class AD7843(Touch):
|
||||||
"""AD7843 class"""
|
"""AD7843 class"""
|
||||||
def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000):
|
def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000):
|
||||||
self.width = width
|
self.width = width
|
||||||
@ -21,6 +22,7 @@ class AD7843(object):
|
|||||||
self.int_pin = int_pin
|
self.int_pin = int_pin
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
self.bouncetime = 500
|
self.bouncetime = 500
|
||||||
|
self.rotate = 0
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
"""some init functions"""
|
"""some init functions"""
|
||||||
@ -54,14 +56,14 @@ class AD7843(object):
|
|||||||
buffer = []
|
buffer = []
|
||||||
fuse = 40
|
fuse = 40
|
||||||
while len(buffer) < 20 and fuse > 0:
|
while len(buffer) < 20 and fuse > 0:
|
||||||
# if self.cs_pin:
|
if self.cs_pin:
|
||||||
# RPi.GPIO.output(self.cs_pin, 0)
|
RPi.GPIO.output(self.cs_pin, 0)
|
||||||
self.spi.xfer2([0xd0])
|
self.spi.xfer2([0xd0])
|
||||||
recvx = self.spi.readbytes(2)
|
recvx = self.spi.readbytes(2)
|
||||||
self.spi.xfer2([0x90])
|
self.spi.xfer2([0x90])
|
||||||
recvy = self.spi.readbytes(2)
|
recvy = self.spi.readbytes(2)
|
||||||
# if self.cs_pin:
|
if self.cs_pin:
|
||||||
# RPi.GPIO.output(self.cs_pin, 1)
|
RPi.GPIO.output(self.cs_pin, 1)
|
||||||
|
|
||||||
tc_rx = recvx[0] << 5
|
tc_rx = recvx[0] << 5
|
||||||
tc_rx |= recvx[1] >> 3
|
tc_rx |= recvx[1] >> 3
|
||||||
@ -71,7 +73,6 @@ class AD7843(object):
|
|||||||
|
|
||||||
pos_x = self.get_x(tc_rx)
|
pos_x = self.get_x(tc_rx)
|
||||||
pos_y = self.get_y(tc_ry)
|
pos_y = self.get_y(tc_ry)
|
||||||
print(pos_x, pos_y)
|
|
||||||
if 0 <= pos_x <= self.width and 0 <= pos_y <= self.height:
|
if 0 <= pos_x <= self.width and 0 <= pos_y <= self.height:
|
||||||
buffer.append((pos_x, pos_y))
|
buffer.append((pos_x, pos_y))
|
||||||
fuse -= 1
|
fuse -= 1
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import spidev # pylint: disable=I0011,F0401
|
import spidev # pylint: disable=I0011,F0401
|
||||||
import RPi.GPIO
|
import RPi.GPIO
|
||||||
|
from gfxlcd.abstract.touch import Touch
|
||||||
|
|
||||||
|
|
||||||
class XPT2046(object):
|
class XPT2046(Touch):
|
||||||
"""XPT2046 class"""
|
"""XPT2046 class"""
|
||||||
def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000):
|
def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000):
|
||||||
self.width = width
|
self.width = width
|
||||||
@ -14,13 +15,14 @@ class XPT2046(object):
|
|||||||
self.correction = {
|
self.correction = {
|
||||||
'x': 540,
|
'x': 540,
|
||||||
'y': 50,
|
'y': 50,
|
||||||
'ratio_x': 0.94, #14.35,
|
'ratio_x': 0.94,
|
||||||
'ratio_y': 1.26, #10.59
|
'ratio_y': 1.26,
|
||||||
}
|
}
|
||||||
self.cs_pin = cs_pin
|
self.cs_pin = cs_pin
|
||||||
self.int_pin = int_pin
|
self.int_pin = int_pin
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
self.bouncetime = 500
|
self.bouncetime = 500
|
||||||
|
self.rotate = 0
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
"""some init functions"""
|
"""some init functions"""
|
||||||
@ -33,18 +35,36 @@ class XPT2046(object):
|
|||||||
RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT)
|
RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT)
|
||||||
RPi.GPIO.output(self.cs_pin, 1)
|
RPi.GPIO.output(self.cs_pin, 1)
|
||||||
|
|
||||||
def get_x(self, value):
|
# def get_x(self, value):
|
||||||
"""correct value to x"""
|
# """correct value to x"""
|
||||||
return int((value - self.correction['x']) / self.correction['ratio_x'])
|
# return int((value - self.correction['x']) / self.correction['ratio_x'])
|
||||||
|
#
|
||||||
def get_y(self, value):
|
# def get_y(self, value):
|
||||||
"""correct value to y"""
|
# """correct value to y"""
|
||||||
return int((value - self.correction['y']) / self.correction['ratio_y'])
|
# return int((value - self.correction['y']) / self.correction['ratio_y'])
|
||||||
|
|
||||||
def _interrupt(self, channel):
|
def _interrupt(self, channel):
|
||||||
"""call users callback"""
|
"""call users callback"""
|
||||||
self.callback(self.get_position())
|
self.callback(self.get_position())
|
||||||
|
|
||||||
|
def _get_xy(self, offset_x, offset_y):
|
||||||
|
"""correct x and y"""
|
||||||
|
if self.rotate == 0:
|
||||||
|
return int((offset_x - self.correction['x']) / self.correction['ratio_x']), \
|
||||||
|
self.height - int((offset_y - self.correction['y']) / self.correction['ratio_y'])
|
||||||
|
|
||||||
|
if self.rotate == 90:
|
||||||
|
return int((offset_y - self.correction['y']) / self.correction['ratio_y']), \
|
||||||
|
int((offset_x - self.correction['x']) / self.correction['ratio_x']),
|
||||||
|
|
||||||
|
if self.rotate == 180:
|
||||||
|
return self.width - int((offset_x - self.correction['x']) / self.correction['ratio_x']), \
|
||||||
|
int((offset_y - self.correction['y']) / self.correction['ratio_y'])
|
||||||
|
|
||||||
|
if self.rotate == 270:
|
||||||
|
return self.height - int((offset_y - self.correction['y']) / self.correction['ratio_y']), \
|
||||||
|
self.width - int((offset_x - self.correction['x']) / self.correction['ratio_x'])
|
||||||
|
|
||||||
def get_position(self):
|
def get_position(self):
|
||||||
"""get touch coords"""
|
"""get touch coords"""
|
||||||
buffer = []
|
buffer = []
|
||||||
@ -72,14 +92,20 @@ class XPT2046(object):
|
|||||||
if self.cs_pin:
|
if self.cs_pin:
|
||||||
RPi.GPIO.output(self.cs_pin, 1)
|
RPi.GPIO.output(self.cs_pin, 1)
|
||||||
if tc_rz > 10:
|
if tc_rz > 10:
|
||||||
pos_x = self.get_x(tc_rx)
|
pos_x, pos_y = self._get_xy(tc_rx, tc_ry)
|
||||||
pos_y = self.get_y(tc_ry)
|
if self._in_bounds(pos_x, pos_y):
|
||||||
if 0 <= pos_x <= self.width and 0 <= pos_y <= self.height:
|
|
||||||
buffer.append((pos_x, pos_y))
|
buffer.append((pos_x, pos_y))
|
||||||
fuse -= 1
|
fuse -= 1
|
||||||
|
|
||||||
return self._calculate_avr(buffer)
|
return self._calculate_avr(buffer)
|
||||||
|
|
||||||
|
def _in_bounds(self, pos_x, pos_y):
|
||||||
|
"""checks if point is in range"""
|
||||||
|
if self.rotate == 0 or self.rotate == 280:
|
||||||
|
return 0 <= pos_x <= self.width and 0 <= pos_y <= self.height
|
||||||
|
else:
|
||||||
|
return 0 <= pos_y <= self.width and 0 <= pos_x <= self.height
|
||||||
|
|
||||||
def _calculate_avr(self, points):
|
def _calculate_avr(self, points):
|
||||||
"""calculate x,y by average"""
|
"""calculate x,y by average"""
|
||||||
if len(points) == 0:
|
if len(points) == 0:
|
||||||
|
Loading…
Reference in New Issue
Block a user