Exercise of using bitmaptools.draw_polygon() added in CircuitPython 8.1.0
Exercise of using bitmaptools.draw_polygon() added in CircuitPython 8.1.0 (Release Notes for 8.1.0). Tested on
Raspberry Pi Pico/CircuitPython 8.1.0 with SSD1331 SPI color OLED.
exCpyPico_SSD1331_polygon.py, a simple exercise using bitmaptools.draw_polygon().
"""
Simple Test 0.95" 96x64 SSD1331 SPI Color OLED
run on Raspberry Pi Pico (RP2040)/CircuitPython 8.1.0
Exercse of using bitmaptools.draw_polygon()
https://docs.circuitpython.org/en/latest/shared-bindings/bitmaptools/index.html#bitmaptools.draw_polygon
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
import bitmaptools
import time
# 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
main_group = displayio.Group()
display.root_group = main_group
#draw polygon of border
border_width = display.width
border_height = display.height
palette_border = displayio.Palette(2)
palette_border[0] = 0x000000
palette_border[1] = 0xffffff
bmp_border = displayio.Bitmap(border_width,border_height, 2)
bmp_border.fill(0)
xs = bytes([0, 0, border_width-1, border_width-1])
ys = bytes([0, border_height-1, border_height-1, 0])
bitmaptools.draw_polygon(bmp_border, xs, ys, 1)
tilegrid_border = displayio.TileGrid(bitmap=bmp_border, pixel_shader=palette_border)
main_group.append(tilegrid_border)
time.sleep(1)
#draw more polygon
polygon_group = displayio.Group(x=5, y=5)
main_group.append(polygon_group)
polygon_width = display.width-10
polygon_height = display.height-10
palette = displayio.Palette(4)
palette[0] = 0x505050
palette[1] = 0xff0000
palette[2] = 0x00ff00
palette[3] = 0x0000ff
bmp = displayio.Bitmap(polygon_width,polygon_height, 4)
bmp.fill(0)
xs = bytes([0, 50, 30])
ys = bytes([0, 22, 40])
bitmaptools.draw_polygon(bmp, xs, ys, 1)
tilegrid = displayio.TileGrid(bitmap=bmp, pixel_shader=palette)
polygon_group.append(tilegrid)
time.sleep(1)
xs = bytes([5, 50, 30, 25])
ys = bytes([10, 22, 40, 55])
bitmaptools.draw_polygon(bmp, xs, ys, 2)
time.sleep(1)
xs = bytes([20, 50, 80, 55, 20])
ys = bytes([20, 15, 30, 50, 35])
bitmaptools.draw_polygon(bmp, xs, ys, 3)
time.sleep(1)
while True:
pass
exCpyPico_SSD1331_polygon_ani.py, with animation.
"""
Simple Test 0.95" 96x64 SSD1331 SPI Color OLED
run on Raspberry Pi Pico (RP2040)/CircuitPython 8.1.0
Exercse of using bitmaptools.draw_polygon() to display a animated polygon
https://docs.circuitpython.org/en/latest/shared-bindings/bitmaptools/index.html#bitmaptools.draw_polygon
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
import bitmaptools
import time
from math import sin
from math import cos
from math import radians
# 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
main_group = displayio.Group()
display.root_group = main_group
#draw polygon of border
border_width = display.width
border_height = display.height
palette_border = displayio.Palette(2)
palette_border[0] = 0xa0a0a0
palette_border[1] = 0xffffff
bmp_border = displayio.Bitmap(border_width,border_height, 2)
bmp_border.fill(0)
xs = bytes([0, 0, border_width-1, border_width-1])
ys = bytes([0, border_height-1, border_height-1, 0])
bitmaptools.draw_polygon(bmp_border, xs, ys, 1)
tilegrid_border = displayio.TileGrid(bitmap=bmp_border, pixel_shader=palette_border)
main_group.append(tilegrid_border)
time.sleep(1)
#draw a animated polygon
display_c_x = int(border_width/2)
display_c_y = int(border_height/2)
aniArea_r = 30
aniArea_l = aniArea_r*2 #aniArea = aniArea_l x aniArea_l
# offset of animated area on display
aniArea_x = int((display.width - aniArea_l)/2)
aniArea_y = int((display.height - aniArea_l)/2)
polygon_group = displayio.Group(x=aniArea_x, y=aniArea_y)
main_group.append(polygon_group)
polygon_width = display.width-10
polygon_height = display.height-10
palette = displayio.Palette(2)
palette[0] = 0x000000
palette[1] = 0xff0000
bmp = displayio.Bitmap(aniArea_l, aniArea_l, 2)
bmp.fill(0)
aniArea = displayio.TileGrid(bitmap=bmp, pixel_shader=palette)
polygon_group.append(aniArea)
time.sleep(1)
# convert list of [[x,y], [x, y], ...]
# to list[2] in form of [[x, x, ...], [y, y, ...]]
# also convert date in range of -aniArea_r..+aniArea_r to 0..aniArea_l
def cnvPolygon(p):
new_xs = bytes([p[0][0]+aniArea_r,
p[1][0]+aniArea_r,
p[2][0]+aniArea_r,
p[3][0]+aniArea_r,
p[4][0]+aniArea_r])
new_ys = bytes([p[0][1]+aniArea_r,
p[1][1]+aniArea_r,
p[2][1]+aniArea_r,
p[3][1]+aniArea_r,
p[4][1]+aniArea_r])
return [new_xs, new_ys]
angle = 0
while True:
angle = angle+5
if angle>=360:
angle = 0
polygon = [[int(aniArea_r * cos(radians(angle))), int(aniArea_r * sin(radians(angle)))],
[int(aniArea_r * cos(radians(angle+72))), int(aniArea_r * sin(radians(angle+72)))],
[int(aniArea_r * cos(radians(angle+144))), int(aniArea_r * sin(radians(angle+144)))],
[int(aniArea_r * cos(radians(angle+216))), int(aniArea_r * sin(radians(angle+216)))],
[int(aniArea_r * cos(radians(angle+288))), int(aniArea_r * sin(radians(angle+288)))],]
pp = cnvPolygon(polygon)
bmp.fill(0)
bitmaptools.draw_polygon(bmp, pp[0], pp[1], 1)
time.sleep(0.05)
Tested work on Raspberry Pi Pico W also.
Comments
Post a Comment