Merge pull request #3 from bkosciow/touch

Touch
This commit is contained in:
Bartosz 2017-05-05 12:14:28 +02:00 committed by GitHub
commit bccdfbae10
7 changed files with 172 additions and 7 deletions

View File

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

32
gfxlcd/demos/touch.py Normal file
View File

@ -0,0 +1,32 @@
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
from gfxlcd.driver.ad7843.ad7853 import AD7843
RPi.GPIO.setmode(RPi.GPIO.BCM)
lcd_tft = ILI9325(240, 320, ILIGPIO())
lcd_tft.init()
def callback(position):
print('(x,y)', position)
touch = AD7843(240, 320, 26, callback)
touch.init()
while True:
try:
time.sleep(0.05)
# ret = touch.get_position()
# if ret:
# print(ret[0], ret[1])
except KeyboardInterrupt:
touch.close()
# RPi.GPIO.cleanup()

View File

@ -164,6 +164,7 @@ class Area(Pixel):
if area is not None:
self._set_area(*area)
area = None
temporary_area = None
def _is_transparent(self, color):
"""check if color is a transparency color"""
@ -172,7 +173,8 @@ class Area(Pixel):
elif type(self.options['transparency_color'][0]) == int \
and color == self.options['transparency_color']:
return True
elif type(self.options['transparency_color'][0]) == list \
elif (type(self.options['transparency_color'][0]) == list or
type(self.options['transparency_color'][0]) == tuple) \
and color in self.options['transparency_color']:
return True

View File

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

View File

@ -0,0 +1,81 @@
import spidev # pylint: disable=I0011,F0401
import RPi.GPIO
class AD7843(object):
"""AD7843 class"""
def __init__(self, width, height, int_pin=None, callback=None, spi=0, speed=2000000):
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.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"""
return self.width - int((value - self.correction['x']) / self.correction['ratio_x'])
def get_y(self, value):
"""correct value to 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])
recvx = self.spi.readbytes(2)
self.spi.xfer2([0x90])
recvy = self.spi.readbytes(2)
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)
if 0 <= pos_x <= self.width and 0 <= pos_y <= self.height:
buffer.append((pos_x, pos_y))
return self._calculate_avr(buffer)
def _calculate_avr(self, points):
"""calculate x,y by average"""
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()

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
===
@ -187,4 +233,4 @@ Default:
DB15 ------------------------ G21
CS ------------------------ GND (always selected)
REST ------------------------ G25
LED_A ------------------------ 3.3
LED_A ------------------------ 3.3

View File

@ -13,9 +13,9 @@ def read(*paths):
setup(
name='gfxlcd',
version='0.1.2',
description='gfxlcd is a handler for grpahical lcds: ILI9328, SSD1306, NJU6450 @ Raspberry Pi.',
keywords=['gfxlcd', 'raspberry pi' ,'ili9328' ,'ssd1306', 'nju6450', 'lcd', 'graphical lcd'],
version='0.2.0',
description='gfxlcd is a handler for graphical lcds: ILI9328, SSD1306, NJU6450, touch panel: AD7843 @ Raspberry Pi.',
keywords=['gfxlcd', 'raspberry pi' ,'ili9328' ,'ssd1306', 'nju6450', 'lcd', 'graphical lcd', 'touch panel', 'ad7843'],
long_description=(read('readme.md')),
url='https://github.com/bkosciow/gfxlcd',
license='MIT',