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] 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() -