Adsense HTML/JavaScript

Friday, September 24, 2021

nanoESP32-S2/CircuitPython 7 + SPI ST7789 LCD (first try CircuitPython 7)

This video show how to flash nanoESP32-S2 with latest stable CircuitPython 7.0.0 firmware, install libraries, and run exercise to display on 2.0" IPS 240x320 RGB LCD with SPI ST7789 driver. It's my first time to run stable CircuitPython 7.


Download and install CircuitPython firmware on nanoESP32-S2

Visit https://circuitpython.org/downloads to download CircuitPython firmware:

Search "ESP32 S2" and select "NanoESP32 S2 w/WROVER".
Download .BIN for CircuitPython 7.0.0, it's the latest stable release of CircuitPython currently.

Now going to identify the USB will be conneted to ESP32-S2:

- Before ESP32-S2 connected, clear dmesg buffer.
$ sudo dmesg -c

- Connect USB to onboard USB port marked "ch340"
- identify connected USB port.
$ dmesg

ttyUSB0 connected in my case.
  
To flash nanoESP32-S2 with CircuitPython firmware:
$ esptool.py --chip auto --port <COM PORT> -b 460800 --before=default_reset \
--after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x0000 \
<DOWNLOADED FILE>
Prepare Libraries

Visit https://circuitpython.org/libraries, download Bundle for Version 7.x.

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

Connection between SPI ST7789 and nanoESP32-S2

         +-----------------------------------+
-----    |                                   |
 GND | --+        -----------------------    |
 VCC | --------- | 3V3		     GND | --+
 SCL | --------- | 0			 |
 SDA | --------- | 1			 |
 RES | --------- | 2	nanoESP32-S2	 |
 DC  | --------- | 3			 |
 CS  | --------- | 4			 |
 BLK | --------- | 5			 |
----
SPI ST7789 LCD

Exercise:

cpyNanoESP32S2_spiST7789.py
"""
Example of CircuitPython 7/nanoESP32-S2
to display on ST7789 SPI IPS Screen

Connection between nanoESP32-S2 and
the IPS screen, with ST7789 SPI interface.
GP5  - BLK
GP4  - CS
GP3  - DC
GP2  - RES
GP1  - SDA
GP0  - 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

import digitalio

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

#assign pins for nanoESP32-S2
tft_blk = board.IO5
tft_cs = board.IO4
tft_dc = board.IO3
tft_res = board.IO2
spi_mosi = board.IO1
spi_clk = board.IO0

pin_blk = digitalio.DigitalInOut(tft_blk)
pin_blk.direction = digitalio.Direction.OUTPUT
pin_blk.value = True

spi = busio.SPI(spi_clk, MOSI=spi_mosi)

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

#for 240x320
display = adafruit_st7789.ST7789(display_bus,
                    width=240, height=320)

#for 240x240
#display = adafruit_st7789.ST7789(display_bus,
#                    width=240, height=240, rowstart=80)

# Make the display context

#Erik: CircuitPython 6 changed to 7
splash = displayio.Group()   #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

#Erik: CircuitPython 6 changed to 7
#text_group1 = displayio.Group(max_size=10, scale=2, x=20, y=40)
text_group1 = displayio.Group(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

#Erik: CircuitPython 6 changed to 7
#text_group2 = displayio.Group(max_size=10, scale=1, x=20, y=60)
text_group2 = displayio.Group(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

text_group2b = displayio.Group(scale=1, x=20, y=76)
text2b = os.uname()[2]
text_area2b = label.Label(terminalio.FONT, text=text2b, color=0xFFFFFF)
text_group2b.append(text_area2b)  # Subgroup for text scaling

# Draw a label
#Erik: CircuitPython 6 changed to 7
#text_group3 = displayio.Group(max_size=10, scale=1, x=20, y=100)
text_group3 = displayio.Group(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
#Erik: CircuitPython 6 changed to 7
#text_group4 = displayio.Group(max_size=10, scale=2, x=20, y=120)
text_group4 = displayio.Group(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_group2b)
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

cpyNanoESP32S2__spiST7789_shape.py
"""
Example of CircuitPython 7/nanoESP32-S2
to display on ST7789 SPI IPS Screen

Connection between nanoESP32-S2 and
the IPS screen, with ST7789 SPI interface.
GP5  - BLK
GP4  - CS
GP3  - DC
GP2  - RES
GP1  - SDA
GP0  - SCL
3V3  - VCC
GND  - GND
"""

import os
import board
import time
import terminalio
import displayio
import busio
import adafruit_st7789
from adafruit_display_shapes.rect import Rect
from adafruit_display_shapes.circle import Circle
from adafruit_display_shapes.roundrect import RoundRect
from adafruit_display_shapes.triangle import Triangle
from adafruit_display_shapes.sparkline import Sparkline

import digitalio

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

#assign pins for nanoESP32-S2
tft_blk = board.IO5
tft_cs = board.IO4
tft_dc = board.IO3
tft_res = board.IO2
spi_mosi = board.IO1
spi_clk = board.IO0

pin_blk = digitalio.DigitalInOut(tft_blk)
pin_blk.direction = digitalio.Direction.OUTPUT
pin_blk.value = True

spi = busio.SPI(spi_clk, MOSI=spi_mosi)

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

#for 240x320
display = adafruit_st7789.ST7789(display_bus,
                    width=240, height=320)

#for 240x240
#display = adafruit_st7789.ST7789(display_bus,
#                    width=240, height=240, rowstart=80)

display.rotation = 270

print(display.width, " x ", display.height)

splash = displayio.Group()
display.show(splash)

color_bitmap = displayio.Bitmap(display.width, display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFFFFFF
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)

"""
ref:
Adafruit Display_Shapes Library
https://circuitpython.readthedocs.io/projects/display-shapes/en/latest/index.html
"""

triangle = Triangle(0, 0, 0, 60, 80, 0, fill=0xFF0000, outline=0xFF00FF)
splash.append(triangle)
time.sleep(1)

for i in range(180):
    time.sleep(0.05)
    triangle.x=i
    triangle.y=i
    triangle.fill=int((i*0x000100) + i)
    triangle.outline=(int)((i/10)*0x010000)

centerX = (int)(display.width/2)
centerY = (int)(display.height/2)
circle = Circle(centerX, centerY, 100, fill=0x000000, outline=0xFF0000)
splash.append(circle)
time.sleep(1)

for i in range(0xFF):
    time.sleep(0.01)
    circle.fill=(int)(i*0x010101)

triangle.fill=0x0000FF
triangle.outline=0x000000
for i in range(180):
    time.sleep(0.05)
    triangle.x=180-i

#re-order triangle to front
splash.remove(triangle)
splash.append(triangle)
triangle.fill=0x00FF00
triangle.outline=0x000000

for i in range(180):
    time.sleep(0.05)
    triangle.x=i
    triangle.y=180-i

print("--- finished ---")
while True:
    pass


Remark for change from CircuitPython 6 to 7


Please note that defination of displayio.Group() change from:

CircuitPython 6.3.x:
classdisplayio.Group(*, max_size: int = 4, scale: int = 1, x: int = 0, y: int = 0)
CircuitPython 7.0.x:
classdisplayio.Group(*, scale: int = 1, x: int = 0, y: int = 0)

So I have to modify accordingly.


Next:

No comments:

Post a Comment