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