touch driver

This commit is contained in:
Bartosz Kościów 2017-05-04 22:07:57 +02:00
parent 81af30e786
commit 7b09c302df
4 changed files with 92 additions and 1 deletions

24
gfxlcd/demos/touch.py Normal file
View File

@ -0,0 +1,24 @@
import RPi.GPIO
import sys
sys.path.append("../../")
from gfxlcd.driver.ili9325.gpio import GPIO as ILIGPIO
from gfxlcd.driver.ili9325.ili9325 import ILI9325
from gfxlcd.driver.ad7843.ad7853 import AD7843
RPi.GPIO.setmode(RPi.GPIO.BCM)
# lcd_tft = ILI9325(240, 320, ILIGPIO())
# lcd_tft.init()
touch = AD7843(240, 320)
while True:
try:
ret = touch.get_position()
if ret:
# s.draw_pixel(ret[0], ret[1])
print(ret[0], ret[1])
except KeyboardInterrupt:
touch.close()

View File

@ -164,6 +164,7 @@ class Area(Pixel):
if area is not None:
self._set_area(*area)
area = None
temporary_area = None
def _is_transparent(self, color):
"""check if color is a transparency color"""
@ -172,7 +173,8 @@ class Area(Pixel):
elif type(self.options['transparency_color'][0]) == int \
and color == self.options['transparency_color']:
return True
elif type(self.options['transparency_color'][0]) == list \
elif (type(self.options['transparency_color'][0]) == list or
type(self.options['transparency_color'][0]) == tuple) \
and color in self.options['transparency_color']:
return True

View File

@ -0,0 +1 @@
__author__ = 'kosci'

View File

@ -0,0 +1,64 @@
import spidev # pylint: disable=I0011,F0401
class AD7843(object):
"""AD7843 class"""
def __init__(self, width, height, spi=0):
self.width = width
self.height = height
self.spi = spidev.SpiDev()
self.spi.open(spi, 0)
self.spi.max_speed_hz = 2000000
self.spi.mode = 0
self.correction = {
'x': 364,
'y': 430,
'ratio_x': 14.35,
'ratio_y': 10.59
}
def get_x(self, value):
"""correct value to x"""
return self.width - int((value - self.correction['x']) / self.correction['ratio_x'])
def get_y(self, value):
"""correct value to y"""
return self.height - int((value - self.correction['y']) / self.correction['ratio_y'])
def get_position(self):
"""get touch coords"""
buffer = []
while len(buffer) < 20:
self.spi.xfer2([0xd0])
rx = self.spi.readbytes(2)
self.spi.xfer2([0x90])
ry = self.spi.readbytes(2)
tc_rx = rx[0] << 5
tc_rx |= rx[1] >> 3
tc_ry = ry[0] << 5
tc_ry |= ry[1] >> 3
x = self.get_x(tc_rx)
y = self.get_y(tc_ry)
if x < 0 or x > self.width or y < 0 or y > self.height:
return None
buffer.append((x, y))
return self._calculate_avr(buffer)
def _calculate_avr(self, points):
"""calculate x,y by average"""
sum_x = 0
sum_y = 0
for point in points:
sum_x += point[0]
sum_y += point[1]
return int(sum_x / len(points)), int(sum_y / len(points))
def close(self):
"""vlose action"""
self.spi.close()