touch driver

This commit is contained in:
Bartosz Kościów 2017-05-05 11:52:46 +02:00
parent 7b09c302df
commit 960588f527
5 changed files with 98 additions and 25 deletions

View File

@ -1,2 +1,4 @@
0.2.0
- add AD7843 touch panel driver
0.1.0
- extract library from Doton project

View File

@ -1,5 +1,6 @@
import RPi.GPIO
import sys
import time
sys.path.append("../../")
from gfxlcd.driver.ili9325.gpio import GPIO as ILIGPIO
from gfxlcd.driver.ili9325.ili9325 import ILI9325
@ -7,18 +8,25 @@ from gfxlcd.driver.ad7843.ad7853 import AD7843
RPi.GPIO.setmode(RPi.GPIO.BCM)
# lcd_tft = ILI9325(240, 320, ILIGPIO())
# lcd_tft.init()
lcd_tft = ILI9325(240, 320, ILIGPIO())
lcd_tft.init()
touch = AD7843(240, 320)
def callback(position):
print('(x,y)', position)
touch = AD7843(240, 320, 26, callback)
touch.init()
while True:
try:
ret = touch.get_position()
if ret:
# s.draw_pixel(ret[0], ret[1])
print(ret[0], ret[1])
time.sleep(0.05)
# ret = touch.get_position()
# if ret:
# print(ret[0], ret[1])
except KeyboardInterrupt:
touch.close()
# RPi.GPIO.cleanup()

View File

@ -1,9 +1,10 @@
import spidev # pylint: disable=I0011,F0401
import RPi.GPIO
class AD7843(object):
"""AD7843 class"""
def __init__(self, width, height, spi=0):
def __init__(self, width, height, int_pin=None, callback=None, spi=0):
self.width = width
self.height = height
self.spi = spidev.SpiDev()
@ -16,6 +17,17 @@ class AD7843(object):
'ratio_x': 14.35,
'ratio_y': 10.59
}
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
)
def get_x(self, value):
"""correct value to x"""
@ -23,28 +35,31 @@ class AD7843(object):
def get_y(self, value):
"""correct value to y"""
return self.height - int((value - self.correction['y']) / self.correction['ratio_y'])
return self.height - int((value - self.correction['y']) / self.correction['ratio_y'])
def _interrupt(self, channel):
"""call users callback"""
self.callback(self.get_position())
def get_position(self):
"""get touch coords"""
buffer = []
while len(buffer) < 20:
self.spi.xfer2([0xd0])
rx = self.spi.readbytes(2)
recvx = self.spi.readbytes(2)
self.spi.xfer2([0x90])
ry = self.spi.readbytes(2)
recvy = self.spi.readbytes(2)
tc_rx = rx[0] << 5
tc_rx |= rx[1] >> 3
tc_rx = recvx[0] << 5
tc_rx |= recvx[1] >> 3
tc_ry = ry[0] << 5
tc_ry |= ry[1] >> 3
tc_ry = recvy[0] << 5
tc_ry |= recvy[1] >> 3
x = self.get_x(tc_rx)
y = self.get_y(tc_ry)
if x < 0 or x > self.width or y < 0 or y > self.height:
return None
buffer.append((x, y))
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))
return self._calculate_avr(buffer)
@ -59,6 +74,8 @@ class AD7843(object):
return int(sum_x / len(points)), int(sum_y / len(points))
def close(self):
"""vlose action"""
"""close action"""
if self.int_pin:
RPi.GPIO.remove_event_detect(self.int_pin)
self.spi.close()

View File

@ -9,13 +9,18 @@ Supported:
- ssd1306 via SPI
- nju6450 via GPIO
And for touch panels:
- ad7843 via SPI, uses irq or not
On NJU and SSD uses buffer to keep current content as help for page operations.
Wiring is below
Demos are in demos directory
Initialization
LCD initialization
===
## SSD1306
### SPI
@ -125,6 +130,47 @@ lcd.threshold = 255 - for images a threshold between black and white (on monochr
lcd.transparency_color = [110, 57] #110 - color(s) that are skipped during drawing an image
## Touch panels
===
## AD7843
Constructor:
AD7843(width, height, (T_INT), (callback))
Can be used with T_INT
def callback(position):
print('(x,y)', position)
touch = AD7843(240, 320, 26, callback)
touch.init()
or without:
touch = AD7843(240, 320)
touch.init()
while True:
try:
time.sleep(0.05)
ret = touch.get_position()
if ret:
print(ret[0], ret[1])
except KeyboardInterrupt:
touch.close()
There is no automatic calibration. It must be done manually.
self.correction = {
'x': 364,
'y': 430,
'ratio_x': 14.35,
'ratio_y': 10.59
}
Wiring
===

View File

@ -13,7 +13,7 @@ def read(*paths):
setup(
name='gfxlcd',
version='0.1.2',
version='0.2.0',
description='gfxlcd is a handler for grpahical lcds: ILI9328, SSD1306, NJU6450 @ Raspberry Pi.',
keywords=['gfxlcd', 'raspberry pi' ,'ili9328' ,'ssd1306', 'nju6450', 'lcd', 'graphical lcd'],
long_description=(read('readme.md')),