-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some PicoGraphics based MP examples for the PicoUnicorn
- Loading branch information
Showing
4 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# This is a PicoGraphics version of the original demo.py | ||
|
||
from picounicorn import PicoUnicorn | ||
from picographics import PicoGraphics, DISPLAY_UNICORN_PACK | ||
|
||
picounicorn = PicoUnicorn() | ||
graphics = PicoGraphics(display=DISPLAY_UNICORN_PACK) | ||
|
||
w = picounicorn.get_width() | ||
h = picounicorn.get_height() | ||
|
||
# Display a rainbow across Pico Unicorn | ||
for x in range(w): | ||
for y in range(h): | ||
# PicoGraphics allows us to set HSV pens directly | ||
PEN = graphics.create_pen_hsv(x / w, y / h, 1.0) | ||
graphics.set_pen(PEN) | ||
graphics.pixel(x, y) | ||
|
||
# Ask the Unicorn to update the graphics | ||
picounicorn.update(graphics) | ||
|
||
print("Press Button A") | ||
|
||
while not picounicorn.is_pressed(picounicorn.BUTTON_A): # Wait for Button A to be pressed | ||
pass | ||
|
||
# Clear the display | ||
|
||
# Set the pen to black | ||
BLACK = graphics.create_pen(0, 0, 0) | ||
graphics.set_pen(BLACK) | ||
graphics.clear() | ||
|
||
# Ask the Unicorn to update the graphics | ||
picounicorn.update(graphics) | ||
|
||
print("Button A pressed!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This is a PicoGraphics version of the original demo.py | ||
|
||
from picounicorn import PicoUnicorn | ||
from picographics import PicoGraphics, DISPLAY_UNICORN_PACK | ||
import time | ||
|
||
picounicorn = PicoUnicorn() | ||
graphics = PicoGraphics(display=DISPLAY_UNICORN_PACK) | ||
|
||
w = picounicorn.get_width() | ||
h = picounicorn.get_height() | ||
|
||
while True: | ||
t = time.ticks_ms() / 3600 | ||
for x in range(w): | ||
for y in range(h): | ||
# PicoGraphics allows us to set HSV pens directly | ||
PEN = graphics.create_pen_hsv(t + ((x + y) / w / 4), 1.0, 1.0) | ||
graphics.set_pen(PEN) | ||
graphics.pixel(x, y) | ||
|
||
# Ask the Unicorn to update the graphics | ||
picounicorn.update(graphics) | ||
|
||
# And sleep, so we update ~ 60fps | ||
time.sleep(1.0 / 60) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import time | ||
import random | ||
from picounicorn import PicoUnicorn | ||
from picographics import PicoGraphics, DISPLAY_UNICORN_PACK | ||
|
||
''' | ||
A pretty, procedural fire effect. Based on fire_effect.py from bigger Unicorns! | ||
''' | ||
|
||
picounicorn = PicoUnicorn() | ||
graphics = PicoGraphics(display=DISPLAY_UNICORN_PACK) | ||
|
||
fire_colours = [graphics.create_pen(0, 0, 0), | ||
graphics.create_pen(20, 20, 20), | ||
graphics.create_pen(180, 30, 0), | ||
graphics.create_pen(220, 160, 0), | ||
graphics.create_pen(255, 255, 180)] | ||
|
||
|
||
@micropython.native # noqa: F821 | ||
def update(): | ||
# take local references as it's quicker than accessing the global | ||
# and we access it a lot in this method | ||
_heat = heat | ||
|
||
# clear the bottom row and then add a new fire seed to it | ||
for x in range(width): | ||
_heat[x][height - 1] = 0.0 | ||
_heat[x][height - 2] = 0.0 | ||
|
||
for c in range(fire_spawns): | ||
x = random.randint(0, width - 4) + 2 | ||
_heat[x + 0][height - 1] = 1.0 | ||
_heat[x + 1][height - 1] = 1.0 | ||
_heat[x - 1][height - 1] = 1.0 | ||
_heat[x + 0][height - 2] = 1.0 | ||
_heat[x + 1][height - 2] = 1.0 | ||
_heat[x - 1][height - 2] = 1.0 | ||
|
||
factor = damping_factor / 5.0 | ||
for y in range(0, height - 2): | ||
for x in range(1, width - 1): | ||
_heat[x][y] += _heat[x][y + 1] + _heat[x][y + 2] + _heat[x - 1][y + 1] + _heat[x + 1][y + 1] | ||
_heat[x][y] *= factor | ||
|
||
|
||
@micropython.native # noqa: F821 | ||
def draw(): | ||
# take local references as it's quicker than accessing the global | ||
# and we access it a lot in this method | ||
_graphics = graphics | ||
_heat = heat | ||
_set_pen = graphics.set_pen | ||
_pixel = graphics.pixel | ||
_fire_colours = fire_colours | ||
|
||
for y in range(picounicorn.get_height()): | ||
for x in range(picounicorn.get_width()): | ||
value = _heat[y][x] | ||
if value < 0.15: | ||
_set_pen(_fire_colours[0]) | ||
elif value < 0.25: | ||
_set_pen(_fire_colours[1]) | ||
elif value < 0.35: | ||
_set_pen(_fire_colours[2]) | ||
elif value < 0.45: | ||
_set_pen(_fire_colours[3]) | ||
else: | ||
_set_pen(_fire_colours[4]) | ||
_pixel(x, y) | ||
|
||
picounicorn.update(_graphics) | ||
|
||
|
||
width = picounicorn.get_height() + 2 | ||
height = picounicorn.get_width() + 4 | ||
heat = [[0.0 for y in range(height)] for x in range(width)] | ||
fire_spawns = 1 | ||
damping_factor = 0.97 | ||
|
||
while True: | ||
|
||
start = time.ticks_ms() | ||
|
||
update() | ||
draw() | ||
|
||
print("total took: {} ms".format(time.ticks_ms() - start)) | ||
|
||
# And sleep, so we update ~ 60fps | ||
time.sleep(1.0 / 60) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters