RP2040/CircuitPython display on SH1107 SPI OLED

Exercise of using WeAct RP2040 (16MB) with CircuitPython 8.0.4 to display on 1.12" 128x128 SH1107 SPI OLED.


Connection
    SH1107	RP2040
    ==================
    GND	        GND
    VCC	        3V3
    SCL	        GP10
    SDA	        GP11
    RES	        GP13
    DC	        GP14
    CS	        GP15
Prepare Library

Visit https://circuitpython.org/libraries, download CircuitPython Libraries Bundle for Version 8.x.

Upload adafruit_display_text adafruit_display_shapes folders, and adafruit_displayio_sh1107.mpy to CircuitPython device's /lib.

Exercise Code

cpyRP_SH1107_hello.py
"""
Exercise run on WeAct RP2040 (16MB)/CircuitPython 8.0.4
To driver 1.12" 128x128 SH1107 SPI OLED

ref:
https://docs.circuitpython.org/projects/displayio-sh1107/en/latest/

I call adafruit_displayio_sh1107.SH1107() with
display_offset=adafruit_displayio_sh1107.DISPLAY_OFFSET_ADAFRUIT_128x128_OLED_5297.
It's by trying.

If it's shifted in your display, try others:
-DISPLAY_OFFSET_ADAFRUIT_128x128_OLED_5297
-DISPLAY_OFFSET_ADAFRUIT_FEATHERWING_OLED_4650
-DISPLAY_OFFSET_PIMORONI_MONO_OLED_PIM374'


"""

import busio
import board
import displayio
import terminalio
import time
import os, sys

from adafruit_display_text import label
import adafruit_displayio_sh1107

displayio.release_displays()

print("=========================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print(adafruit_displayio_sh1107.__name__, adafruit_displayio_sh1107.__version__)
print("=========================================")
#===================================
# Setup my SH1107
#===================================
oled_SCL = board.GP10
oled_SDA = board.GP11
oled_RES = board.GP13
oled_DC = board.GP14
oled_CS = board.GP15

# 128x128 SH1107
OLED_W = 128
OLED_H = 128

oled_spi = busio.SPI(clock=oled_SCL, MOSI=oled_SDA)
display_bus = displayio.FourWire(oled_spi, command=oled_DC, chip_select=oled_CS, reset=oled_RES)

display = adafruit_displayio_sh1107.SH1107(
    display_bus,
    width=OLED_W, height=OLED_H,
    display_offset=adafruit_displayio_sh1107.DISPLAY_OFFSET_ADAFRUIT_128x128_OLED_5297,
    rotation=270)
# end of display setup =============

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

"""
show() is deprecated and will be removed in CircuitPython 9.0.0. Use .root_group = group instead.
https://docs.circuitpython.org/en/latest/shared-bindings/displayio/index.html#displayio.Display.show
"""
#display.show(splash)
display.root_group = splash

bg_bitmap = displayio.Bitmap(display.width, display.height, 1)
bg_palette = displayio.Palette(1)
bg_palette[0] = 0xFFFFFF  # White

background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
splash.append(background)

inner_bitmap = displayio.Bitmap(display.width-2, display.height-2, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x000000  # Black

inner = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=1, y=1)
splash.append(inner)


# draw a label say hello
text_hello = "coxxect.blogspot.com"
label_hello = label.Label(terminalio.FONT, text=text_hello, scale=1, color=0xFFFFFF, x=5, y=10)
splash.append(label_hello)

# label for CircuitPython info
sys_info = sys.implementation[0] + " " + os.uname()[2] + "\nrun on " + os.uname()[4]
# labels for driver
label_sysinfo = label.Label(terminalio.FONT,
                            text=sys_info,
                            scale=1,
                            color=0xFFFFFF,
                            x=5, y=40)
splash.append(label_sysinfo)

# labels for driver
label_drv_name = label.Label(terminalio.FONT,
                             text=adafruit_displayio_sh1107.__name__,
                             scale=1,
                             color=0xFFFFFF,
                             x=5, y=90)
splash.append(label_drv_name)

label_drv_ver = label.Label(terminalio.FONT,
                            text=adafruit_displayio_sh1107.__version__,
                            scale=2,
                            color=0xFFFFFF,
                            x=5, y=110)
splash.append(label_drv_ver)

# scroll()/reverse_scroll()
# ref:
# https://learn.adafruit.com/rgb-led-matrices-matrix-panels-with-circuitpython/example-simple-two-line-text-scroller

def scroll(line):
    line.x = line.x - 1
    line_width = line.bounding_box[2]
    if line.x < -line_width:
        line.x = display.width
        
def reverse_scroll(line):
    line.x = line.x + 1
    line_width = line.bounding_box[2]
    if line.x >= display.width:
        line.x = -line_width

time.sleep(1)

while True:
    scroll(label_sysinfo)
    scroll(label_drv_name)
    reverse_scroll(label_drv_ver)
    display.refresh(minimum_frames_per_second=0)



cpyRP_SH1107_star.py
"""
Exercise run on WeAct RP2040 (16MB)/CircuitPython 8.0.4
To driver 1.12" 128x128 SH1107 SPI OLED
Draw a Star using:
- adafruit_display_shapes.polygon
- vectorio.Polygon

ref:
https://docs.circuitpython.org/projects/displayio-sh1107/en/latest/

The vectorio module provide simple filled drawing primitives for use with displayio:
https://docs.circuitpython.org/en/latest/shared-bindings/vectorio/index.html

"""

import busio
import board
import displayio
import terminalio
import time
import os, sys
import math

from adafruit_display_text import label
from adafruit_display_shapes.polygon import Polygon
import adafruit_displayio_sh1107
import vectorio

displayio.release_displays()

print("=========================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print(adafruit_displayio_sh1107.__name__, adafruit_displayio_sh1107.__version__)
print("=========================================")
#===================================
# Setup my SH1107
#===================================
oled_SCL = board.GP10
oled_SDA = board.GP11
oled_RES = board.GP13
oled_DC = board.GP14
oled_CS = board.GP15

# 128x128 SH1107
OLED_W = 128
OLED_H = 128

oled_spi = busio.SPI(clock=oled_SCL, MOSI=oled_SDA)
display_bus = displayio.FourWire(oled_spi, command=oled_DC, chip_select=oled_CS, reset=oled_RES)

display = adafruit_displayio_sh1107.SH1107(
    display_bus,
    width=OLED_W, height=OLED_H,
    display_offset=adafruit_displayio_sh1107.DISPLAY_OFFSET_ADAFRUIT_128x128_OLED_5297,
    rotation=270)
# end of display setup =============

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

"""
show() is deprecated and will be removed in CircuitPython 9.0.0. Use .root_group = group instead.
https://docs.circuitpython.org/en/latest/shared-bindings/displayio/index.html#displayio.Display.show
"""
#display.show(splash)
display.root_group = splash

bg_bitmap = displayio.Bitmap(display.width, display.height, 1)
bg_palette = displayio.Palette(1)
bg_palette[0] = 0xFFFFFF  # White

background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
splash.append(background)

inner_bitmap = displayio.Bitmap(display.width-2, display.height-2, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x000000  # Black

inner = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=1, y=1)
splash.append(inner)


center_x = int(display.width/2)
center_y = int(display.height/2)
circle_r = 50
circle_inner_r = 25
deg = -90


def getPolPt_o(r, deg):
    rad=math.radians(deg)
    px = int(r*math.cos(rad))
    py = int(r*math.sin(rad))
    return(int(px),
           int(py))

def getPolPt(x, y, r, deg):
    rad=math.radians(deg)
    px = int(r*math.cos(rad)+center_x)
    py = int(r*math.sin(rad)+center_y)
    return(int(px),
           int(py))

pol_o=[getPolPt_o(circle_r, deg),
       getPolPt_o(circle_inner_r, deg+36),
       getPolPt_o(circle_r, deg+72),
       getPolPt_o(circle_inner_r, deg+108),
       getPolPt_o(circle_r, deg+144),
       getPolPt_o(circle_inner_r, deg+180),
       getPolPt_o(circle_r, deg+216),
       getPolPt_o(circle_inner_r, deg+252),
       getPolPt_o(circle_r, deg+288),
       getPolPt_o(circle_inner_r, deg+324),
       ]

pol=[getPolPt(center_x, center_y, circle_r, deg),
     getPolPt(center_x, center_y, circle_inner_r, deg+36),
     getPolPt(center_x, center_y, circle_r, deg+72),
     getPolPt(center_x, center_y, circle_inner_r, deg+108),
     getPolPt(center_x, center_y, circle_r, deg+144),
     getPolPt(center_x, center_y, circle_inner_r, deg+180),
     getPolPt(center_x, center_y, circle_r, deg+216),
     getPolPt(center_x, center_y, circle_inner_r, deg+252),
     getPolPt(center_x, center_y, circle_r, deg+288),
     getPolPt(center_x, center_y, circle_inner_r, deg+324),
     ]

pol_palette = displayio.Palette(1)
pol_palette[0] = 0x000000  # Black
vectorio_polygon = vectorio.Polygon(pixel_shader=pol_palette,
                                    x = center_x, y = center_y,
                                    points=pol_o)
splash.append(vectorio_polygon)

polygon_outline = Polygon(points=pol,
                          outline=0xFFFFFF)
splash.append(polygon_outline)


while True:
    for y in range(1, inner_bitmap.height-2):
        for x in range(1, inner_bitmap.width-2):
            inner_bitmap[x, y] = 1
            time.sleep(0.0002)
    

    for y in range(1, inner_bitmap.height-2):
        for x in range(1, inner_bitmap.width-2):
            inner_bitmap[x, y] = 0
            time.sleep(0.0002)


cpyRP_SH1107_terminal.py
"""
Exercise run on WeAct RP2040 (16MB)/CircuitPython 8.0.4
To driver 1.12" 128x128 SH1107 SPI OLED
use as REPL display terminal

Once a Display is setup, before switche to display any given group of layers,
the default CircuitPython terminal will be shown; work as REPL display terminal.

"""

import busio
import board
import displayio
import terminalio
import time
import os, sys
import math

from adafruit_display_text import label
from adafruit_display_shapes.polygon import Polygon
import adafruit_displayio_sh1107
import vectorio

displayio.release_displays()

print("=========================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print(adafruit_displayio_sh1107.__name__, adafruit_displayio_sh1107.__version__)
print("=========================================")
#===================================
# Setup my SH1107
#===================================
oled_SCL = board.GP10
oled_SDA = board.GP11
oled_RES = board.GP13
oled_DC = board.GP14
oled_CS = board.GP15

# 128x128 SH1107
OLED_W = 128
OLED_H = 128

oled_spi = busio.SPI(clock=oled_SCL, MOSI=oled_SDA)
display_bus = displayio.FourWire(oled_spi, command=oled_DC, chip_select=oled_CS, reset=oled_RES)

display = adafruit_displayio_sh1107.SH1107(
    display_bus,
    width=OLED_W, height=OLED_H,
    display_offset=adafruit_displayio_sh1107.DISPLAY_OFFSET_ADAFRUIT_128x128_OLED_5297,
    rotation=270)
# end of display setup =============


print("Enter something:")

while True:
    user_input = input()

    if user_input == "":
        print("~ bye ~")
        break
    else:
        print(":", user_input)






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