From 7f17d045460496f0da8011222e84ac4aaac4031a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Sun, 21 May 2017 18:47:23 +0200 Subject: [PATCH] xpt2046 --- gfxlcd/demos/touch_320x480.py | 12 ++++----- gfxlcd/demos/touch_loop.py | 11 +++++--- gfxlcd/driver/xpt2046/xpt2046.py | 45 ++++++++++++++++++-------------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/gfxlcd/demos/touch_320x480.py b/gfxlcd/demos/touch_320x480.py index 7fdac2d..6ffd702 100644 --- a/gfxlcd/demos/touch_320x480.py +++ b/gfxlcd/demos/touch_320x480.py @@ -16,12 +16,12 @@ def callback(position): print('(x,y)', position) touch = XPT2046(320, 480, 17, callback, 7) -touch.correction = { - 'x': -3394,#364, - 'y': -3350,#430, - 'ratio_x': 1, - 'ratio_y': 1 -} +# touch.correction = { +# 'x': 1,#3394,#364, +# 'y': 1,#3350,#430, +# 'ratio_x': 1, +# 'ratio_y': 1 +# } touch.init() while True: diff --git a/gfxlcd/demos/touch_loop.py b/gfxlcd/demos/touch_loop.py index a0bb1de..7bb4351 100644 --- a/gfxlcd/demos/touch_loop.py +++ b/gfxlcd/demos/touch_loop.py @@ -4,18 +4,23 @@ import time sys.path.append("../../") from gfxlcd.driver.ili9325.gpio import GPIO as ILIGPIO from gfxlcd.driver.ili9325.ili9325 import ILI9325 +from gfxlcd.driver.xpt2046.xpt2046 import XPT2046 from gfxlcd.driver.ad7843.ad7843 import AD7843 +from gfxlcd.driver.ili9486.spi import SPI +from gfxlcd.driver.ili9486.ili9486 import ILI9486 RPi.GPIO.setmode(RPi.GPIO.BCM) -lcd_tft = ILI9325(240, 320, ILIGPIO()) +# lcd_tft = ILI9325(240, 320, ILIGPIO()) +# lcd_tft.init() +lcd_tft = ILI9486(320, 480, SPI()) lcd_tft.init() - def callback(position): print('(x,y)', position) -touch = AD7843(240, 320) +#touch = AD7843(240, 320) +touch = XPT2046(320, 480) touch.init() diff --git a/gfxlcd/driver/xpt2046/xpt2046.py b/gfxlcd/driver/xpt2046/xpt2046.py index 1235b39..3eb21f9 100644 --- a/gfxlcd/driver/xpt2046/xpt2046.py +++ b/gfxlcd/driver/xpt2046/xpt2046.py @@ -12,10 +12,10 @@ class XPT2046(object): self.spi.max_speed_hz = speed self.spi.mode = 0 self.correction = { - 'x': 364, - 'y': 430, - 'ratio_x': 14.35, - 'ratio_y': 10.59 + 'x': 540, + 'y': 50, + 'ratio_x': 0.94, #14.35, + 'ratio_y': 1.26, #10.59 } self.cs_pin = cs_pin self.int_pin = int_pin @@ -43,7 +43,6 @@ class XPT2046(object): def _interrupt(self, channel): """call users callback""" - print('bb') self.callback(self.get_position()) def get_position(self): @@ -53,24 +52,30 @@ class XPT2046(object): while len(buffer) < 20 and fuse > 0: if self.cs_pin: RPi.GPIO.output(self.cs_pin, 0) - self.spi.xfer2([0xd0]) - recvx = self.spi.readbytes(2) - self.spi.xfer2([0x90]) - recvy = self.spi.readbytes(2) + + self.spi.xfer2([0x80 | 0x08 | 0x30]) + recv = self.spi.readbytes(1) + tc_rz = recv[0] & 0x7f + + self.spi.xfer2([0x80 | 0x08 | 0x40]) + recv = self.spi.readbytes(1) + tc_rz += (255-recv[0] & 0x7f) + + self.spi.xfer2([0x80 | 0x10]) + recv = self.spi.readbytes(2) + tc_rx = 1023-((recv[0] << 2)|(recv[1] >> 6)) + + self.spi.xfer2([0x80 | 0x50]) + recv = self.spi.readbytes(2) + tc_ry = ((recv[0] << 2)|(recv[1] >> 6)) if self.cs_pin: RPi.GPIO.output(self.cs_pin, 1) - tc_rx = recvx[0] << 5 - tc_rx |= recvx[1] >> 3 - - tc_ry = recvy[0] << 5 - tc_ry |= recvy[1] >> 3 - - pos_x = self.get_x(tc_rx) - pos_y = self.get_y(tc_ry) - print(pos_x, pos_y) - if 0 <= pos_x <= self.width and 0 <= pos_y <= self.height: - buffer.append((pos_x, pos_y)) + if tc_rz > 10: + pos_x = self.get_x(tc_rx) + pos_y = self.get_y(tc_ry) + if 0 <= pos_x <= self.width and 0 <= pos_y <= self.height: + buffer.append((pos_x, pos_y)) fuse -= 1 return self._calculate_avr(buffer)