Skip to content

Commit 9e0b3ad

Browse files
authored
Merge pull request #830 from ahnlak/pico-unicorn-mp-examples
Added some PicoGraphics based MP examples for the PicoUnicorn
2 parents 57042bf + 8c3a21e commit 9e0b3ad

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This is a PicoGraphics version of the original demo.py
2+
3+
from picounicorn import PicoUnicorn
4+
from picographics import PicoGraphics, DISPLAY_UNICORN_PACK
5+
6+
picounicorn = PicoUnicorn()
7+
graphics = PicoGraphics(display=DISPLAY_UNICORN_PACK)
8+
9+
w = picounicorn.get_width()
10+
h = picounicorn.get_height()
11+
12+
# Display a rainbow across Pico Unicorn
13+
for x in range(w):
14+
for y in range(h):
15+
# PicoGraphics allows us to set HSV pens directly
16+
PEN = graphics.create_pen_hsv(x / w, y / h, 1.0)
17+
graphics.set_pen(PEN)
18+
graphics.pixel(x, y)
19+
20+
# Ask the Unicorn to update the graphics
21+
picounicorn.update(graphics)
22+
23+
print("Press Button A")
24+
25+
while not picounicorn.is_pressed(picounicorn.BUTTON_A): # Wait for Button A to be pressed
26+
pass
27+
28+
# Clear the display
29+
30+
# Set the pen to black
31+
BLACK = graphics.create_pen(0, 0, 0)
32+
graphics.set_pen(BLACK)
33+
graphics.clear()
34+
35+
# Ask the Unicorn to update the graphics
36+
picounicorn.update(graphics)
37+
38+
print("Button A pressed!")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This is a PicoGraphics version of the original demo.py
2+
3+
from picounicorn import PicoUnicorn
4+
from picographics import PicoGraphics, DISPLAY_UNICORN_PACK
5+
import time
6+
7+
picounicorn = PicoUnicorn()
8+
graphics = PicoGraphics(display=DISPLAY_UNICORN_PACK)
9+
10+
w = picounicorn.get_width()
11+
h = picounicorn.get_height()
12+
13+
while True:
14+
t = time.ticks_ms() / 3600
15+
for x in range(w):
16+
for y in range(h):
17+
# PicoGraphics allows us to set HSV pens directly
18+
PEN = graphics.create_pen_hsv(t + ((x + y) / w / 4), 1.0, 1.0)
19+
graphics.set_pen(PEN)
20+
graphics.pixel(x, y)
21+
22+
# Ask the Unicorn to update the graphics
23+
picounicorn.update(graphics)
24+
25+
# And sleep, so we update ~ 60fps
26+
time.sleep(1.0 / 60)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import time
2+
import random
3+
from picounicorn import PicoUnicorn
4+
from picographics import PicoGraphics, DISPLAY_UNICORN_PACK
5+
6+
'''
7+
A pretty, procedural fire effect. Based on fire_effect.py from bigger Unicorns!
8+
'''
9+
10+
picounicorn = PicoUnicorn()
11+
graphics = PicoGraphics(display=DISPLAY_UNICORN_PACK)
12+
13+
fire_colours = [graphics.create_pen(0, 0, 0),
14+
graphics.create_pen(20, 20, 20),
15+
graphics.create_pen(180, 30, 0),
16+
graphics.create_pen(220, 160, 0),
17+
graphics.create_pen(255, 255, 180)]
18+
19+
20+
@micropython.native # noqa: F821
21+
def update():
22+
# take local references as it's quicker than accessing the global
23+
# and we access it a lot in this method
24+
_heat = heat
25+
26+
# clear the bottom row and then add a new fire seed to it
27+
for x in range(width):
28+
_heat[x][height - 1] = 0.0
29+
_heat[x][height - 2] = 0.0
30+
31+
for c in range(fire_spawns):
32+
x = random.randint(0, width - 4) + 2
33+
_heat[x + 0][height - 1] = 1.0
34+
_heat[x + 1][height - 1] = 1.0
35+
_heat[x - 1][height - 1] = 1.0
36+
_heat[x + 0][height - 2] = 1.0
37+
_heat[x + 1][height - 2] = 1.0
38+
_heat[x - 1][height - 2] = 1.0
39+
40+
factor = damping_factor / 5.0
41+
for y in range(0, height - 2):
42+
for x in range(1, width - 1):
43+
_heat[x][y] += _heat[x][y + 1] + _heat[x][y + 2] + _heat[x - 1][y + 1] + _heat[x + 1][y + 1]
44+
_heat[x][y] *= factor
45+
46+
47+
@micropython.native # noqa: F821
48+
def draw():
49+
# take local references as it's quicker than accessing the global
50+
# and we access it a lot in this method
51+
_graphics = graphics
52+
_heat = heat
53+
_set_pen = graphics.set_pen
54+
_pixel = graphics.pixel
55+
_fire_colours = fire_colours
56+
57+
for y in range(picounicorn.get_height()):
58+
for x in range(picounicorn.get_width()):
59+
value = _heat[y + 1][x]
60+
if value < 0.15:
61+
_set_pen(_fire_colours[0])
62+
elif value < 0.25:
63+
_set_pen(_fire_colours[1])
64+
elif value < 0.35:
65+
_set_pen(_fire_colours[2])
66+
elif value < 0.45:
67+
_set_pen(_fire_colours[3])
68+
else:
69+
_set_pen(_fire_colours[4])
70+
_pixel(x, y)
71+
72+
picounicorn.update(_graphics)
73+
74+
75+
width = picounicorn.get_height() + 2
76+
height = picounicorn.get_width() + 4
77+
heat = [[0.0 for y in range(height)] for x in range(width)]
78+
fire_spawns = 1
79+
damping_factor = 0.97
80+
81+
while True:
82+
83+
start = time.ticks_ms()
84+
85+
update()
86+
draw()
87+
88+
print("total took: {} ms".format(time.ticks_ms() - start))
89+
90+
# And sleep, so we update ~ 60fps
91+
time.sleep(1.0 / 60)

micropython/modules/picographics/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Bear in mind that MicroPython has only 192K of RAM available- a 320x240 pixel di
7373
* Interstate75 and 75W - HUB75 Matrix driver - `DISPLAY_INTERSTATE75_SIZEOFMATRIX` please read below!
7474
* Cosmic Unicorn - 32x32 LED Matrix - `DISPLAY_COSMIC_UNICORN`
7575
* Stellar Unicorn - 16x16 LED Matrix - `DISPLAY_STELLAR_UNICORN`
76+
* Pico Unicorn Pack - 16x7 LED Matrix - `DISPLAY_UNICORN_PACK`
7677

7778
#### Interstate75 and Interstate75W Display modes
7879

@@ -274,7 +275,7 @@ Send the contents of your Pico Graphics buffer to your screen:
274275
display.update()
275276
```
276277

277-
If you are using a Galactic Unicorn, then the process for updating the display is different. Instead of the above, do:
278+
If you are using a Unicorn (Galactic, Cosmic, Stellar or Pico), then the process for updating the display is different. Instead of the above, do:
278279

279280
```python
280281
galactic_unicorn.update(display)

0 commit comments

Comments
 (0)