Adsense HTML/JavaScript

Wednesday, December 16, 2020

ESP32-S2/CircuitPython: display on IPS screen (ST7789/SPI) using adafruit_st7789/displayio lib


This post show how to program in CircuitPython run on ESP32-S2(nanoESP32-S2 dev. board) to display on 1.14" 135x240 IPS Screen with ST7789V driver/SPI interface, using adafruit_st7789/displayio library.

The display is 1.14" 135x240 (RGB) IPS (SKU: MSP1141), details refer to : LCDwiki - 1.14inch IPS Module

Connection:


Library:

In this exercise, adafruit_st7789 and adafruit_display_text of Adafruit CircuitPython Library Bundle is needed.

Visit https://circuitpython.org/libraries, download the appropriate bundle for your version of CircuitPython.

Unzip the file, copy adafruit_st7789.mpy and adafruit_display_text folder to the lib folder on your CIRCUITPY drive.

Example code:

Copy below examples code to code.py on CIRCUITPY drive to run.

spiST7789

"""
Example of CircuitPython/ESP32-S2 (run on nanoESP32-S2)
to display on 1.14" 135x240 (RGB) IPS screen
with ST7789 driver via SPI interface.

Connection between nanoESP32 and
the IPS screen, with ST7789 SPI interface.
3V3  - BLK (backlight, always on)
IO37 - CS
IO38 - DC
IO39 - RES
IO40 - SDA
IO41 - SCL
3V3  - VCC
GND  - GND
"""

import os
import board
import time
import terminalio
import displayio
import busio
from adafruit_display_text import label
#from adafruit_st7789 import ST7789
import adafruit_st7789

print("==============================")
print(os.uname())
print("Hello nanoESP32-S2/CircuitPython ST7789 SPI IPS Display")
print(adafruit_st7789.__name__ + " version: " + adafruit_st7789.__version__)
print()

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

tft_cs = board.IO37
tft_dc = board.IO38
tft_res = board.IO39
spi_mosi = board.IO40
spi_clk = board.IO41

"""
classbusio.SPI(clock: microcontroller.Pin,
                MOSI: Optional[microcontroller.Pin] = None,
                MISO: Optional[microcontroller.Pin] = None)
"""
spi = busio.SPI(spi_clk, MOSI=spi_mosi)

display_bus = displayio.FourWire(
    spi, command=tft_dc, chip_select=tft_cs, reset=tft_res
)

#I get the parameters by guessing and trying
#display = ST7789(display_bus, width=135, height=240, rowstart=40, colstart=53)
display = adafruit_st7789.ST7789(display_bus,
                    width=135, height=240,
                    rowstart=40, colstart=53)

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

color_bitmap = displayio.Bitmap(135, 240, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x00FF00

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(133, 238, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x0000FF
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=1, y=1)
splash.append(inner_sprite)

# Draw a label
text_group1 = displayio.Group(max_size=10, scale=2, x=20, y=40)
text1 = "ESP32-S2"
text_area1 = label.Label(terminalio.FONT, text=text1, color=0xFF0000)
text_group1.append(text_area1)  # Subgroup for text scaling
# Draw a label
text_group2 = displayio.Group(max_size=10, scale=1, x=20, y=60)
text2 = "CircuitPython"
text_area2 = label.Label(terminalio.FONT, text=text2, color=0xFFFFFF)
text_group2.append(text_area2)  # Subgroup for text scaling

# Draw a label
text_group3 = displayio.Group(max_size=10, scale=1, x=20, y=100)
text3 = adafruit_st7789.__name__
text_area3 = label.Label(terminalio.FONT, text=text3, color=0x0000000)
text_group3.append(text_area3)  # Subgroup for text scaling
# Draw a label
text_group4 = displayio.Group(max_size=10, scale=2, x=20, y=120)
text4 = adafruit_st7789.__version__
text_area4 = label.Label(terminalio.FONT, text=text4, color=0x000000)
text_group4.append(text_area4)  # Subgroup for text scaling

splash.append(text_group1)
splash.append(text_group2)
splash.append(text_group3)
splash.append(text_group4)

time.sleep(3.0)

rot = 0
while True:
    time.sleep(5.0)
    rot = rot + 90
    if (rot>=360):
        rot =0
    display.rotation = rot
    

spiST7789_bitmap
"""
Example of CircuitPython/ESP32-S2 (run on nanoESP32-S2)
to display on 1.14" 135x240 (RGB) IPS screen
with ST7789 driver via SPI interface.

Connection between nanoESP32 and
the IPS screen, with ST7789 SPI interface.
3V3  - BLK (backlight, always on)
IO37 - CS
IO38 - DC
IO39 - RES
IO40 - SDA
IO41 - SCL
3V3  - VCC
GND  - GND
"""

import os
import board
import time
import terminalio
import displayio
import busio
from adafruit_display_text import label
import adafruit_st7789

print("==============================")
print(os.uname())
print("Hello nanoESP32-S2/CircuitPython ST7789 SPI IPS Display")
print(adafruit_st7789.__name__ + " version: " + adafruit_st7789.__version__)
print()

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

tft_cs = board.IO37
tft_dc = board.IO38
tft_res = board.IO39
spi_mosi = board.IO40
spi_clk = board.IO41

"""
classbusio.SPI(clock: microcontroller.Pin,
                MOSI: Optional[microcontroller.Pin] = None,
                MISO: Optional[microcontroller.Pin] = None)
"""
spi = busio.SPI(spi_clk, MOSI=spi_mosi)

display_bus = displayio.FourWire(
    spi, command=tft_dc, chip_select=tft_cs, reset=tft_res
)

display = adafruit_st7789.ST7789(display_bus,
                    width=135, height=240,
                    rowstart=40, colstart=53)
display.rotation = 270

group = displayio.Group(max_size=10)
display.show(group)

bitmap = displayio.Bitmap(240, 135, 135)

palette = displayio.Palette(135)
for p in range(135):
    palette[p] = (0x010000*p) + (0x0100*p) + p

for y in range(135):
    for x in range(240):
        bitmap[x,y] = y
        
tileGrid = displayio.TileGrid(bitmap, pixel_shader=palette, x=0, y=0)
group.append(tileGrid)

time.sleep(3.0)

while True:
    for p in range(135):
        palette[p] = p
    time.sleep(3.0)

    for p in range(135):
        palette[p] = 0x0100 * p
    time.sleep(3.0)

    for p in range(135):
        palette[p] = 0x010000 * p
    time.sleep(3.0)

spiST7789_terminal
"""
Example of CircuitPython/ESP32-S2 (run on nanoESP32-S2)
to display on 1.14" 135x240 (RGB) IPS screen
with ST7789 driver via SPI interface.

Connection between nanoESP32 and
the IPS screen, with ST7789 SPI interface.
3V3  - BLK (backlight, always on)
IO37 - CS
IO38 - DC
IO39 - RES
IO40 - SDA
IO41 - SCL
3V3  - VCC
GND  - GND
"""

import os
import board
import time
import terminalio
import displayio
import busio
from adafruit_display_text import label
#from adafruit_st7789 import ST7789
import adafruit_st7789

print("==============================")
print(os.uname())
print("Hello nanoESP32-S2/CircuitPython ST7789 SPI IPS Display")
print(adafruit_st7789.__name__ + " version: " + adafruit_st7789.__version__)
print()

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

tft_cs = board.IO37
tft_dc = board.IO38
tft_res = board.IO39
spi_mosi = board.IO40
spi_clk = board.IO41

"""
classbusio.SPI(clock: microcontroller.Pin,
                MOSI: Optional[microcontroller.Pin] = None,
                MISO: Optional[microcontroller.Pin] = None)
"""
spi = busio.SPI(spi_clk, MOSI=spi_mosi)

display_bus = displayio.FourWire(
    spi, command=tft_dc, chip_select=tft_cs, reset=tft_res
)

#I get the parameters by guessing and trying
#display = ST7789(display_bus, width=135, height=240, rowstart=40, colstart=53)
display = adafruit_st7789.ST7789(display_bus,
                    width=135, height=240,
                    rowstart=40, colstart=53)
display.rotation = 270

"""
Now click on to activate Serial Console,
Press any key to enter the REPL.
You would have a terminal that you could type at and have the screen update.
"""


~ More example of ESP32-S2/CircuitPython

No comments:

Post a Comment