3.2" 320x240 IPS LCD (ILI9341 SPI) with Cap. Touch (FT6336) and Micro SD Slot on ESP32-S3/CircuitPython 9.0.3

Exercise to work with 3.2" 320x240 IPS LCD (ILI9341 SPI) with Cap. Touch (FT6336) and Micro SD Slot, on Espressif ESP32-S3-DevKitC-1 running CircuitPython 9.0.3.


Connection:

ESP32-S3-DevKitC-1
			+---USB-----USB---+
			|GND	    GND	  |
			|GND	    5V0   |
			|GPIO19	    GPIO14|
			|GPIO20	    GPIO13|
			|GPIO21	    GPIO12| 
			|GPIO47	    GPIO11| 
			|GPIO48	    GPIO10| 
			|GPIO45	    GPIO9 | 
			|GPIO0	    GPIO46|
			|GPIO35	    GPIO3 | 
			|GPIO36	    GPIO8 | 
			|GPIO37	    GPIO18|
			|GPIO38	    GPIO17| LCD_CS
			|GPIO39	    GPIO16| LCD_RST
			|GPIO40	    GPIO15| LCD_RS
			|GPIO41	    GPIO7 | SDI (MOSI)
		CTP_SCL	|GPIO42	    GPIO6 | SCK
		CTP_RST	|GPIO2	    GPIO5 | LED
		CTP_SDA	|GPIO1	    GPIO4 | SDO (MISO)
		CTP_INT	|GPIO44	    RST	  |
		SD_CS	|GPIO43	    3V3	  |
		GND	|GND	    3V3	  | 3V3
			+-----------------+
				
ILI9341 Module   
1 	VCC		3V3
2 	GND		GND
3 	LCD_CS		GPIO17
4 	LCD_RST		GPIO16
5 	LCD_RS		GPIO15
6 	SDI(MOSI)	GPIO7
7 	SCK		GPIO6
8 	LED		GPIO5
9 	SDO(MISO)	GPIO4
10 	CTP_SCL		GPIO42
11 	CTP_RST		GPIO2
12 	CTP_SDA		GPIO1
13 	CTP_INT		GPIO44
14 	SD_CS		GPIO43


Exercise Code:

cpyS3_ili9341_color.py
"""
CircuitPython exercise run on ESP32-S3,
to display on IPS with ILI9341 SPI driver
and capacitive touch FT6336U.

Color and display area test
---------------------------

Display Module:
3.2 inch IPS SPI Module ILI9341 with cap. touch FT6336U

Test on:
dev. board:     Espressif ESP32-S3-DevKitC-1
CircuitPython:  9.0.3

Libraries needed:
- adafruit_ili9341.mpy
- adafruit_display_text folder

* No invert option for adafruit_ili9341 driver.
  To invert color, send command of 0x21.
  Follow Adafruit_ILI9341 library (Arduino framework), invertDisplay() function
  https://github.com/adafruit/Adafruit_ILI9341/blob/master/Adafruit_ILI9341.cpp
  
"""
import os, sys
import board
import busio
import terminalio
import displayio
import fourwire
from adafruit_display_text import label
import adafruit_ili9341

import time

ILI9341_INVOFF = 0x20   #Display Inversion OFF
ILI9341_INVON  = 0x21   #Display Inversion ON

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

#===init display ======================
#Connection to ILI9341/SD
DISP_CS  = board.IO17
DISP_RES = board.IO16
DISP_DC  = board.IO15
SPI_MOSI = board.IO7
SPI_SCL  = board.IO6
DISP_BL  = board.IO5
SPI_MISO = board.IO4
SD_CS    = board.IO43

spi = busio.SPI(clock=SPI_SCL,
                MOSI=SPI_MOSI,
                MISO=SPI_MISO)

#On CircuitPython 9  FourWire moved from displayio to fourwire
display_bus = fourwire.FourWire(spi, command=DISP_DC, chip_select=DISP_CS, reset=DISP_RES)

#--- Setup display ---
# for 3.2" 320x240 SPI ILI9341 TFT
disp_width = 320
disp_height = 240
display = adafruit_ili9341.ILI9341(display_bus,
                                   width=disp_width,
                                   height=disp_height,
                                   backlight_pin=DISP_BL,
                                   )

display_bus.send(int(ILI9341_INVON), "") #invert color

sysinfo = sys.implementation[0] + " " + os.uname()[3] + "\nrun on " + os.uname()[4]
drv_info = adafruit_ili9341.__name__ + " " + adafruit_ili9341.__version__
print("=============================================")
print("coXXect.blogspot.com")
print("---------------------------------------------")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("=============================================")
print(drv_info)
time.sleep(1)
# Make the display context
bgGroup = displayio.Group()

display.root_group = bgGroup

bg_bitmap = displayio.Bitmap(display.width, display.height, 1)  # with one color
bg_palette = displayio.Palette(1)
bg_palette[0] = 0xFFFFFF # White
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)

colorSet = ((0xFF0000, "RED"),
            (0x00FF00, "GREEN"),
            (0x0000FF, "BLUE"),
            (0xFFFFFF, "WHITE"),
            (0x000000, "BLACK"))

border = 1
color_bitmap = displayio.Bitmap(display.width-border*2, display.height-border*2, 1)  # with one color
color_palette = displayio.Palette(1)
color_palette[0] = 0x000000  # BLACK
color_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=border, y=border)
bgGroup.append(color_sprite)

# Draw label
text_group = displayio.Group(scale=2, x=10, y=120)
text = "coXXect.blogspot.com"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
text_group.append(text_area)  # Subgroup for text scaling
bgGroup.append(text_group)

info_group = displayio.Group(scale=1, x=10, y=200)
info_label = label.Label(terminalio.FONT,
                         text=sysinfo + "\n" + drv_info,
                         color=0xFFFFFF)
info_group.append(info_label)  # Subgroup for text scaling
bgGroup.append(info_group)

time.sleep(2)
text_area.text = "coXXect"
text_area.color = 0xFFFF00  #yellow
text_group.scale = 5

def brightness_dec():
    for b in range(256):
        time.sleep(0.02)
        display.brightness=(255-b)/255
        
def brightness_inc():
    for b in range(256):
        time.sleep(0.02)
        display.brightness=b/255
    
for i in colorSet:
    brightness_dec()
    time.sleep(2)
    color_palette[0] = i[0]
    text_area.text = i[1]
    text_area.color = i[0] ^ 0xFFFFFF
    brightness_inc()
    
brightness_dec()
text_area.text = "coxxect.blogspot.com/"
text_group.scale = 2
brightness_inc()

print("~ bye ~")


cpyS3_ili9341_FT6336.py
"""
CircuitPython exercise run on ESP32-S3,
to display on IPS with ILI9341 SPI driver
and capacitive touch FT6336U.

Touch test
Display cursor following touch point
and adjust screen brightness according to touched horizontal position.
---------------------------

Display Module:
3.2 inch IPS SPI Module ILI9341 with cap. touch FT6336U

Test on:
dev. board:     Espressif ESP32-S3-DevKitC-1
CircuitPython:  9.0.3

Libraries needed:
- adafruit_ili9341.mpy
- adafruit_display_text folder
- adafruit_focaltouch.mpy
- adafruit_cursorcontrol folder

* No invert option for adafruit_ili9341 driver.
  To invert color, send command of 0x21.
* with rotation=0, have to adjust touch x/y to project on screen.
"""
import os, sys
import board
import busio
import terminalio
import displayio
import fourwire
from adafruit_display_text import label
import adafruit_ili9341

import time

from digitalio import DigitalInOut, Direction
import adafruit_focaltouch
import adafruit_cursorcontrol.cursorcontrol

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

#===init display ======================
#Connection to ILI9341/SD
DISP_CS  = board.IO17
DISP_RES = board.IO16
DISP_DC  = board.IO15
SPI_MOSI = board.IO7
SPI_SCL  = board.IO6
DISP_BL  = board.IO5
SPI_MISO = board.IO4
SD_CS    = board.IO43

#Connection to FT6336U
TOUCH_SCL = board.IO42
TOUCH_SDA = board.IO1
TOUCH_INT = board.IO44
TOUCH_RST = board.IO2

touch_irq = DigitalInOut(TOUCH_INT)
touch_irq.direction = Direction.INPUT

touch_res = DigitalInOut(TOUCH_RST)
touch_res.direction = Direction.OUTPUT
touch_res.value = True
time.sleep(0.1)
touch_res.value = False
time.sleep(0.1)
touch_res.value = True
time.sleep(0.5)

spi = busio.SPI(clock=SPI_SCL,
                MOSI=SPI_MOSI,
                MISO=SPI_MISO)

#On CircuitPython 9  FourWire moved from displayio to fourwire
display_bus = fourwire.FourWire(spi, command=DISP_DC, chip_select=DISP_CS, reset=DISP_RES)

touch_i2c = busio.I2C(TOUCH_SCL, TOUCH_SDA)
ft = adafruit_focaltouch.Adafruit_FocalTouch(touch_i2c, debug=False, irq_pin=touch_irq)

#--- Setup display ---
# for 3.2" 320x240 SPI ILI9341 TFT
disp_width = 320
disp_height = 240
display = adafruit_ili9341.ILI9341(display_bus,
                                   width=disp_width,
                                   height=disp_height,
                                   backlight_pin=DISP_BL,
                                   )

display_bus.send(int(0x21), "") #invert color

sysinfo = sys.implementation[0] + " " + os.uname()[3] + "\nrun on " + os.uname()[4]
drv_info = adafruit_ili9341.__name__ + " " + adafruit_ili9341.__version__ + "\n" + \
           adafruit_focaltouch.__name__ + " " + adafruit_focaltouch.__version__ + "\n" + \
           adafruit_cursorcontrol.__name__ + " " + adafruit_cursorcontrol.__version__
print("=============================================")
print("coXXect.blogspot.com")
print("---------------------------------------------")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("=============================================")
print(drv_info)
time.sleep(1)
# Make the display context
bgGroup = displayio.Group()

display.root_group = bgGroup

bg_bitmap = displayio.Bitmap(display.width, display.height, 1)  # with one color
bg_palette = displayio.Palette(1)
bg_palette[0] = 0xFFFFFF # White
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)

border = 1
color_bitmap = displayio.Bitmap(display.width-border*2, display.height-border*2, 1)  # with one color
color_palette = displayio.Palette(1)
color_palette[0] = 0x000000  # BLACK
color_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=border, y=border)
bgGroup.append(color_sprite)

# Draw label
text_group = displayio.Group(scale=2, x=10, y=120)
text = "coXXect.blogspot.com"
text_area = label.Label(terminalio.FONT, text=text, color=0xFF0000)
text_group.append(text_area)  # Subgroup for text scaling
bgGroup.append(text_group)

# Draw labels to display touch x/y
x_group = displayio.Group(scale=2, x=10, y=20)
x_text = "---"
x_area = label.Label(terminalio.FONT, text=x_text, color=0xFFFFFF)
x_group.append(x_area)
bgGroup.append(x_group)
y_group = displayio.Group(scale=2, x=10, y=40)
y_text = "---"
y_area = label.Label(terminalio.FONT, text=y_text, color=0xFFFFFF)
y_group.append(y_area)
bgGroup.append(y_group)

info_group = displayio.Group(scale=1, x=10, y=160)
info_label = label.Label(terminalio.FONT,
                         text=sysinfo + "\n" + drv_info,
                         color=0xFFFF00)
info_group.append(info_label)  # Subgroup for text scaling
bgGroup.append(info_group)

# initialize the mouse cursor object
# place over all others
mouse_cursor = adafruit_cursorcontrol.cursorcontrol.Cursor(
    display, display_group=bgGroup)
mouse_cursor.x = int(display.width/2)
mouse_cursor.y = int(display.height/2)

while True:
    # if the screen is being touched print the touches
    if ft.touched:
        touches = ft.touches
        
        #sometimes ft.touches is [], empty!
        #so I check len to prevent IndexError: index out of range
        if len(touches)>0:
            #print(touches)
            x = touches[0]['x']
            y = touches[0]['y']

            #with rotation=0, have to adjust touch x/y to project on screen.
            mouse_cursor.x = y
            mouse_cursor.y = display.height-x
            x_area.text = str(x)
            y_area.text = str(y)

            #change screen brightness according to touch horizontal position
            display.brightness = y/display.width
    else:
        print('no touch')

    time.sleep(.3)


print("~ bye ~")


cpyS3_ili9341_FT6336_270.py
"""
CircuitPython exercise run on ESP32-S3,
to display on IPS with ILI9341 SPI driver
and capacitive touch FT6336U.

Touch test
Display cursor following touch point
and adjust screen brightness according to touched horizontal position.
---------------------------

Display Module:
3.2 inch IPS SPI Module ILI9341 with cap. touch FT6336U

Test on:
dev. board:     Espressif ESP32-S3-DevKitC-1
CircuitPython:  9.0.3

Libraries needed:
- adafruit_ili9341.mpy
- adafruit_display_text folder
- adafruit_focaltouch.mpy
- adafruit_cursorcontrol folder

* No invert option for adafruit_ili9341 driver.
  To invert color, send command of 0x21.
* with rotation=270, touch x/y no adjustment needed.
"""
import os, sys
import board
import busio
import terminalio
import displayio
import fourwire
from adafruit_display_text import label
import adafruit_ili9341

import time

from digitalio import DigitalInOut, Direction
import adafruit_focaltouch
import adafruit_cursorcontrol.cursorcontrol

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

#===init display ======================
#Connection to ILI9341/SD
DISP_CS  = board.IO17
DISP_RES = board.IO16
DISP_DC  = board.IO15
SPI_MOSI = board.IO7
SPI_SCL  = board.IO6
DISP_BL  = board.IO5
SPI_MISO = board.IO4
SD_CS    = board.IO43

#Connection to FT6336U
TOUCH_SCL = board.IO42
TOUCH_SDA = board.IO1
TOUCH_INT = board.IO44
TOUCH_RST = board.IO2

touch_irq = DigitalInOut(TOUCH_INT)
touch_irq.direction = Direction.INPUT

touch_res = DigitalInOut(TOUCH_RST)
touch_res.direction = Direction.OUTPUT
touch_res.value = True
time.sleep(0.1)
touch_res.value = False
time.sleep(0.1)
touch_res.value = True
time.sleep(0.5)

spi = busio.SPI(clock=SPI_SCL,
                MOSI=SPI_MOSI,
                MISO=SPI_MISO)

#On CircuitPython 9  FourWire moved from displayio to fourwire
display_bus = fourwire.FourWire(spi, command=DISP_DC, chip_select=DISP_CS, reset=DISP_RES)

touch_i2c = busio.I2C(TOUCH_SCL, TOUCH_SDA)
ft = adafruit_focaltouch.Adafruit_FocalTouch(touch_i2c, debug=False, irq_pin=touch_irq)

#--- Setup display ---
# for 3.2" 320x240 SPI ILI9341 TFT
disp_width = 240
disp_height = 320
display = adafruit_ili9341.ILI9341(display_bus,
                                   width=disp_width,
                                   height=disp_height,
                                   backlight_pin=DISP_BL,
                                   rotation=270
                                   )
display_bus.send(int(0x21), "") #invert color

sysinfo = sys.implementation[0] + " " + os.uname()[3] + "\nrun on " + os.uname()[4]
drv_info = adafruit_ili9341.__name__ + " " + adafruit_ili9341.__version__ + "\n" + \
           adafruit_focaltouch.__name__ + " " + adafruit_focaltouch.__version__ + "\n" + \
           adafruit_cursorcontrol.__name__ + " " + adafruit_cursorcontrol.__version__
print("=============================================")
print("coXXect.blogspot.com")
print("---------------------------------------------")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("=============================================")
print(drv_info)
time.sleep(1)
# Make the display context
bgGroup = displayio.Group()

display.root_group = bgGroup

bg_bitmap = displayio.Bitmap(display.width, display.height, 1)  # with one color
bg_palette = displayio.Palette(1)
bg_palette[0] = 0xFFFFFF # White
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)

border = 1
color_bitmap = displayio.Bitmap(display.width-border*2, display.height-border*2, 1)  # with one color
color_palette = displayio.Palette(1)
color_palette[0] = 0x000000  # BLACK
color_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=border, y=border)
bgGroup.append(color_sprite)

# Draw label
text_group = displayio.Group(scale=2, x=10, y=120)
text = "coXXect.blogspot.com"
text_area = label.Label(terminalio.FONT, text=text, color=0xFF0000)
text_group.append(text_area)  # Subgroup for text scaling
bgGroup.append(text_group)

# Draw labels to display touch x/y
x_group = displayio.Group(scale=2, x=10, y=20)
x_text = "---"
x_area = label.Label(terminalio.FONT, text=x_text, color=0xFFFFFF)
x_group.append(x_area)
bgGroup.append(x_group)
y_group = displayio.Group(scale=2, x=10, y=40)
y_text = "---"
y_area = label.Label(terminalio.FONT, text=y_text, color=0xFFFFFF)
y_group.append(y_area)
bgGroup.append(y_group)

info_group = displayio.Group(scale=1, x=10, y=180)
info_label = label.Label(terminalio.FONT,
                         text=sysinfo + "\n" + drv_info,
                         color=0xFFFF00)
info_group.append(info_label)  # Subgroup for text scaling
bgGroup.append(info_group)

# initialize the mouse cursor object
# place over all others
mouse_cursor = adafruit_cursorcontrol.cursorcontrol.Cursor(
    display, display_group=bgGroup)
mouse_cursor.x = int(display.width/2)
mouse_cursor.y = int(display.height/2)

while True:
    # if the screen is being touched print the touches
    if ft.touched:
        touches = ft.touches
        
        #sometimes ft.touches is [], empty!
        #so I check len to prevent IndexError: index out of range
        if len(touches)>0:
            #print(touches)
            x = touches[0]['x']
            y = touches[0]['y']

            #with rotation=270, touch x/y no adjustment needed.
            mouse_cursor.x = x
            mouse_cursor.y = y
            x_area.text = str(x)
            y_area.text = str(y)

            #change screen brightness according to touch vertical position
            display.brightness = y/display.height
    else:
        print('no touch')

    time.sleep(.3)


print("~ bye ~")


cpyS3_ili9341_SDbmp.py
"""
CircuitPython exercise run on ESP32-S3,
to display on IPS with ILI9341 SPI driver
and capacitive touch FT6336U.

adafruit_imageload exercise:
load bmp from SD card and display on ILI9341
ref: https://docs.circuitpython.org/projects/imageload/
     https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad
---------------------------

Display Module:
3.2 inch IPS SPI Module ILI9341 with cap. touch FT6336U

Test on:
dev. board:     Espressif ESP32-S3-DevKitC-1
CircuitPython:  9.0.3

Libraries needed:
- adafruit_ili9341.mpy
- import adafruit_imageload folder

* No invert option for adafruit_ili9341 driver.
  To invert color, send command of 0x21.
  Follow Adafruit_ILI9341 library (Arduino framework), invertDisplay() function
  https://github.com/adafruit/Adafruit_ILI9341/blob/master/Adafruit_ILI9341.cpp
  
"""
import os, sys
import board
import busio
import terminalio
import displayio
import fourwire
import adafruit_ili9341

import sdcardio
import storage
import adafruit_imageload

ILI9341_INVOFF = 0x20   #Display Inversion OFF
ILI9341_INVON  = 0x21   #Display Inversion ON

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

#===init display ======================
#Connection to ILI9341/SD
DISP_CS  = board.IO17
DISP_RES = board.IO16
DISP_DC  = board.IO15
SPI_MOSI = board.IO7
SPI_SCL  = board.IO6
DISP_BL  = board.IO5
SPI_MISO = board.IO4
SD_CS    = board.IO43

spi = busio.SPI(clock=SPI_SCL,
                MOSI=SPI_MOSI,
                MISO=SPI_MISO)

#On CircuitPython 9  FourWire moved from displayio to fourwire
display_bus = fourwire.FourWire(spi, command=DISP_DC, chip_select=DISP_CS, reset=DISP_RES)

#--- Setup display ---
# for 3.2" 320x240 SPI ILI9341 TFT
disp_width = 320
disp_height = 240
display = adafruit_ili9341.ILI9341(display_bus,
                                   width=disp_width,
                                   height=disp_height,
                                   backlight_pin=DISP_BL,
                                   )

display_bus.send(int(ILI9341_INVON), "") #invert color
isInverted = True

#Setup SDCard
sd = sdcardio.SDCard(spi, SD_CS)

sysinfo = sys.implementation[0] + " " + os.uname()[3] + "\nrun on " + os.uname()[4]
drv_info = adafruit_ili9341.__name__ + " " + adafruit_ili9341.__version__ + "\n" + \
           adafruit_imageload.__name__ + " " + adafruit_imageload.__version__
print("=============================================")
print("coXXect.blogspot.com")
print("---------------------------------------------")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("=============================================")
print(drv_info)
print()


sd_path = "/sd"
vfs = storage.VfsFat(sd)
storage.mount(vfs, sd_path)
"""
print("List Directory in SD:")
for f in os.listdir(sd_path):
    print(f)
print()
"""

bmpFiles = ["image_01.bmp", "image_02.bmp", "image_03.bmp", "image_04.bmp", "image_05.bmp"]

# Make the display context
bgGroup = displayio.Group()

display.root_group = bgGroup

image, palette = adafruit_imageload.load("/sd/image_01.bmp",
                                          bitmap=displayio.Bitmap,
                                          palette=displayio.Palette)

image_TileGrid = displayio.TileGrid(image, pixel_shader=palette)
bgGroup.append(image_TileGrid)

while True:
    for b in bmpFiles:
        bmpfile = sd_path + "/" + b
        #print("bitmap file:", bmpfile)
        image, palette = adafruit_imageload.load(bmpfile,
                                                 bitmap=displayio.Bitmap,
                                                 palette=displayio.Palette)
        image_TileGrid.bitmap = image
        image_TileGrid.pixel_shader = palette
    
    #Toggle display inversion
    if isInverted:
        display_bus.send(int(ILI9341_INVOFF), "") #Display Inversion OFF
        isInverted = False
    else:
        display_bus.send(int(ILI9341_INVON), "")  #Display Inversion ON
        isInverted = True


print()
print("~ bye ~")

To prepare your bitmaps, read the post Convert image to 24-bit color BMPs using GIMP.

next:
CircuitPython to download bmp images from web, and display on ILI9341 SPI TFT.
ESP-NOW on CircuitPython 9.0.3, tested on ESP32-S3/ESP32-C3.


More exercise:

To draw line and rect:


cpyS3_ili9341_sq.py
"""
CircuitPython exercise run on ESP32-S3,
to display on IPS with ILI9341 SPI driver.

details and code:
https://coxxect.blogspot.com/2024/04/32-320x240-ips-lcd-ili9341-spi-with-cap.html

CircuitPython exercise to draw line and rectangle.
---------------------------

Display Module:
3.2 inch IPS SPI Module ILI9341 with cap. touch FT6336U

Test on:
dev. board:     Espressif ESP32-S3-DevKitC-1
CircuitPython:  9.0.4

Libraries needed:
- adafruit_ili9341.mpy
- adafruit_display_text folder
- adafruit_display_shapes folder
  
"""
import os, sys
import board
import busio
import terminalio
import displayio
import fourwire
from adafruit_display_text import label
from adafruit_display_shapes.rect import Rect
import adafruit_ili9341
import bitmaptools

import time

ILI9341_INVOFF = 0x20   #Display Inversion OFF
ILI9341_INVON  = 0x21   #Display Inversion ON

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

#===init display ======================
#Connection to ILI9341/SD
DISP_CS  = board.IO17
DISP_RES = board.IO16
DISP_DC  = board.IO15
SPI_MOSI = board.IO7
SPI_SCL  = board.IO6
DISP_BL  = board.IO5
SPI_MISO = board.IO4
SD_CS    = board.IO43

spi = busio.SPI(clock=SPI_SCL,
                MOSI=SPI_MOSI,
                MISO=SPI_MISO)

#On CircuitPython 9  FourWire moved from displayio to fourwire
display_bus = fourwire.FourWire(spi, command=DISP_DC, chip_select=DISP_CS, reset=DISP_RES)

#--- Setup display ---
# for 3.2" 320x240 SPI ILI9341 TFT
disp_width = 320
disp_height = 240
display = adafruit_ili9341.ILI9341(display_bus,
                                   width=disp_width,
                                   height=disp_height,
                                   backlight_pin=DISP_BL,
                                   )

display_bus.send(int(ILI9341_INVON), "") #invert color

sysinfo = sys.implementation[0] + " " + os.uname()[3] + "\nrun on " + os.uname()[4]
drv_info = adafruit_ili9341.__name__ + " " + adafruit_ili9341.__version__
print("=============================================")
print("coXXect.blogspot.com")
print("---------------------------------------------")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("=============================================")
print(drv_info)
time.sleep(1)
# Make the display context
bgGroup = displayio.Group()

display.root_group = bgGroup

bg_bitmap = displayio.Bitmap(display.width, display.height, 2)
bg_palette = displayio.Palette(2)
bg_palette[0] = 0x000000 # Black
bg_palette[1] = 0xFFFFFF # White
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)

# Draw label
text_group = displayio.Group(scale=2, x=10, y=120)
text = "coXXect.blogspot.com"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
text_group.append(text_area)  # Subgroup for text scaling
bgGroup.append(text_group)

info_group = displayio.Group(scale=1, x=10, y=200)
info_label = label.Label(terminalio.FONT,
                         text=sysinfo + "\n" + drv_info,
                         color=0xFFFFFF)
info_group.append(info_label)  # Subgroup for text scaling
bgGroup.append(info_group)

time.sleep(3)

bgGroup.remove(text_group)
bgGroup.remove(info_group)


"""
bitmaptools exercise to draw line on bitmap
bitmaptools.draw_line(dest_bitmap: displayio.Bitmap, x1: int, y1: int, x2: int, y2: int, value: int)
https://docs.circuitpython.org/en/latest/shared-bindings/bitmaptools/index.html#bitmaptools.draw_line
"""
x = 0
y = 0
x_space = 2
y_space = 2
while x < display.width:
    bitmaptools.draw_line(bg_bitmap, x, 0, x, display.height, 1)
    x = x + x_space
    x_space = x_space+1
    
while y < display.height:
    bitmaptools.draw_line(bg_bitmap, 0, y, display.width, y, 1)
    y = y + y_space
    y_space = y_space+1
    
"""
draw square
adafruit_display_shapes.rect.Rect(...)
https://docs.circuitpython.org/projects/display-shapes/en/latest/api.html#adafruit_display_shapes.rect.Rect
"""

sq_x = 0
sq_y = 0
sq_l = 35
sq_x_dir = 1
sq_y_dir = 1
sq_x_max = display.width-sq_l
sq_y_max = display.height-sq_l
sq = Rect(sq_x, sq_y, sq_l, sq_l, fill=000000, outline=0xFFFFFF)
bgGroup.append(sq)

while True:
    sq_x = sq_x + sq_x_dir
    sq_y = sq_y + sq_y_dir
    sq.x = sq_x
    sq.y = sq_y
    
    if sq_x_dir > 0:
        if sq_x > sq_x_max:
            sq_x_dir = sq_x_dir * -1
    else:
        if sq_x <= 0:
            sq_x_dir = sq_x_dir * -1
            
    if sq_y_dir > 0:
        if sq_y > sq_y_max:
            sq_y_dir = sq_y_dir * -1
    else:
        if sq_y <= 0:
            sq_y_dir = sq_y_dir * -1

    time.sleep(0.02)


print("~ bye ~")

next:
CircuitPython Exercise: Touch buttons on ILI9341 SPI LCD with FT6336 Capacitive Touch.




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