Raspberry Pi Pico 2 (RP2350)/MicroPython display on two OLED, SSD1306 I2C and SPI.

In previous I exercised on Raspberry Pi Pico 2/MicroPython to display on I2C SSD1306 OLED and SPI SSD1306 OLED. In this post display on both I2C and SPI SSD1306 OLED.



Remark: when I test the code, base on MicroPython v1.24.0-preview.321 on Raspberry Pi Pico 2 normal RP2350 and RP2350-RISCV version. It's found something strange when running on  RP2350-RISCV version. Check the video at 4:30.
- The MicroPython running in a loop, without time.sleep().
- If "Stop" on Thonny IDE clicked to stop from running, the Pico 2 will disconnected, and REPL fail. - ----- Have to reboot the Pico to re-connect.

It should be a MicroPython issue, not SSD1306 issue.



Connection:

Connection between Raspberry Pi Pico 2 and I2C/SPI SSD1306 OLED
I2C SSD1306
- SCL		GP5
- SDA		GP4
SPI SSD1306
- D/C		GP13
- RST		GP12
- SDA		GP11 (mosi)
- SCL		GP10 (sck)
- VCC		3V3
- GND		GND
		GP9 (dummy cs) - No connection
		GP8 (miso) - No connection
Exercise Code:

To install ssd1306 driver on Raspberry Pi Pico 2, read the post I2C OLED (SSD1306/SSD1315) screen with Raspberry Pi Pico 2 (RP2350) using MicroPython.


mpyPico2_dual_oled_hello.py, a simple Hello World to display some things.
"""
MicroPython/Raspberry Pi Pico 2 exercise
display on Dual OLED, 0.96" 128x64 SSD1306 I2C/SPI OLED.
(It should work on both Pico/Pico 2)

Install ssd1306 @ micropython-lib SSD1306 OLED driver (ver: 0.1.0)
in Thonny > Tools > Manager packages...

I2C OLED is connected to I2C(0)
SCL - GP5
SDA - GP4

SPI OLED is connected to SPI(1)
SCK  - GP10
MOSI - GP11
MISO - GP8  (Not use)

"""
import sys
import os
import time
import ssd1306

DISP_WIDTH=128
DISP_HEIGHT=64

# notice that:
# parameter cs have to be provided to ssd1306.SSD1306_SPI(),
# so I assign a GP8 as dummt cs, no need connect it.
dc  = machine.Pin(13)
res = machine.Pin(12)
cs  = machine.Pin(9)   #dummy (any un-used pin), no connection

print("====================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("====================================")

package_info = ssd1306.__name__
try:
    package_info = package_info + " ver:" + ssd1306.__version__
except AttributeError as exc:
    print("AttributeError!", exc)

print("ssd1306 package: ", package_info)
print("====================================")
oled_i2c = machine.I2C(0)
print("oled_i2c:", oled_i2c)
oled_spi = machine.SPI(1)
print("oled_spi:", oled_spi)
print()

oled_ssd1306_i2c = ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, oled_i2c)
oled_ssd1306_spi = ssd1306.SSD1306_SPI(DISP_WIDTH, DISP_HEIGHT, oled_spi, dc, res, cs)
oled_ssd1306_i2c.fill(1)
oled_ssd1306_spi.fill(1)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(1)
oled_ssd1306_i2c.fill(0)
oled_ssd1306_spi.fill(0)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(1)
    
oled_ssd1306_i2c.rect(1, 1, DISP_WIDTH-2, 25, 1)
oled_ssd1306_spi.rect(1, 1, DISP_WIDTH-2, 25, 1)
    
oled_ssd1306_i2c.text('Hello, World!', 5, 5, 1)
oled_ssd1306_i2c.text(oled_ssd1306_i2c.__qualname__, 5, 15, 1)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.text('Hello, World!', 5, 5, 1)
oled_ssd1306_spi.text(oled_ssd1306_spi.__qualname__, 5, 15, 1)
oled_ssd1306_spi.show()
    
while True:
    for y in range(9):
        oled_ssd1306_i2c.scroll(0, y)
        oled_ssd1306_spi.scroll(0, y)
        oled_ssd1306_i2c.show()
        oled_ssd1306_spi.show()
        time.sleep(0.2)
    for y in range(9):
        oled_ssd1306_i2c.scroll(0, -y)
        oled_ssd1306_spi.scroll(0, -y)
        oled_ssd1306_i2c.show()
        oled_ssd1306_spi.show()
        time.sleep(0.2)       

print("~ bye ~")

mpyPico2_dual_oled_pixel.py, fill screen in pixel-by-pixel.
"""
MicroPython/Raspberry Pi Pico 2 exercise
display on Dual OLED, 0.96" 128x64 SSD1306 I2C/SPI OLED.
(It should work on both Pico/Pico 2)

Fill screen in pixel-by-pixel, test on both I2C and SPI SSD1306 OLED.

Install ssd1306 @ micropython-lib SSD1306 OLED driver (ver: 0.1.0)
in Thonny > Tools > Manager packages...

I2C OLED is connected to I2C(0)
SCL - GP5
SDA - GP4

SPI OLED is connected to SPI(1)
SCK  - GP10
MOSI - GP11
MISO - GP8  (Not use)

"""
import sys
import os
import time
import ssd1306

DISP_WIDTH=128
DISP_HEIGHT=64

# notice that:
# parameter cs have to be provided to ssd1306.SSD1306_SPI(),
# so I assign a GP8 as dummt cs, no need connect it.
dc  = machine.Pin(13)
res = machine.Pin(12)
cs  = machine.Pin(9)   #dummy (any un-used pin), no connection

print("====================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("====================================")

package_info = ssd1306.__name__
try:
    package_info = package_info + " ver:" + ssd1306.__version__
except AttributeError as exc:
    print("AttributeError!", exc)

print("ssd1306 package: ", package_info)
print("====================================")
oled_i2c = machine.I2C(0)
print("oled_i2c:", oled_i2c)
oled_spi = machine.SPI(1)
print("oled_spi:", oled_spi)
print()

oled_ssd1306_i2c = ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, oled_i2c)
oled_ssd1306_spi = ssd1306.SSD1306_SPI(DISP_WIDTH, DISP_HEIGHT, oled_spi, dc, res, cs)
oled_ssd1306_i2c.fill(1)
oled_ssd1306_spi.fill(1)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(1)
oled_ssd1306_i2c.fill(0)
oled_ssd1306_spi.fill(0)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(1)

def pixel_speed_test(oled):
    time.sleep(0.1)  # it's not sure any delay operation in MicroPython, 100ms delay before/after each test
    start_time = time.ticks_ms()
    for y in range(oled.height):
        for x in range(oled.width):
            oled.pixel(x, y, 1)
    stop_time = time.ticks_ms()
    time.sleep(0.1)
    oled.show()
    
    diff_time = time.ticks_diff(stop_time, start_time)
    oled.text("Pixel test", 5, 10, 0)
    oled.text(oled.__qualname__, 5, 20, 0)
    oled.text(str(diff_time)+ " ms", 5, 30, 0)
    oled.show()
    
    time.sleep(1)
    
    time.sleep(0.1)
    start_time = time.ticks_ms()
    for y in range(oled.height):
        for x in range(oled.width):
            oled.pixel(x, y, 0)
    stop_time = time.ticks_ms()
    time.sleep(0.1)
    oled.show()
    
    diff_time = time.ticks_diff(stop_time, start_time)
    oled.text("Pixel test", 5, 10, 1)
    oled.text(oled.__qualname__, 5, 20, 1)
    oled.text(str(diff_time)+ " ms", 5, 30, 1)
    oled.show()
    
    time.sleep(1)

while True:
    pixel_speed_test(oled_ssd1306_i2c)
    pixel_speed_test(oled_ssd1306_spi)
     
print("~ bye ~")


mpyPico2_dual_oled_polygon.py, two polygons rotate on both OLED.
"""
MicroPython/Raspberry Pi Pico 2 exercise
display on Dual OLED, 0.96" 128x64 SSD1306 I2C/SPI OLED.
(It should work on both Pico/Pico 2)

Polygon exercise - two polygons rotate on both OLED.

Install ssd1306 @ micropython-lib SSD1306 OLED driver (ver: 0.1.0)
in Thonny > Tools > Manager packages...

I2C OLED is connected to I2C(0)
SCL - GP5
SDA - GP4

SPI OLED is connected to SPI(1)
SCK  - GP10
MOSI - GP11
MISO - GP8  (Not use)

"""
import sys
import os
import time
import ssd1306
from array import array
import math

DISP_WIDTH=128
DISP_HEIGHT=64

# notice that:
# parameter cs have to be provided to ssd1306.SSD1306_SPI(),
# so I assign a GP8 as dummt cs, no need connect it.
dc  = machine.Pin(13)
res = machine.Pin(12)
cs  = machine.Pin(9)   #dummy (any un-used pin), no connection

print("====================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("====================================")

package_info = ssd1306.__name__
try:
    package_info = package_info + " ver:" + ssd1306.__version__
except AttributeError as exc:
    print("AttributeError!", exc)

print("ssd1306 package: ", package_info)
print("====================================")
oled_i2c = machine.I2C(0)
print("oled_i2c:", oled_i2c)
oled_spi = machine.SPI(1)
print("oled_spi:", oled_spi)
print()

oled_ssd1306_i2c = ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, oled_i2c)
oled_ssd1306_spi = ssd1306.SSD1306_SPI(DISP_WIDTH, DISP_HEIGHT, oled_spi, dc, res, cs)
oled_ssd1306_i2c.fill(1)
oled_ssd1306_spi.fill(1)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(1)
oled_ssd1306_i2c.fill(0)
oled_ssd1306_spi.fill(0)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(1)

# === myEquPolygonClass class ===
class myEquPolygonClass:
    
    def __init__(self, num_of_ends, r, angel=0):
        self.num_of_ends = num_of_ends
        self.r = r
        self.angel = angel
        
        self.coords_list = []
        self.reCal_coords_list()
        
    def update_coords_list(self, num_of_ends, r, angel=0):
        self.num_of_ends = num_of_ends
        self.r = r
        self.angel = angel
        
        self.coords_list.clear()
        self.reCal_coords_list()
        
    def reCal_coords_list(self):
        for end in range(self.num_of_ends):
            angle_in_radians = math.radians((360 * end/self.num_of_ends) + self.angel)
            #print("degree of end", end, angle_in_radians)
            #print(angle_in_radians, ":", math.sin(angle_in_radians), math.cos(angle_in_radians))
            self.coords_list.append(int(self.r * math.sin(angle_in_radians)))
            self.coords_list.append(int(self.r * math.cos(angle_in_radians)))
        
    def show(self, screen, offset_x=0, offset_y=0, fill=0):
        coords_array = array('h', self.coords_list)
        
        screen.poly(offset_x, offset_y,
                    coords_array,
                    1,        # color
                    fill)     # optional fill: True - fill, False - not fill
        screen.show()
# === End of myFixedPolygonClass ===
        
center_x_i2c = int(oled_ssd1306_i2c.width/2)
center_y_i2c = int(oled_ssd1306_i2c.height/2)
center_x_spi = int(oled_ssd1306_spi.width/2)
center_y_spi = int(oled_ssd1306_spi.height/2)

myEquPolygon1 = myEquPolygonClass(5, 30)
myEquPolygon2 = myEquPolygonClass(5, 30, 0)
myEquPolygon1.show(oled_ssd1306_i2c, center_x_i2c, center_y_i2c, 0)
myEquPolygon2.show(oled_ssd1306_spi, center_x_spi, center_y_spi, 0)

angel = 0
while True:
    angel = angel + 1

    myEquPolygon1.update_coords_list(5, 30, angel)
    myEquPolygon2.update_coords_list(5, 30, -angel)
    
    oled_ssd1306_i2c.fill(0)
    oled_ssd1306_spi.fill(0)
    myEquPolygon1.show(oled_ssd1306_i2c, center_x_i2c, center_y_i2c)
    myEquPolygon2.show(oled_ssd1306_spi, center_x_spi, center_y_spi)
    
    if angel >= 360:
        angel = 0
        
    time.sleep(0.1)
     
print("~ bye ~")



mpyPico2_dual_oled_polygon2.py, display animated star and polygon.
"""
MicroPython/Raspberry Pi Pico 2 exercise
display on Dual OLED, 0.96" 128x64 SSD1306 I2C/SPI OLED.
(It should work on both Pico/Pico 2)

Animated star and polygon

Install ssd1306 @ micropython-lib SSD1306 OLED driver (ver: 0.1.0)
in Thonny > Tools > Manager packages...

I2C OLED is connected to I2C(0)
SCL - GP5
SDA - GP4

SPI OLED is connected to SPI(1)
SCK  - GP10
MOSI - GP11
MISO - GP8  (Not use)

"""
import sys
import os
import time
import ssd1306
from array import array
import math

DISP_WIDTH=128
DISP_HEIGHT=64

# notice that:
# parameter cs have to be provided to ssd1306.SSD1306_SPI(),
# so I assign a GP8 as dummt cs, no need connect it.
dc  = machine.Pin(13)
res = machine.Pin(12)
cs  = machine.Pin(9)   #dummy (any un-used pin), no connection

print("====================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("====================================")

package_info = ssd1306.__name__
try:
    package_info = package_info + " ver:" + ssd1306.__version__
except AttributeError as exc:
    print("AttributeError!", exc)

print("ssd1306 package: ", package_info)
print("====================================")
oled_i2c = machine.I2C(0)
print("oled_i2c:", oled_i2c)
oled_spi = machine.SPI(1)
print("oled_spi:", oled_spi)
print()

oled_ssd1306_i2c = ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, oled_i2c)
oled_ssd1306_spi = ssd1306.SSD1306_SPI(DISP_WIDTH, DISP_HEIGHT, oled_spi, dc, res, cs)
oled_ssd1306_i2c.fill(1)
oled_ssd1306_spi.fill(1)
oled_ssd1306_i2c.text(oled_ssd1306_i2c.__qualname__, 5, 5, 0)
oled_ssd1306_spi.text(oled_ssd1306_spi.__qualname__, 5, 5, 0)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(3)
oled_ssd1306_i2c.fill(0)
oled_ssd1306_spi.fill(0)
oled_ssd1306_i2c.text(oled_ssd1306_i2c.__qualname__, 5, 5, 1)
oled_ssd1306_spi.text(oled_ssd1306_spi.__qualname__, 5, 5, 1)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(3)

# === myEquPolygonClass class ===
class myEquPolygonClass:
    
    def __init__(self, num_of_ends, r, angel=0):
        self.num_of_ends = num_of_ends
        self.r = r
        self.angel = angel
        
        self.coords_list = []
        self.reCal_coords_list()
        
    def update_coords_list(self, num_of_ends, r, angel=0):
        self.num_of_ends = num_of_ends
        self.r = r
        self.angel = angel
        
        self.coords_list.clear()
        self.reCal_coords_list()
        
    def reCal_coords_list(self):
        for end in range(self.num_of_ends):
            angle_in_radians = math.radians((360 * end/self.num_of_ends) + self.angel)
            #print("degree of end", end, angle_in_radians)
            #print(angle_in_radians, ":", math.sin(angle_in_radians), math.cos(angle_in_radians))
            self.coords_list.append(int(self.r * math.sin(angle_in_radians)))
            self.coords_list.append(int(self.r * math.cos(angle_in_radians)))
        
    def show(self, screen, offset_x=0, offset_y=0, fill=0):
        coords_array = array('h', self.coords_list)
        
        screen.poly(offset_x, offset_y,
                    coords_array,
                    1,        # color
                    fill)     # optional fill: True - fill, False - not fill
        screen.show()
# === End of myFixedPolygonClass ===

# === myStarClass class ===
class myStarClass:
    
    def __init__(self, num_of_ends, r1, r2, angel=0):
        self.num_of_ends = num_of_ends
        self.r1 = r1
        self.r2 = r2
        self.angel = angel
        
        self.coords_list = []
        self.reCal_coords_list()
        
    def update_coords_list(self, num_of_ends, r1, r2, angel=0):
        self.num_of_ends = num_of_ends
        self.r1 = r1
        self.r2 = r2
        self.angel = angel
        
        self.coords_list.clear()
        self.reCal_coords_list()
        
    def reCal_coords_list(self):

        half_angel_per_end = (360/self.num_of_ends)/2
        for end in range(self.num_of_ends):
            angle_in_radians = math.radians((360 * end/self.num_of_ends) + self.angel)
            self.coords_list.append(int(self.r1 * math.sin(angle_in_radians)))
            self.coords_list.append(int(self.r1 * math.cos(angle_in_radians)))
            
            angle_in_radians = math.radians((360 * end/self.num_of_ends) + half_angel_per_end + self.angel)
            self.coords_list.append(int(self.r2 * math.sin(angle_in_radians)))
            self.coords_list.append(int(self.r2 * math.cos(angle_in_radians)))
        
    def show(self, screen, offset_x=0, offset_y=0, fill=0):
        coords_array = array('h', self.coords_list)
        
        screen.poly(offset_x, offset_y,
                    coords_array,
                    1,        # color
                    fill)     # optional fill: True - fill, False - not fill
        screen.show()
# === End of myFixedPolygonClass ===
        
center_x_i2c = int(oled_ssd1306_i2c.width/2)
center_y_i2c = int(oled_ssd1306_i2c.height/2)
center_x_spi = int(oled_ssd1306_spi.width/2)
center_y_spi = int(oled_ssd1306_spi.height/2)

init_angel = 0
init_r1 = 20
init_r2 = 15
myStar1 = myStarClass(num_of_ends = 5,
                      r1 = init_r1, r2 = init_r2,
                      angel = init_angel)
myEquPolygon2 = myEquPolygonClass(num_of_ends = 5,
                                  r = 30,
                                  angel=init_angel)

myStar1.show(oled_ssd1306_i2c, center_x_i2c, center_y_i2c, 0)
myEquPolygon2.show(oled_ssd1306_spi, center_x_spi, center_y_spi, 0)

angel = init_angel

r1 = init_r1
r2 = init_r2
r2_min = 5
r2_max = 30
r2_dir = 1
while True:
    angel = angel + 1

    myStar1.update_coords_list(5, r1, r2, angel)
    myEquPolygon2.update_coords_list(5, 30, -angel)
    
    oled_ssd1306_i2c.fill(0)
    oled_ssd1306_spi.fill(0)
    myStar1.show(oled_ssd1306_i2c, center_x_i2c, center_y_i2c)
    myEquPolygon2.show(oled_ssd1306_spi, center_x_spi, center_y_spi)
    
    if angel >= 360:
        angel = 0
        
    if r2_dir == 1:
        r2 = r2 +1
        if r2 >= r2_max:
            r2_dir = 0
    else:
        r2 = r2-1
        if r2 <= r2_min:
            r2_dir = 1
            
    time.sleep(0.2)
     
print("~ bye ~")


mpyPico2_dual_oled_polygon3.py, display star with different num_of_ends on both OLED.
"""
MicroPython/Raspberry Pi Pico 2 exercise
display on Dual OLED, 0.96" 128x64 SSD1306 I2C/SPI OLED.
(It should work on both Pico/Pico 2)

Try different num_of_ends in both OLED.

Install ssd1306 @ micropython-lib SSD1306 OLED driver (ver: 0.1.0)
in Thonny > Tools > Manager packages...

I2C OLED is connected to I2C(0)
SCL - GP5
SDA - GP4

SPI OLED is connected to SPI(1)
SCK  - GP10
MOSI - GP11
MISO - GP8  (Not use)

"""
import sys
import os
import time
import ssd1306
from array import array
import math

DISP_WIDTH=128
DISP_HEIGHT=64

# notice that:
# parameter cs have to be provided to ssd1306.SSD1306_SPI(),
# so I assign a GP8 as dummt cs, no need connect it.
dc  = machine.Pin(13)
res = machine.Pin(12)
cs  = machine.Pin(9)   #dummy (any un-used pin), no connection

print("====================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("====================================")

package_info = ssd1306.__name__
try:
    package_info = package_info + " ver:" + ssd1306.__version__
except AttributeError as exc:
    print("AttributeError!", exc)

print("ssd1306 package: ", package_info)
print("====================================")
oled_i2c = machine.I2C(0)
print("oled_i2c:", oled_i2c)
oled_spi = machine.SPI(1)
print("oled_spi:", oled_spi)
print()

oled_ssd1306_i2c = ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, oled_i2c)
oled_ssd1306_spi = ssd1306.SSD1306_SPI(DISP_WIDTH, DISP_HEIGHT, oled_spi, dc, res, cs)
oled_ssd1306_i2c.fill(1)
oled_ssd1306_spi.fill(1)
oled_ssd1306_i2c.text(oled_ssd1306_i2c.__qualname__, 5, 5, 0)
oled_ssd1306_spi.text(oled_ssd1306_spi.__qualname__, 5, 5, 0)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(3)
oled_ssd1306_i2c.fill(0)
oled_ssd1306_spi.fill(0)
oled_ssd1306_i2c.text(oled_ssd1306_i2c.__qualname__, 5, 5, 1)
oled_ssd1306_spi.text(oled_ssd1306_spi.__qualname__, 5, 5, 1)
oled_ssd1306_i2c.show()
oled_ssd1306_spi.show()
time.sleep(3)

# === myEquPolygonClass class ===
class myEquPolygonClass:
    
    def __init__(self, num_of_ends, r, angel=0):
        self.num_of_ends = num_of_ends
        self.r = r
        self.angel = angel
        
        self.coords_list = []
        self.reCal_coords_list()
        
    def update_coords_list(self, num_of_ends, r, angel=0):
        self.num_of_ends = num_of_ends
        self.r = r
        self.angel = angel
        
        self.coords_list.clear()
        self.reCal_coords_list()
        
    def reCal_coords_list(self):
        for end in range(self.num_of_ends):
            angle_in_radians = math.radians((360 * end/self.num_of_ends) + self.angel)
            #print("degree of end", end, angle_in_radians)
            #print(angle_in_radians, ":", math.sin(angle_in_radians), math.cos(angle_in_radians))
            self.coords_list.append(int(self.r * math.sin(angle_in_radians)))
            self.coords_list.append(int(self.r * math.cos(angle_in_radians)))
        
    def show(self, screen, offset_x=0, offset_y=0, fill=0):
        coords_array = array('h', self.coords_list)
        
        screen.poly(offset_x, offset_y,
                    coords_array,
                    1,        # color
                    fill)     # optional fill: True - fill, False - not fill
        screen.show()
# === End of myFixedPolygonClass ===

# === myStarClass class ===
class myStarClass:
    
    def __init__(self, num_of_ends, r1, r2, angel=0):
        self.num_of_ends = num_of_ends
        self.r1 = r1
        self.r2 = r2
        self.angel = angel
        
        self.coords_list = []
        self.reCal_coords_list()
        
    def update_coords_list(self, num_of_ends, r1, r2, angel=0):
        self.num_of_ends = num_of_ends
        self.r1 = r1
        self.r2 = r2
        self.angel = angel
        
        self.coords_list.clear()
        self.reCal_coords_list()
        
    def reCal_coords_list(self):
        
        # if self.num_of_ends ==0, this code will raise error of:
        # ZeroDivisionError: divide by zero
        half_angel_per_end = (360/self.num_of_ends)/2
        
        for end in range(self.num_of_ends):
            angle_in_radians = math.radians((360 * end/self.num_of_ends) + self.angel)
            self.coords_list.append(int(self.r1 * math.sin(angle_in_radians)))
            self.coords_list.append(int(self.r1 * math.cos(angle_in_radians)))
            
            angle_in_radians = math.radians((360 * end/self.num_of_ends) + half_angel_per_end + self.angel)
            self.coords_list.append(int(self.r2 * math.sin(angle_in_radians)))
            self.coords_list.append(int(self.r2 * math.cos(angle_in_radians)))
        
    def show(self, screen, offset_x=0, offset_y=0, fill=0):
        coords_array = array('h', self.coords_list)
        
        screen.poly(offset_x, offset_y,
                    coords_array,
                    1,        # color
                    fill)     # optional fill: True - fill, False - not fill
        screen.show()
# === End of myFixedPolygonClass ===
        
center_x_i2c = int(oled_ssd1306_i2c.width/2)
center_y_i2c = int(oled_ssd1306_i2c.height/2)
center_x_spi = int(oled_ssd1306_spi.width/2)
center_y_spi = int(oled_ssd1306_spi.height/2)

init_num_of_ends = 3
init_angel = 0
init_r1 = 30
init_r2 = 10
myStar1 = myStarClass(num_of_ends = init_num_of_ends,
                      r1 = init_r1, r2 = init_r2,
                      angel = init_angel)

myStar1.show(oled_ssd1306_i2c, center_x_i2c, center_y_i2c, 0)
myStar1.show(oled_ssd1306_spi, center_x_spi, center_y_spi, 0)

angel = init_angel

num_of_ends = init_num_of_ends
r1 = init_r1
r2 = init_r2
r2_min = 5
r2_max = 30
r2_dir = 1
while True:

    myStar1.update_coords_list(num_of_ends, r1, r2, angel)
    
    oled_ssd1306_i2c.fill(0)
    oled_ssd1306_spi.fill(0)
    myStar1.show(oled_ssd1306_i2c, center_x_i2c, center_y_i2c)
    myStar1.show(oled_ssd1306_spi, center_x_spi, center_y_spi)
    
    num_of_ends = num_of_ends + 1
    if num_of_ends > 10:
        num_of_ends = 1
            
    time.sleep(0.1)
     
print("~ bye ~")

Next:
Multi SSD1306 OLED, on Raspberry Pi Pico 2/MicroPython

Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

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