CircuitPython to download bmp images from web, and display on ILI9341 SPI TFT.

Last post show ESP32-S3 running CircuitPython 9.0.3 to display on ILI9341 SPI LCD, detect FT6336 Cap. Touch, and load bmp from SD card using adafruit_imageload.

The post show exercise to download bmp images from internet and display on ILI9341 SPI LCD. Basically, the network part follow the CircuitPython  adafruit_imageload example imageload_from_web.py.


In the original example, bmp file is downloaded from https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_ImageLoad/main/examples/images/4bit.bmp. In this exercise, my own Python http server was setup on Raspberry Pi 5/64-bit Raspberry Pi OS (bookworm), to store my bmp images. Enter the command in Terminal:
python3 -m http.server
Or save it in a executable shell script as shown in the video.

Exercise code:

cpyS3_ili9341_imageload_from_web.py
"""
CircuitPython exercise run on ESP32-S3,
to load image from web and display on ILI9341 SPI LCD.

ref:
https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad/blob/main/examples/imageload_from_web.py
---------------------------

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
- adafruit_requests.mpy
- adafruit_connection_manager.py

"""
import os, sys
import board
import busio
import terminalio
import displayio
import fourwire
import adafruit_ili9341

import adafruit_imageload

import wifi
import socketpool
import adafruit_requests as requests
import ssl
from io import BytesIO

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

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()

# 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] = 0x0000FF # Blue
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)

# Prepare WiFi connection
# To simplify, both ssd and password are hard coded here.
wifi.radio.connect("ssid", "password")

print("Connected -", wifi.radio.ap_info.ssid)
print("My IP address is", wifi.radio.ipv4_address)

socket = socketpool.SocketPool(wifi.radio)
https = requests.Session(socket, ssl.create_default_context())

# pylint: disable=line-too-long
# sample bitmap from Adafruit_CircuitPython_ImageLoad example
url = "https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_ImageLoad/main/examples/images/4bit.bmp"
# load from my web sarver
#url = "http://192.168.67.140:8000/image_01.bmp"

print("Fetching text from %s" % url)
response = https.get(url)
print("GET complete")

bytes_img = BytesIO(response.content)
image, palette = adafruit_imageload.load(bytes_img)

print("========================")
print("image:")
print("image.width:", image.width)
print("image.height:", image.height)
print("image.bits_per_value:", image.bits_per_value)


img_sprite = displayio.TileGrid(image, pixel_shader=palette,
                                x=int((display.width-image.width)/2),
                                y=int((display.height-image.height)/2))
bgGroup.append(img_sprite)

print()
print("~ bye ~")


cpyS3_ili9341_imageload_from_web_loop.py, loop to download and display number of bmp repeatly.
"""
CircuitPython exercise run on ESP32-S3,
to load image from web and display on ILI9341 SPI LCD, repeatly.

ref:
https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad/blob/main/examples/imageload_from_web.py
---------------------------

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
- adafruit_requests.mpy
- adafruit_connection_manager.py

"""
import os, sys
import board
import busio
import terminalio
import displayio
import fourwire
import adafruit_ili9341

import adafruit_imageload

import wifi
import socketpool
import adafruit_requests as requests
import ssl
from io import BytesIO

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

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()

# 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] = 0x0000FF # Blue
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)

# Prepare WiFi connection
# To simplify, both ssd and password are hard coded here.
wifi.radio.connect("ssid", "password")

print("Connected -", wifi.radio.ap_info.ssid)
print("My IP address is", wifi.radio.ipv4_address)

socket = socketpool.SocketPool(wifi.radio)
https = requests.Session(socket, ssl.create_default_context())

images_url = "http://192.168.67.140:8000/"
bmpFiles = ["image_01.bmp", "image_02.bmp", "image_03.bmp", "image_04.bmp", "image_05.bmp"]
image_Tile = None

while True:
    for b in bmpFiles:
        url = images_url+b
        print("Fetching text from %s" % url)
        response = https.get(url)
        print("GET complete")
        
        bytes_img = BytesIO(response.content)
        image, palette = adafruit_imageload.load(bytes_img)
        
        if image_Tile is None:
            image_Tile = displayio.TileGrid(image, pixel_shader=palette, x=0, y=0)
            bgGroup.append(image_Tile)
        else:
            image_Tile.bitmap = image
            image_Tile.pixel_shader = palette

print()
print("~ bye ~")



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