rotation for AD

This commit is contained in:
Bartosz Kościów 2017-05-21 20:15:49 +02:00
parent b1a314c60e
commit d35c996400
3 changed files with 27 additions and 19 deletions

View File

@ -16,7 +16,7 @@ def callback(position):
print('(x,y)', position) print('(x,y)', position)
touch = AD7843(240, 320, 26, callback) touch = AD7843(240, 320, 26, callback)
# touch.rotate = 270
touch.init() touch.init()
while True: while True:

View File

@ -35,13 +35,23 @@ class AD7843(Touch):
RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT) RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT)
RPi.GPIO.output(self.cs_pin, 1) RPi.GPIO.output(self.cs_pin, 1)
def get_x(self, value): def _get_xy(self, offset_x, offset_y):
"""correct value to x""" """correct x and y"""
return self.width - int((value - self.correction['x']) / self.correction['ratio_x']) 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): if self.rotate == 90:
"""correct value to y""" return self.height - int((offset_y - self.correction['y']) / self.correction['ratio_y']), \
return self.height - int((value - 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): def _interrupt(self, channel):
"""call users callback""" """call users callback"""
@ -71,14 +81,20 @@ class AD7843(Touch):
tc_ry = recvy[0] << 5 tc_ry = recvy[0] << 5
tc_ry |= recvy[1] >> 3 tc_ry |= recvy[1] >> 3
pos_x = self.get_x(tc_rx) pos_x, pos_y = self._get_xy(tc_rx, tc_ry)
pos_y = self.get_y(tc_ry) if self._in_bounds(pos_x, pos_y):
if 0 <= pos_x <= self.width and 0 <= pos_y <= self.height:
buffer.append((pos_x, pos_y)) buffer.append((pos_x, pos_y))
fuse -= 1 fuse -= 1
return self._calculate_avr(buffer) 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): def _calculate_avr(self, points):
"""calculate x,y by average""" """calculate x,y by average"""
if len(points) == 0: if len(points) == 0:

View File

@ -35,14 +35,6 @@ class XPT2046(Touch):
RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT) RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT)
RPi.GPIO.output(self.cs_pin, 1) 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): def _interrupt(self, channel):
"""call users callback""" """call users callback"""
self.callback(self.get_position()) self.callback(self.get_position())
@ -101,7 +93,7 @@ class XPT2046(Touch):
def _in_bounds(self, pos_x, pos_y): def _in_bounds(self, pos_x, pos_y):
"""checks if point is in range""" """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 return 0 <= pos_x <= self.width and 0 <= pos_y <= self.height
else: else:
return 0 <= pos_y <= self.width and 0 <= pos_x <= self.height return 0 <= pos_y <= self.width and 0 <= pos_x <= self.height