|
| 1 | +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +""" |
| 4 | +Simple painting demo that draws on the Adafruit Qualia ESP32-S3 RGB666 |
| 5 | +with the 4.0" square display and FT6206 captouch driver |
| 6 | +""" |
| 7 | + |
| 8 | +import displayio |
| 9 | +import busio |
| 10 | +import board |
| 11 | +import dotclockframebuffer |
| 12 | +from framebufferio import FramebufferDisplay |
| 13 | +import adafruit_focaltouch |
| 14 | + |
| 15 | +displayio.release_displays() |
| 16 | + |
| 17 | +# Initialize the Display |
| 18 | +tft_pins = dict(board.TFT_PINS) |
| 19 | + |
| 20 | +tft_timings = { |
| 21 | + "frequency": 16000000, |
| 22 | + "width": 720, |
| 23 | + "height": 720, |
| 24 | + "hsync_pulse_width": 2, |
| 25 | + "hsync_front_porch": 46, |
| 26 | + "hsync_back_porch": 44, |
| 27 | + "vsync_pulse_width": 2, |
| 28 | + "vsync_front_porch": 16, |
| 29 | + "vsync_back_porch": 18, |
| 30 | + "hsync_idle_low": False, |
| 31 | + "vsync_idle_low": False, |
| 32 | + "de_idle_high": False, |
| 33 | + "pclk_active_high": False, |
| 34 | + "pclk_idle_high": False, |
| 35 | +} |
| 36 | + |
| 37 | +init_sequence_tl040hds20 = bytes() |
| 38 | + |
| 39 | +board.I2C().deinit() |
| 40 | +i2c = busio.I2C(board.SCL, board.SDA, frequency=100_000) |
| 41 | +tft_io_expander = dict(board.TFT_IO_EXPANDER) |
| 42 | +# tft_io_expander['i2c_address'] = 0x38 # uncomment for rev B |
| 43 | +dotclockframebuffer.ioexpander_send_init_sequence( |
| 44 | + i2c, init_sequence_tl040hds20, **tft_io_expander |
| 45 | +) |
| 46 | + |
| 47 | +fb = dotclockframebuffer.DotClockFramebuffer(**tft_pins, **tft_timings) |
| 48 | +display = FramebufferDisplay(fb, auto_refresh=False) |
| 49 | + |
| 50 | +# Main Program |
| 51 | +pixel_size = 6 |
| 52 | +palette_width = 160 |
| 53 | +palette_height = display.height // 8 |
| 54 | + |
| 55 | +bitmap = displayio.Bitmap(display.width, display.height, 65535) |
| 56 | + |
| 57 | +# Create a TileGrid to hold the bitmap |
| 58 | +tile_grid = displayio.TileGrid( |
| 59 | + bitmap, |
| 60 | + pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565), |
| 61 | +) |
| 62 | + |
| 63 | +# Create a Group to hold the TileGrid |
| 64 | +group = displayio.Group() |
| 65 | + |
| 66 | +# Add the TileGrid to the Group |
| 67 | +group.append(tile_grid) |
| 68 | + |
| 69 | +# Add the Group to the Display |
| 70 | +display.root_group = group |
| 71 | + |
| 72 | +display.auto_refresh = True |
| 73 | + |
| 74 | +ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c, address=0x48) |
| 75 | + |
| 76 | +current_color = displayio.ColorConverter().convert(0xFFFFFF) |
| 77 | + |
| 78 | +for i in range(palette_width): |
| 79 | + color_index = i * 255 // palette_width |
| 80 | + rgb565 = displayio.ColorConverter().convert( |
| 81 | + color_index | color_index << 8 | color_index << 16 |
| 82 | + ) |
| 83 | + r_mask = 0xF800 |
| 84 | + g_mask = 0x07E0 |
| 85 | + b_mask = 0x001F |
| 86 | + for j in range(palette_height): |
| 87 | + bitmap[i, j + palette_height] = rgb565 & b_mask |
| 88 | + bitmap[i, j + palette_height * 2] = rgb565 & (b_mask | g_mask) |
| 89 | + bitmap[i, j + palette_height * 3] = rgb565 & g_mask |
| 90 | + bitmap[i, j + palette_height * 4] = rgb565 & (r_mask | g_mask) |
| 91 | + bitmap[i, j + palette_height * 5] = rgb565 & r_mask |
| 92 | + bitmap[i, j + palette_height * 6] = rgb565 & (r_mask | b_mask) |
| 93 | + bitmap[i, j + palette_height * 7] = rgb565 |
| 94 | + |
| 95 | +while True: |
| 96 | + if ft.touched: |
| 97 | + try: |
| 98 | + for touch in ft.touches: |
| 99 | + x = touch["x"] |
| 100 | + y = touch["y"] |
| 101 | + if x < palette_width: |
| 102 | + current_color = bitmap[x, y] |
| 103 | + else: |
| 104 | + for i in range(pixel_size): |
| 105 | + for j in range(pixel_size): |
| 106 | + x_pixel = x - (pixel_size // 2) + i |
| 107 | + y_pixel = y - (pixel_size // 2) + j |
| 108 | + if ( |
| 109 | + 0 <= x_pixel < display.width |
| 110 | + and 0 <= y_pixel < display.height |
| 111 | + ): |
| 112 | + bitmap[x_pixel, y_pixel] = current_color |
| 113 | + except RuntimeError: |
| 114 | + pass |
0 commit comments