320x240 ILI9341 SPI TFT drived with ESP32-S3/CircuitPython

ESP32-S3-DevKitC-1/CircuitPython 7.3.2 exercise to drive 2.4" 320x240 ILI9341 SPI TFT using displayio.

Libraries needed:
- adafruit_display_text folder
- adafruit_ili9341.mpy

If you don't know how to download Adafruit CircuitPython Library Bundle, read Download and install CircuitPython Library to CIRCUITPY drive's lib folder.

Exercise code:

cpyS3_ili9341_simpletest.py, modified from CircuitPython Library Bundle's example ili9341_simpletest.py.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This test will initialize the display using displayio and draw a solid green
background, a smaller purple rectangle, and some yellow text. All drawing is done
using native displayio modules.

Pinouts are for the 2.4" TFT FeatherWing or Breakout with a Feather M4 or M0.
"""
import board
import terminalio
import displayio
from adafruit_display_text import label
import adafruit_ili9341
import busio

# Release any resources currently in use for the displays
displayio.release_displays()

#===init display ======================
disp_width = 320
disp_height = 240

disp_cs  = board.IO4
disp_res = board.IO5
disp_dc  = board.IO6

disp_mosi = board.IO7
disp_clk = board.IO15
disp_spi = busio.SPI(clock=disp_clk,
                     MOSI=disp_mosi)

display_bus = displayio.FourWire(
    disp_spi,
    command=disp_dc,
    chip_select=disp_cs,
    reset=disp_res
)

display = adafruit_ili9341.ILI9341(display_bus,
                                   width=disp_width,
                                   height=disp_height)
#=======================================

# Make the display context
splash = displayio.Group()
display.show(splash)

# Draw a green background
color_bitmap = displayio.Bitmap(320, 240, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x00FF00  # Bright Green

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)

splash.append(bg_sprite)

# Draw a smaller inner rectangle
inner_bitmap = displayio.Bitmap(280, 200, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0xAA0088  # Purple
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=20, y=20)
splash.append(inner_sprite)

# Draw a label
text_group = displayio.Group(scale=3, x=57, y=120)
text = "Hello World!"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00)
text_group.append(text_area)  # Subgroup for text scaling
splash.append(text_group)

while True:
    pass


cpyS3_ili9341_RGB.py, used to verify color.
"""
ESP32-S3-DevKitC-1/CircuitPython exercise
with 2.4 inch 320x240 ILI9341 SPI TFT

used to varify color:
The code display a single color background only,
varify color by changing color_palette[0]
"""
import board
import terminalio
import displayio
from adafruit_display_text import label
import adafruit_ili9341
import busio

# Release any resources currently in use for the displays
displayio.release_displays()

#===init display ======================
disp_width = 320
disp_height = 240

disp_cs  = board.IO4
disp_res = board.IO5
disp_dc  = board.IO6

disp_mosi = board.IO7
disp_clk = board.IO15
disp_spi = busio.SPI(clock=disp_clk,
                     MOSI=disp_mosi)

display_bus = displayio.FourWire(
    disp_spi,
    command=disp_dc,
    chip_select=disp_cs,
    reset=disp_res
)

display = adafruit_ili9341.ILI9341(display_bus,
                                   width=disp_width,
                                   height=disp_height)
#=======================================

COLOR_WHITE = 0xFFFFFF
COLOR_BLACK = 0x000000
COLOR_RED   = 0xFF0000
COLOR_GREEN = 0x00FF00
COLOR_BLUE  = 0x0000FF

# Make the display context
splash = displayio.Group()
display.show(splash)

# Draw a green background
color_bitmap = displayio.Bitmap(320, 240, 1)
color_palette = displayio.Palette(1)

# Change color_palette[0] to varify color
color_palette[0] = COLOR_GREEN

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)

splash.append(bg_sprite)




cpyS3_ili9341_color.py, color demo.
"""
ESP32-S3-DevKitC-1/CircuitPython exercise
with 2.4 inch 320x240 ILI9341 SPI TFT

color demo by changing palette
"""
import board
import terminalio
import displayio
from adafruit_display_text import label
import adafruit_ili9341
import busio
import time

# Release any resources currently in use for the displays
displayio.release_displays()

print(adafruit_ili9341.__name__,
      adafruit_ili9341.__version__)

#===init display ======================
disp_width = 320
disp_height = 240

disp_cs  = board.IO4
disp_res = board.IO5
disp_dc  = board.IO6

disp_mosi = board.IO7
disp_clk = board.IO15
disp_spi = busio.SPI(clock=disp_clk,
                     MOSI=disp_mosi)

display_bus = displayio.FourWire(
    disp_spi,
    command=disp_dc,
    chip_select=disp_cs,
    reset=disp_res
)

display = adafruit_ili9341.ILI9341(display_bus,
                                   width=disp_width,
                                   height=disp_height)
#=======================================

# Make the display context
splash = displayio.Group()
display.show(splash)

NUM_OF_DIV = 8
COLOR_WHITE = 0xFFFFFF
COLOR_BLACK = 0x000000

num_of_color = NUM_OF_DIV*NUM_OF_DIV

# Draw a green background
color_bitmap = displayio.Bitmap(disp_width, disp_height, num_of_color)
color_palette = displayio.Palette(num_of_color)
#color_palette[0] = COLOR_BLACK

col_per_div = int(256/NUM_OF_DIV)

div_width = int(disp_width/NUM_OF_DIV)
div_height = int(disp_height/NUM_OF_DIV)

# prepare color_palette - GREEN x BLUE
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(i*col_per_div*0x0100
                                             + j*col_per_div)

#prepare color_bitmap
for j in range(NUM_OF_DIV):
    for i in range(NUM_OF_DIV):
        for y in range(5, div_height-5):
            for x in range(5, div_width-5):
                p = j*NUM_OF_DIV + i
                #print(i, j, p)
                color_bitmap[i*div_width+x, j*div_height+y] = p

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)

splash.append(bg_sprite)

time.sleep(3)
# change color_palette - RED x GREEN
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(i*col_per_div*0x010000
                                             + j*col_per_div*0x0100)

time.sleep(3)
# change color_palette - BLUE x RED
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(i*col_per_div
                                             + j*col_per_div*0x010000)

time.sleep(3)
# change color_palette - RED only
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(j*col_per_div*0x010000)
        
time.sleep(3)
# change color_palette - GREEN only
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(j*col_per_div*0x0100)
       
time.sleep(3)
# change color_palette - BLUE only
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(j*col_per_div)
       
time.sleep(3)
# change color_palette - gray
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(j*col_per_div*0x010000 +
                                             j*col_per_div*0x0100 +
                                             j*col_per_div)       
       
time.sleep(3)

#while True:
#    pass


Next:
ESP32-S3/CircuitPython, generate QR Code, display on ILI9341 SPI TFT.


Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator