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 1/6] 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) From b1a314c60eb4a2463538898b78b350f5a30507b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Sun, 21 May 2017 19:41:48 +0200 Subject: [PATCH 2/6] touch abstract xpt rotation --- gfxlcd/abstract/touch.py | 21 ++++++++ .../{touch_320x480.py => touch_480x320.py} | 10 ++-- gfxlcd/driver/ad7843/ad7843.py | 13 ++--- gfxlcd/driver/xpt2046/xpt2046.py | 52 ++++++++++++++----- 4 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 gfxlcd/abstract/touch.py rename gfxlcd/demos/{touch_320x480.py => touch_480x320.py} (75%) diff --git a/gfxlcd/abstract/touch.py b/gfxlcd/abstract/touch.py new file mode 100644 index 0000000..c3cb11a --- /dev/null +++ b/gfxlcd/abstract/touch.py @@ -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 diff --git a/gfxlcd/demos/touch_320x480.py b/gfxlcd/demos/touch_480x320.py similarity index 75% rename from gfxlcd/demos/touch_320x480.py rename to gfxlcd/demos/touch_480x320.py index 6ffd702..d730194 100644 --- a/gfxlcd/demos/touch_320x480.py +++ b/gfxlcd/demos/touch_480x320.py @@ -15,13 +15,9 @@ lcd_tft.init() def callback(position): print('(x,y)', position) -touch = XPT2046(320, 480, 17, callback, 7) -# touch.correction = { -# 'x': 1,#3394,#364, -# 'y': 1,#3350,#430, -# 'ratio_x': 1, -# 'ratio_y': 1 -# } +touch = XPT2046(480, 320, 17, callback, 7) +#touch.rotate = 270 + touch.init() while True: diff --git a/gfxlcd/driver/ad7843/ad7843.py b/gfxlcd/driver/ad7843/ad7843.py index 5f5e9e1..a8fdd5b 100644 --- a/gfxlcd/driver/ad7843/ad7843.py +++ b/gfxlcd/driver/ad7843/ad7843.py @@ -1,8 +1,9 @@ import spidev # pylint: disable=I0011,F0401 import RPi.GPIO +from gfxlcd.abstract.touch import Touch -class AD7843(object): +class AD7843(Touch): """AD7843 class""" def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000): self.width = width @@ -21,6 +22,7 @@ class AD7843(object): self.int_pin = int_pin self.callback = callback self.bouncetime = 500 + self.rotate = 0 def init(self): """some init functions""" @@ -54,14 +56,14 @@ class AD7843(object): buffer = [] fuse = 40 while len(buffer) < 20 and fuse > 0: - # if self.cs_pin: - # RPi.GPIO.output(self.cs_pin, 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) - # if self.cs_pin: - # RPi.GPIO.output(self.cs_pin, 1) + if self.cs_pin: + RPi.GPIO.output(self.cs_pin, 1) tc_rx = recvx[0] << 5 tc_rx |= recvx[1] >> 3 @@ -71,7 +73,6 @@ class AD7843(object): 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)) fuse -= 1 diff --git a/gfxlcd/driver/xpt2046/xpt2046.py b/gfxlcd/driver/xpt2046/xpt2046.py index 3eb21f9..40c1b9a 100644 --- a/gfxlcd/driver/xpt2046/xpt2046.py +++ b/gfxlcd/driver/xpt2046/xpt2046.py @@ -1,8 +1,9 @@ import spidev # pylint: disable=I0011,F0401 import RPi.GPIO +from gfxlcd.abstract.touch import Touch -class XPT2046(object): +class XPT2046(Touch): """XPT2046 class""" def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000): self.width = width @@ -14,13 +15,14 @@ class XPT2046(object): self.correction = { 'x': 540, 'y': 50, - 'ratio_x': 0.94, #14.35, - 'ratio_y': 1.26, #10.59 + 'ratio_x': 0.94, + 'ratio_y': 1.26, } self.cs_pin = cs_pin self.int_pin = int_pin self.callback = callback self.bouncetime = 500 + self.rotate = 0 def init(self): """some init functions""" @@ -33,18 +35,36 @@ class XPT2046(object): RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT) RPi.GPIO.output(self.cs_pin, 1) - def get_x(self, value): - """correct value to x""" - return int((value - self.correction['x']) / self.correction['ratio_x']) - - def get_y(self, value): - """correct value to y""" - return int((value - self.correction['y']) / self.correction['ratio_y']) + # def get_x(self, value): + # """correct value to x""" + # return int((value - self.correction['x']) / self.correction['ratio_x']) + # + # def get_y(self, value): + # """correct value to y""" + # return int((value - self.correction['y']) / self.correction['ratio_y']) def _interrupt(self, channel): """call users callback""" 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): """get touch coords""" buffer = [] @@ -72,14 +92,20 @@ class XPT2046(object): if self.cs_pin: RPi.GPIO.output(self.cs_pin, 1) 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: + pos_x, pos_y = self._get_xy(tc_rx, tc_ry) + if self._in_bounds(pos_x, pos_y): buffer.append((pos_x, pos_y)) fuse -= 1 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): """calculate x,y by average""" if len(points) == 0: From d35c996400bc51c772329d59077867d95c65ecd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Sun, 21 May 2017 20:15:49 +0200 Subject: [PATCH 3/6] rotation for AD --- gfxlcd/demos/touch_240x320.py | 2 +- gfxlcd/driver/ad7843/ad7843.py | 34 +++++++++++++++++++++++--------- gfxlcd/driver/xpt2046/xpt2046.py | 10 +--------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/gfxlcd/demos/touch_240x320.py b/gfxlcd/demos/touch_240x320.py index a63dd56..f67dc10 100644 --- a/gfxlcd/demos/touch_240x320.py +++ b/gfxlcd/demos/touch_240x320.py @@ -16,7 +16,7 @@ def callback(position): print('(x,y)', position) touch = AD7843(240, 320, 26, callback) - +# touch.rotate = 270 touch.init() while True: diff --git a/gfxlcd/driver/ad7843/ad7843.py b/gfxlcd/driver/ad7843/ad7843.py index a8fdd5b..d05c5c0 100644 --- a/gfxlcd/driver/ad7843/ad7843.py +++ b/gfxlcd/driver/ad7843/ad7843.py @@ -35,13 +35,23 @@ class AD7843(Touch): RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT) RPi.GPIO.output(self.cs_pin, 1) - def get_x(self, value): - """correct value to x""" - return self.width - int((value - self.correction['x']) / self.correction['ratio_x']) + 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']), \ + int((offset_y - self.correction['y']) / self.correction['ratio_y']) - def get_y(self, value): - """correct value to y""" - return self.height - int((value - self.correction['y']) / self.correction['ratio_y']) + if self.rotate == 90: + return self.height - 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']), \ + self.height - int((offset_y - self.correction['y']) / self.correction['ratio_y']) + + if self.rotate == 270: + return int((offset_y - self.correction['y']) / self.correction['ratio_y']), \ + self.width - int((offset_x - self.correction['x']) / self.correction['ratio_x']) def _interrupt(self, channel): """call users callback""" @@ -71,14 +81,20 @@ class AD7843(Touch): tc_ry = recvy[0] << 5 tc_ry |= recvy[1] >> 3 - 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: + pos_x, pos_y = self._get_xy(tc_rx, tc_ry) + if self._in_bounds(pos_x, pos_y): buffer.append((pos_x, pos_y)) fuse -= 1 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 == 180: + 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): """calculate x,y by average""" if len(points) == 0: diff --git a/gfxlcd/driver/xpt2046/xpt2046.py b/gfxlcd/driver/xpt2046/xpt2046.py index 40c1b9a..f1cfeff 100644 --- a/gfxlcd/driver/xpt2046/xpt2046.py +++ b/gfxlcd/driver/xpt2046/xpt2046.py @@ -35,14 +35,6 @@ class XPT2046(Touch): RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT) RPi.GPIO.output(self.cs_pin, 1) - # def get_x(self, value): - # """correct value to x""" - # return int((value - self.correction['x']) / self.correction['ratio_x']) - # - # def get_y(self, value): - # """correct value to y""" - # return int((value - self.correction['y']) / self.correction['ratio_y']) - def _interrupt(self, channel): """call users callback""" self.callback(self.get_position()) @@ -101,7 +93,7 @@ class XPT2046(Touch): def _in_bounds(self, pos_x, pos_y): """checks if point is in range""" - if self.rotate == 0 or self.rotate == 280: + if self.rotate == 0 or self.rotate == 180: 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 From 125110ee51c04c4b65693f61eadef5027c90fa8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Sun, 21 May 2017 20:29:00 +0200 Subject: [PATCH 4/6] refactor touch classes --- gfxlcd/abstract/touch.py | 62 ++++++++++++++++++++++++++++---- gfxlcd/demos/touch_240x320.py | 2 +- gfxlcd/driver/ad7843/ad7843.py | 58 +----------------------------- gfxlcd/driver/xpt2046/xpt2046.py | 54 +--------------------------- 4 files changed, 59 insertions(+), 117 deletions(-) diff --git a/gfxlcd/abstract/touch.py b/gfxlcd/abstract/touch.py index c3cb11a..e299262 100644 --- a/gfxlcd/abstract/touch.py +++ b/gfxlcd/abstract/touch.py @@ -1,21 +1,71 @@ """Touch panel interface""" +import spidev # pylint: disable=I0011,F0401 import abc +import RPi.GPIO class Touch(metaclass=abc.ABCMeta): """Touch class""" + def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000): + self.width = width + self.height = height + self.spi = spidev.SpiDev() + self.spi.open(spi, 0) + self.spi.max_speed_hz = speed + self.spi.mode = 0 + self.cs_pin = cs_pin + self.int_pin = int_pin + self.callback = callback + self.bouncetime = 500 + self.rotate = 0 + self.correction = { + 'x': 0, + 'y': 0, + 'ratio_x': 1, + 'ratio_y': 1, + } - @abc.abstractmethod def init(self): - """some additional init""" - return + """some init functions""" + if self.int_pin: + RPi.GPIO.setup(self.int_pin, RPi.GPIO.IN) + RPi.GPIO.add_event_detect( + self.int_pin, RPi.GPIO.BOTH, callback=self._interrupt, bouncetime=self.bouncetime + ) + if self.cs_pin: + RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT) + RPi.GPIO.output(self.cs_pin, 1) @abc.abstractmethod def get_position(self): """returns pressed position""" return - @abc.abstractmethod def close(self): - """close functions""" - return + """close action""" + if self.int_pin: + RPi.GPIO.remove_event_detect(self.int_pin) + self.spi.close() + + def _interrupt(self, channel): + """call users callback""" + self.callback(self.get_position()) + + def _calculate_avr(self, points): + """calculate x,y by average""" + if len(points) == 0: + return None + 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 _in_bounds(self, pos_x, pos_y): + """checks if point is in range""" + if self.rotate == 0 or self.rotate == 180: + 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 diff --git a/gfxlcd/demos/touch_240x320.py b/gfxlcd/demos/touch_240x320.py index f67dc10..9681fac 100644 --- a/gfxlcd/demos/touch_240x320.py +++ b/gfxlcd/demos/touch_240x320.py @@ -16,7 +16,7 @@ def callback(position): print('(x,y)', position) touch = AD7843(240, 320, 26, callback) -# touch.rotate = 270 +touch.rotate = 180 touch.init() while True: diff --git a/gfxlcd/driver/ad7843/ad7843.py b/gfxlcd/driver/ad7843/ad7843.py index d05c5c0..1f516c9 100644 --- a/gfxlcd/driver/ad7843/ad7843.py +++ b/gfxlcd/driver/ad7843/ad7843.py @@ -1,4 +1,3 @@ -import spidev # pylint: disable=I0011,F0401 import RPi.GPIO from gfxlcd.abstract.touch import Touch @@ -6,34 +5,13 @@ from gfxlcd.abstract.touch import Touch class AD7843(Touch): """AD7843 class""" def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000): - self.width = width - self.height = height - self.spi = spidev.SpiDev() - self.spi.open(spi, 0) - self.spi.max_speed_hz = speed - self.spi.mode = 0 + super().__init__(width, height, int_pin, callback, cs_pin, spi, speed) self.correction = { 'x': 364, 'y': 430, 'ratio_x': 14.35, 'ratio_y': 10.59 } - self.cs_pin = cs_pin - self.int_pin = int_pin - self.callback = callback - self.bouncetime = 500 - self.rotate = 0 - - def init(self): - """some init functions""" - if self.int_pin: - RPi.GPIO.setup(self.int_pin, RPi.GPIO.IN) - RPi.GPIO.add_event_detect( - self.int_pin, RPi.GPIO.BOTH, callback=self._interrupt, bouncetime=self.bouncetime - ) - if self.cs_pin: - RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT) - RPi.GPIO.output(self.cs_pin, 1) def _get_xy(self, offset_x, offset_y): """correct x and y""" @@ -53,14 +31,6 @@ class AD7843(Touch): return int((offset_y - self.correction['y']) / self.correction['ratio_y']), \ self.width - int((offset_x - self.correction['x']) / self.correction['ratio_x']) - def _interrupt(self, channel): - """call users callback""" - if self.cs_pin: - RPi.GPIO.output(self.cs_pin, 0) - self.callback(self.get_position()) - if self.cs_pin: - RPi.GPIO.output(self.cs_pin, 1) - def get_position(self): """get touch coords""" buffer = [] @@ -87,29 +57,3 @@ class AD7843(Touch): fuse -= 1 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 == 180: - 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): - """calculate x,y by average""" - if len(points) == 0: - return None - 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): - """close action""" - if self.int_pin: - RPi.GPIO.remove_event_detect(self.int_pin) - self.spi.close() - diff --git a/gfxlcd/driver/xpt2046/xpt2046.py b/gfxlcd/driver/xpt2046/xpt2046.py index f1cfeff..44d68a6 100644 --- a/gfxlcd/driver/xpt2046/xpt2046.py +++ b/gfxlcd/driver/xpt2046/xpt2046.py @@ -1,4 +1,3 @@ -import spidev # pylint: disable=I0011,F0401 import RPi.GPIO from gfxlcd.abstract.touch import Touch @@ -6,38 +5,13 @@ from gfxlcd.abstract.touch import Touch class XPT2046(Touch): """XPT2046 class""" def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000): - self.width = width - self.height = height - self.spi = spidev.SpiDev() - self.spi.open(spi, 0) - self.spi.max_speed_hz = speed - self.spi.mode = 0 + super().__init__(width, height, int_pin, callback, cs_pin, spi, speed) self.correction = { 'x': 540, 'y': 50, 'ratio_x': 0.94, 'ratio_y': 1.26, } - self.cs_pin = cs_pin - self.int_pin = int_pin - self.callback = callback - self.bouncetime = 500 - self.rotate = 0 - - def init(self): - """some init functions""" - if self.int_pin: - RPi.GPIO.setup(self.int_pin, RPi.GPIO.IN) - RPi.GPIO.add_event_detect( - self.int_pin, RPi.GPIO.BOTH, callback=self._interrupt, bouncetime=self.bouncetime - ) - if self.cs_pin: - RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT) - RPi.GPIO.output(self.cs_pin, 1) - - def _interrupt(self, channel): - """call users callback""" - self.callback(self.get_position()) def _get_xy(self, offset_x, offset_y): """correct x and y""" @@ -90,29 +64,3 @@ class XPT2046(Touch): fuse -= 1 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 == 180: - 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): - """calculate x,y by average""" - if len(points) == 0: - return None - 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): - """close action""" - if self.int_pin: - RPi.GPIO.remove_event_detect(self.int_pin) - self.spi.close() - From 941f5f0a60ccf600ea1156a9680d3b45d278eb7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Mon, 22 May 2017 16:11:08 +0200 Subject: [PATCH 5/6] fix getting width and height --- gfxlcd/abstract/chip.py | 11 +++++++++-- gfxlcd/driver/ili9486/ili9486.py | 1 - 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/gfxlcd/abstract/chip.py b/gfxlcd/abstract/chip.py index 03bf3c0..699dd38 100644 --- a/gfxlcd/abstract/chip.py +++ b/gfxlcd/abstract/chip.py @@ -6,6 +6,7 @@ class Chip(metaclass=abc.ABCMeta): """Chip class""" def __init__(self, width, height, driver, auto_flush): self.options = {} + self.rotation = 0 self._width = width self._height = height self.driver = driver @@ -14,12 +15,18 @@ class Chip(metaclass=abc.ABCMeta): @property def width(self): """get width""" - return self._width + if self.rotation == 0 or self.rotation == 180: + return self._width + else: + return self._height @property def height(self): """get height""" - return self._height + if self.rotation == 0 or self.rotation == 180: + return self._height + else: + return self._width @abc.abstractmethod def _converted_background_color(self): diff --git a/gfxlcd/driver/ili9486/ili9486.py b/gfxlcd/driver/ili9486/ili9486.py index c7c3d72..cdcafb6 100644 --- a/gfxlcd/driver/ili9486/ili9486.py +++ b/gfxlcd/driver/ili9486/ili9486.py @@ -11,7 +11,6 @@ class ILI9486(Area, Chip): def __init__(self, width, height, driver): Chip.__init__(self, width, height, driver, True) Area.__init__(self, driver) - self.rotation = 0 def _converted_background_color(self): """color from 8-8-8 to 5-6-5""" From 78a9467e64bf0012554c253be0d6a5b960b2079f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Mon, 22 May 2017 16:11:36 +0200 Subject: [PATCH 6/6] fix getting width and height --- gfxlcd/driver/ili9325/ili9325.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gfxlcd/driver/ili9325/ili9325.py b/gfxlcd/driver/ili9325/ili9325.py index 27562b5..cf37742 100644 --- a/gfxlcd/driver/ili9325/ili9325.py +++ b/gfxlcd/driver/ili9325/ili9325.py @@ -32,7 +32,6 @@ class ILI9325(Area, Chip): def __init__(self, width, height, driver): Chip.__init__(self, width, height, driver, True) Area.__init__(self, driver) - self.rotation = 0 def _converted_background_color(self): """color from 8-8-8 to 5-6-5"""