From 7b09c302dfe3249c68953d54e92908a110556fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Thu, 4 May 2017 22:07:57 +0200 Subject: [PATCH 1/5] touch driver --- gfxlcd/demos/touch.py | 24 ++++++++++++ gfxlcd/drawing/area.py | 4 +- gfxlcd/driver/ad7843/__init__.py | 1 + gfxlcd/driver/ad7843/ad7853.py | 64 ++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 gfxlcd/demos/touch.py create mode 100644 gfxlcd/driver/ad7843/__init__.py create mode 100644 gfxlcd/driver/ad7843/ad7853.py diff --git a/gfxlcd/demos/touch.py b/gfxlcd/demos/touch.py new file mode 100644 index 0000000..b7194fc --- /dev/null +++ b/gfxlcd/demos/touch.py @@ -0,0 +1,24 @@ +import RPi.GPIO +import sys +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() + + +touch = AD7843(240, 320) + +while True: + try: + ret = touch.get_position() + if ret: + # s.draw_pixel(ret[0], ret[1]) + print(ret[0], ret[1]) + + except KeyboardInterrupt: + touch.close() diff --git a/gfxlcd/drawing/area.py b/gfxlcd/drawing/area.py index 7c2daac..48bd8eb 100644 --- a/gfxlcd/drawing/area.py +++ b/gfxlcd/drawing/area.py @@ -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 diff --git a/gfxlcd/driver/ad7843/__init__.py b/gfxlcd/driver/ad7843/__init__.py new file mode 100644 index 0000000..7c2d50b --- /dev/null +++ b/gfxlcd/driver/ad7843/__init__.py @@ -0,0 +1 @@ +__author__ = 'kosci' diff --git a/gfxlcd/driver/ad7843/ad7853.py b/gfxlcd/driver/ad7843/ad7853.py new file mode 100644 index 0000000..72695ea --- /dev/null +++ b/gfxlcd/driver/ad7843/ad7853.py @@ -0,0 +1,64 @@ +import spidev # pylint: disable=I0011,F0401 + + +class AD7843(object): + """AD7843 class""" + def __init__(self, width, height, spi=0): + self.width = width + self.height = height + self.spi = spidev.SpiDev() + self.spi.open(spi, 0) + self.spi.max_speed_hz = 2000000 + self.spi.mode = 0 + self.correction = { + 'x': 364, + 'y': 430, + 'ratio_x': 14.35, + 'ratio_y': 10.59 + } + + 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 get_position(self): + """get touch coords""" + buffer = [] + while len(buffer) < 20: + self.spi.xfer2([0xd0]) + rx = self.spi.readbytes(2) + self.spi.xfer2([0x90]) + ry = self.spi.readbytes(2) + + tc_rx = rx[0] << 5 + tc_rx |= rx[1] >> 3 + + tc_ry = ry[0] << 5 + tc_ry |= ry[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)) + + 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): + """vlose action""" + self.spi.close() + From 960588f5277726da6beca86e2f515836924e4364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Fri, 5 May 2017 11:52:46 +0200 Subject: [PATCH 2/5] touch driver --- CHANGELOG.txt | 4 ++- gfxlcd/demos/touch.py | 22 ++++++++++----- gfxlcd/driver/ad7843/ad7853.py | 45 ++++++++++++++++++++---------- readme.md | 50 ++++++++++++++++++++++++++++++++-- setup.py | 2 +- 5 files changed, 98 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 2e36e69..5bd8c75 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,2 +1,4 @@ +0.2.0 + - add AD7843 touch panel driver 0.1.0 - - extract library from Doton project \ No newline at end of file + - extract library from Doton project diff --git a/gfxlcd/demos/touch.py b/gfxlcd/demos/touch.py index b7194fc..2f42150 100644 --- a/gfxlcd/demos/touch.py +++ b/gfxlcd/demos/touch.py @@ -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() + diff --git a/gfxlcd/driver/ad7843/ad7853.py b/gfxlcd/driver/ad7843/ad7853.py index 72695ea..2a0c35e 100644 --- a/gfxlcd/driver/ad7843/ad7853.py +++ b/gfxlcd/driver/ad7843/ad7853.py @@ -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() diff --git a/readme.md b/readme.md index 84373ac..8c98cc9 100644 --- a/readme.md +++ b/readme.md @@ -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 \ No newline at end of file + LED_A ------------------------ 3.3 diff --git a/setup.py b/setup.py index d2bb2cb..4d8ae17 100644 --- a/setup.py +++ b/setup.py @@ -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')), From 348d8a65f7a764f04385c6db92eda42d1be71b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Fri, 5 May 2017 11:58:41 +0200 Subject: [PATCH 3/5] speed to argument --- gfxlcd/driver/ad7843/ad7853.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gfxlcd/driver/ad7843/ad7853.py b/gfxlcd/driver/ad7843/ad7853.py index 2a0c35e..7e9e56b 100644 --- a/gfxlcd/driver/ad7843/ad7853.py +++ b/gfxlcd/driver/ad7843/ad7853.py @@ -4,12 +4,12 @@ import RPi.GPIO class AD7843(object): """AD7843 class""" - def __init__(self, width, height, int_pin=None, callback=None, spi=0): + 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 = 2000000 + self.spi.max_speed_hz = speed self.spi.mode = 0 self.correction = { 'x': 364, @@ -58,7 +58,7 @@ class AD7843(object): 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: + if 0 <= pos_x <= self.width and 0 <= pos_y <= self.height: buffer.append((pos_x, pos_y)) return self._calculate_avr(buffer) From eb3dc2fcf0caaf4edc4b40128db007458e912729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Fri, 5 May 2017 12:10:56 +0200 Subject: [PATCH 4/5] docs --- gfxlcd/driver/ad7843/__init__.py | 3 ++- readme.md | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/gfxlcd/driver/ad7843/__init__.py b/gfxlcd/driver/ad7843/__init__.py index 7c2d50b..cf13795 100644 --- a/gfxlcd/driver/ad7843/__init__.py +++ b/gfxlcd/driver/ad7843/__init__.py @@ -1 +1,2 @@ -__author__ = 'kosci' +"""driver/ad7843 module""" +__author__ = 'Bartosz Kosciow' diff --git a/readme.md b/readme.md index 8c98cc9..26f56b1 100644 --- a/readme.md +++ b/readme.md @@ -130,7 +130,7 @@ 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 +Touch panels === ## AD7843 From 3f91cc0c7a97a997dc20c5a14781cd98784977a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Fri, 5 May 2017 12:12:27 +0200 Subject: [PATCH 5/5] setup --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 4d8ae17..f90fe63 100644 --- a/setup.py +++ b/setup.py @@ -14,8 +14,8 @@ def read(*paths): setup( name='gfxlcd', 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'], + 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',