From 822d7697105fc01cca4251166eb574e1dd3c5fc9 Mon Sep 17 00:00:00 2001 From: rdagger Date: Sat, 13 Aug 2022 13:54:53 -0700 Subject: [PATCH] Add simple touch demo. --- demo_fonts8x8.py | 2 +- demo_touch.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 demo_touch.py diff --git a/demo_fonts8x8.py b/demo_fonts8x8.py index 1f913c2..b1ffcfd 100644 --- a/demo_fonts8x8.py +++ b/demo_fonts8x8.py @@ -1,4 +1,4 @@ -"""ILI9341 demo demo (fonts 8x8).""" +"""ILI9341 demo (fonts 8x8).""" from time import sleep from ili9341 import Display, color565 from machine import Pin, SPI # type: ignore diff --git a/demo_touch.py b/demo_touch.py new file mode 100644 index 0000000..2483b14 --- /dev/null +++ b/demo_touch.py @@ -0,0 +1,64 @@ +"""ILI9341 demo (simple touch demo).""" +from ili9341 import Display, color565 +from xpt2046 import Touch +from machine import idle, Pin, SPI # type: ignore + + +class Demo(object): + """Touchscreen simple demo.""" + CYAN = color565(0, 255, 255) + PURPLE = color565(255, 0, 255) + WHITE = color565(255, 255, 255) + + def __init__(self, display, spi2): + """Initialize box. + + Args: + display (ILI9341): display object + touch (XPT2046): touchscreen object + """ + self.display = display + self.touch = Touch(spi2, cs=Pin(5), int_pin=Pin(0), + int_handler=self.touchscreen_press) + # Display initial message + self.display.draw_text8x8(self.display.width // 2 - 32, + self.display.height - 9, + "TOUCH ME", + self.WHITE, + background=self.PURPLE) + + # A small 5x5 sprite for the dot + self.dot = bytearray(b'\x00\x00\x07\xE0\xF8\x00\x07\xE0\x00\x00\x07\xE0\xF8\x00\xF8\x00\xF8\x00\x07\xE0\xF8\x00\xF8\x00\xF8\x00\xF8\x00\xF8\x00\x07\xE0\xF8\x00\xF8\x00\xF8\x00\x07\xE0\x00\x00\x07\xE0\xF8\x00\x07\xE0\x00\x00') + + def touchscreen_press(self, x, y): + """Process touchscreen press events.""" + # Y needs to be flipped + y = (self.display.height - 1) - y + # Display coordinates + self.display.draw_text8x8(self.display.width // 2 - 32, + self.display.height - 9, + "{0:03d}, {1:03d}".format(x, y), + self.CYAN) + # Draw dot + self.display.draw_sprite(self.dot, x - 2, y - 2, 5, 5) + + +def test(): + """Test code.""" + spi1 = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13)) + display = Display(spi1, dc=Pin(4), cs=Pin(16), rst=Pin(17)) + spi2 = SPI(2, baudrate=1000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19)) + + Demo(display, spi2) + + try: + while True: + idle() + + except KeyboardInterrupt: + print("\nCtrl-C pressed. Cleaning up and exiting...") + finally: + display.cleanup() + + +test()