Raspberry Pi Pico/CircuitPython 8 to drive SSD1331 SPI color OLED

Exercises to display on 0.95" 96x64 SSD1331 SPI Color OLED with Raspberry Pi Pico/CircuitPython 8.1.0.


Connection between Raspberry Pi Pico and SSD1331 Color SPI OLED:


Exercise code:

exCpyPico_SSD1331.py, a simple Hello World example.
"""
Simple Test 0.95" 96x64 SSD1331 SPI Color OLED
run on Raspberry Pi Pico (RP2040)/CircuitPython 8.1.0

Connection:
SD1331      RPi Pico
-----------------------------
CS          GP0
DC          GP1
RES         GP4
SDA         GP3
SCL         GP2  
VCC         3V3
GND         GND
"""

import board
import busio
import terminalio
import displayio
import adafruit_ssd1331
from adafruit_display_text import label

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

# init display
disp_clk = board.GP2
disp_mosi = board.GP3
disp_spi = busio.SPI(clock=disp_clk, MOSI=disp_mosi)

disp_res = board.GP4
disp_dc  = board.GP1
disp_cs  = board.GP0

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

display = adafruit_ssd1331.SSD1331(display_bus,
                  width=96, height=64)

print(adafruit_ssd1331.__name__,
      adafruit_ssd1331.__version__)

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

color_bitmap = displayio.Bitmap(96, 64, 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(86, 54, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0xAA0088  # Purple
inner_sprite = displayio.TileGrid(inner_bitmap,
                                  pixel_shader=inner_palette,
                                  x=5, y=5)
splash.append(inner_sprite)

# Draw a label
text = "Hello World!"
text_area = label.Label(terminalio.FONT,
                        text=text,
                        color=0xFFFF00,
                        x=12, y=32)
splash.append(text_area)

while True:
    pass

exCpyPico_SSD1331_label.py, display random pixel, with toggled labels.
"""
Simple Test 0.95" 96x64 SSD1331 SPI Color OLED
run on Raspberry Pi Pico (RP2040)/CircuitPython 8.1.0
display random pixel, with toggled labels.

Connection:
SD1331      RPi Pico
-----------------------------
CS          GP0
DC          GP1
RES         GP4
SDA         GP3
SCL         GP2  
VCC         3V3
GND         GND
"""

import board
import busio
import terminalio
import displayio

import adafruit_ssd1331
from adafruit_display_text import label
from adafruit_display_shapes.roundrect import RoundRect

import random
import time
# import gc

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

# init display
disp_clk = board.GP2
disp_mosi = board.GP3
disp_spi = busio.SPI(clock=disp_clk, MOSI=disp_mosi)

disp_res = board.GP4
disp_dc  = board.GP1
disp_cs  = board.GP0

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

display = adafruit_ssd1331.SSD1331(display_bus,
                  width=96, height=64)

print(adafruit_ssd1331.__name__,
      adafruit_ssd1331.__version__)

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

color_of_bg = 256
color_bitmap = displayio.Bitmap(display.width, display.height, color_of_bg)
color_palette = displayio.Palette(color_of_bg)
#color_palette[0] = 0x00FF00  # Bright Green
# create random color_palette
color_palette[0] = 0x000000   # black
for c in range(1, color_of_bg):    
    color_palette[c]=(random.randrange(255)*0x10000 +
                      random.randrange(255) * 0x100 +
                      random.randrange(255))

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

#================================================
#prepare Label of title
title = ("RPi Pico\n(RP2040)", "CircuitPython", "SSD1331 OLED")
group_title = displayio.Group(scale=1)

label_title = label.Label(terminalio.FONT,
                        text=title[0]+"\n"+
                          title[1]+"\n"+
                          title[2],
                        color=0xFFFFFF)
label_title.anchor_point = (0.0, 0.0)
label_title.anchored_position = (0, 0)
label_title_width = label_title.bounding_box[2]
label_title_height = label_title.bounding_box[3]
shape_title = RoundRect(x=-2, y=-2,
                        width=label_title_width+4,
                        height=label_title_height+4,
                        r=5,
                        fill=0x000000,
                        outline=0xFFFFFF, stroke=1)
group_title.x = (display.width-label_title_width)//2
group_title.y = (display.height-label_title_height)//2
group_title.append(shape_title)
group_title.append(label_title)
#================================================
splash.append(bg_sprite)
splash.append(group_title)

"""
In beginning, I suppose to update label_title
and shape_title at runtime.
But I found that I cannot update shape_title
at runtime.
So I remove both label_title and shape_title,
update label_title and create another shape_title,
then append back.
Such that I have to call gc.collect() repeatly,
otherwise mem_free will decrease when it run.
"""
def toggle_title():
    global title_index
    global label_title
    global shape_title
    global group_title
    
    group_title.remove(shape_title)
    group_title.remove(label_title)
    
    title_index=title_index+1
    if title_index==len(title):
        title_index=0
    #print(title[title_index])
    label_title.text = title[title_index]

    lt_width = label_title.bounding_box[2]
    lt_height = label_title.bounding_box[3]
    shape_title = RoundRect(x=-5, y=-5,
                            width=lt_width+10,
                            height=lt_height+10,
                            r=10,
                            fill=0x000000,
                            outline=0xFFFFFF,
                            stroke=1)
    group_title.x = (display.width-lt_width)//2
    group_title.y = (display.height-lt_height)//2
    group_title.append(shape_title)
    group_title.append(label_title)
    
title_index=len(title)-1
next_dur = 3.0
next_time = time.monotonic() + next_dur
while True:
    x = random.randrange(display.width)
    y = random.randrange(display.height)
    c = random.randrange(color_of_bg)
    color_bitmap[x, y]=c
    
    #toggle label in 3 seconds
    now = time.monotonic()
    if now >= next_time:
        next_time = now+next_dur
        toggle_title()
        #print(gc.mem_free())
        ##gc.collect()
        ##print(gc.mem_free())
        ##print("-----------")
    
    time.sleep(0.05)

print("~ bye ~")


Tested work on Raspberry Pi Pico W also.




next:
Exercise of using bitmaptools.draw_polygon() added in CircuitPython 8.1.0



Comments

Popular posts from this blog

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.

CameraWebServe: ESP32-S3 (arduino-esp32) + OV5640 camera module