From c0dd3603b2810a6f78458ad4c031d7ae9099bc3f Mon Sep 17 00:00:00 2001 From: Liz Date: Fri, 8 Dec 2023 13:18:57 -0500 Subject: [PATCH] adding touchpaint example --- examples/touchpaint_example.py | 94 ++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 examples/touchpaint_example.py diff --git a/examples/touchpaint_example.py b/examples/touchpaint_example.py new file mode 100644 index 0000000..4747a84 --- /dev/null +++ b/examples/touchpaint_example.py @@ -0,0 +1,94 @@ +# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries +# SPDX-License-Identifier: MIT + +""" +Touch paint example for HX83570 + FT5336 TFT Breakout +""" + +import board +import displayio +import adafruit_ft5336 +from adafruit_hx8357 import HX8357 + +displayio.release_displays() + +spi = board.SPI() +tft_cs = board.TX +tft_dc = board.RX + +display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs) +# display is rotated to align x, y with touch screen x, y +display = HX8357(display_bus, width=320, height=480, rotation=90) + +i2c = board.I2C() # uses board.SCL and board.SDA +touch = adafruit_ft5336.Adafruit_FT5336(i2c) + +pixel_size = 10 +palette_width = 45 +palette_height = 320 // 8 + +splash = displayio.Group() +display.root_group = splash + +bitmap = displayio.Bitmap(320, 480, 8) +color_palette = displayio.Palette(8) +color_palette[0] = 0x000000 +color_palette[1] = 0xFF0000 +color_palette[2] = 0xFFFF00 +color_palette[3] = 0x00FF00 +color_palette[4] = 0x00FFFF +color_palette[5] = 0x0000FF +color_palette[6] = 0xFF00FF +color_palette[7] = 0xFFFFFF + +tile_grid = displayio.TileGrid( + bitmap, + pixel_shader=color_palette + +) +# tilegrid is flipped to align x, y with touch screen x, y +tile_grid.flip_y = True +tile_grid.flip_x = True + +splash.append(tile_grid) + +display.root_group = splash + +current_color = 7 + +for i in range(palette_width): + for j in range(palette_height): + bitmap[j + palette_height, i] = 1 + bitmap[j + palette_height * 2, i] = 2 + bitmap[j + palette_height * 3, i] = 3 + bitmap[j + palette_height * 4, i] = 4 + bitmap[j + palette_height * 5, i] = 5 + bitmap[j + palette_height * 6, i] = 6 + bitmap[j + palette_height * 7, i] = 0 + +while True: + if touch.touched: + try: + for t in touch.points: + x = t[0] + y = t[1] + print(x, y) + if ( + not 0 <= x < display.width + or not 0 <= y < display.height + ): + continue # Skip out of bounds touches + if y < palette_width: + current_color = bitmap[x, y] + else: + for i in range(pixel_size): + for j in range(pixel_size): + x_pixel = x - (pixel_size // 2) + i + y_pixel = y - (pixel_size // 2) + j + if ( + 0 <= x_pixel < display.width + and 0 <= y_pixel < display.height + ): + bitmap[x_pixel, y_pixel] = current_color + except RuntimeError: + pass