fix colors

separate touch classes
This commit is contained in:
Bartosz Kościów 2017-05-19 16:52:00 +02:00
parent ba02d19b3a
commit e2e8c9603d
8 changed files with 145 additions and 4 deletions

View File

@ -9,6 +9,7 @@ RPi.GPIO.setmode(RPi.GPIO.BCM)
drv = SPI()
lcd_tft = ILI9486(320, 480, drv)
#lcd_tft.rotation = 270
lcd_tft.init()
image_file = Image.open("assets/japan_temple_240x320.jpg")

View File

@ -0,0 +1,34 @@
import RPi.GPIO
import sys
import time
sys.path.append("../../")
from gfxlcd.driver.ili9486.spi import SPI
from gfxlcd.driver.ili9486.ili9486 import ILI9486
from gfxlcd.driver.xpt2046.xpt2046 import XPT2046
RPi.GPIO.setmode(RPi.GPIO.BCM)
lcd_tft = ILI9486(320, 480, SPI())
lcd_tft.init()
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.init()
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
touch.close()
# RPi.GPIO.cleanup()

View File

@ -4,7 +4,7 @@ import RPi.GPIO
class AD7843(object):
"""AD7843 class"""
def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=2000000):
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()
@ -52,11 +52,16 @@ class AD7843(object):
def get_position(self):
"""get touch coords"""
buffer = []
while len(buffer) < 20:
fuse = 40
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)
# if self.cs_pin:
# RPi.GPIO.output(self.cs_pin, 1)
tc_rx = recvx[0] << 5
tc_rx |= recvx[1] >> 3
@ -66,13 +71,17 @@ 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
return self._calculate_avr(buffer)
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:

View File

@ -6,7 +6,7 @@ from gfxlcd.abstract.chip import Chip
class ILI9486(Area, Chip):
"""Class for ILI9486 based LCD"""
rotations = {0: 0x80, 90: 0xf0, 180: 0x40, 270: 0x20}
rotations = {0: 0x88, 90: 0xf8, 180: 0x48, 270: 0x28}
def __init__(self, width, height, driver):
Chip.__init__(self, width, height, driver, True)

View File

@ -60,6 +60,6 @@ class SPI(Driver):
RPi.GPIO.output(self.pins['RS'], 1)
if self.pins['CS']:
RPi.GPIO.output(self.pins['CS'], 0)
self.spi.xfer2([data])
self.spi.xfer2([data >> 8, data])
if self.pins['CS']:
RPi.GPIO.output(self.pins['CS'], 1)

View File

@ -0,0 +1,2 @@
"""driver/xpt2046 module"""
__author__ = 'Bartosz Kosciow'

View File

@ -0,0 +1,95 @@
import spidev # pylint: disable=I0011,F0401
import RPi.GPIO
class XPT2046(object):
"""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
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
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_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"""
print('bb')
self.callback(self.get_position())
def get_position(self):
"""get touch coords"""
buffer = []
fuse = 40
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)
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))
fuse -= 1
return self._calculate_avr(buffer)
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()