Skip to content

Commit af46e03

Browse files
authored
Merge pull request #676 from pimoroni/cosmic-launch-examples
Cosmic Unicorn: Launch Examples.
2 parents c78e85a + 8188444 commit af46e03

File tree

6 files changed

+136
-142
lines changed

6 files changed

+136
-142
lines changed

micropython/examples/cosmic_unicorn/launch/fire.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import random
2-
from galactic import GalacticUnicorn
2+
from cosmic import CosmicUnicorn
33

44
graphics = None
55
palette = None
66

77
# setup heat value buffer and fire parameters
8-
width = GalacticUnicorn.WIDTH + 2
9-
height = GalacticUnicorn.HEIGHT + 4
8+
width = CosmicUnicorn.WIDTH + 2
9+
height = CosmicUnicorn.HEIGHT + 4
1010
heat = [[0.0 for y in range(height)] for x in range(width)]
1111
fire_spawns = 5
1212
damping_factor = 0.97
@@ -70,8 +70,8 @@ def draw():
7070
heat[x][y] = average
7171

7272
# render the heat values to the graphics buffer
73-
for y in range(GalacticUnicorn.HEIGHT):
74-
for x in range(GalacticUnicorn.WIDTH):
73+
for y in range(CosmicUnicorn.HEIGHT):
74+
for x in range(CosmicUnicorn.WIDTH):
7575
graphics.set_pen(pen_from_value(heat[x + 1][y]))
7676
graphics.pixel(x, y)
7777

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import time
22
import machine
3-
from galactic import GalacticUnicorn
4-
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
3+
from cosmic import CosmicUnicorn
4+
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
55

66
# overclock to 200Mhz
77
machine.freq(200000000)
88

9-
# create galactic object and graphics surface for drawing
10-
galactic = GalacticUnicorn()
9+
# create cosmic object and graphics surface for drawing
10+
cosmic = CosmicUnicorn()
1111
graphics = PicoGraphics(DISPLAY)
1212

1313
brightness = 0.5
@@ -16,14 +16,14 @@
1616
# returns the id of the button that is currently pressed or
1717
# None if none are
1818
def pressed():
19-
if galactic.is_pressed(GalacticUnicorn.SWITCH_A):
20-
return GalacticUnicorn.SWITCH_A
21-
if galactic.is_pressed(GalacticUnicorn.SWITCH_B):
22-
return GalacticUnicorn.SWITCH_B
23-
if galactic.is_pressed(GalacticUnicorn.SWITCH_C):
24-
return GalacticUnicorn.SWITCH_C
25-
if galactic.is_pressed(GalacticUnicorn.SWITCH_D):
26-
return GalacticUnicorn.SWITCH_D
19+
if cosmic.is_pressed(CosmicUnicorn.SWITCH_A):
20+
return CosmicUnicorn.SWITCH_A
21+
if cosmic.is_pressed(CosmicUnicorn.SWITCH_B):
22+
return CosmicUnicorn.SWITCH_B
23+
if cosmic.is_pressed(CosmicUnicorn.SWITCH_C):
24+
return CosmicUnicorn.SWITCH_C
25+
if cosmic.is_pressed(CosmicUnicorn.SWITCH_D):
26+
return CosmicUnicorn.SWITCH_D
2727
return None
2828

2929

@@ -33,30 +33,30 @@ def pressed():
3333
graphics.set_pen(graphics.create_pen(0, 0, 0))
3434
graphics.clear()
3535
graphics.set_pen(graphics.create_pen(155, 155, 155))
36-
graphics.text("PRESS", 12, -1, -1, 1)
37-
graphics.text("A B C OR D!", 2, 5, -1, 1)
36+
graphics.text("PRESS", 3, 6, -1, 1)
37+
graphics.text("A B C OR D!", 5, 14, 32, 1, 0)
3838

3939
# brightness up/down
40-
if galactic.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP):
40+
if cosmic.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_UP):
4141
brightness += 0.01
42-
if galactic.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN):
42+
if cosmic.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_DOWN):
4343
brightness -= 0.01
4444
brightness = max(min(brightness, 1.0), 0.0)
4545

46-
galactic.set_brightness(brightness)
47-
galactic.update(graphics)
46+
cosmic.set_brightness(brightness)
47+
cosmic.update(graphics)
4848

49-
if pressed() == GalacticUnicorn.SWITCH_A:
49+
if pressed() == CosmicUnicorn.SWITCH_A:
5050
import fire as effect
5151
break
52-
if pressed() == GalacticUnicorn.SWITCH_B:
52+
if pressed() == CosmicUnicorn.SWITCH_B:
5353
import supercomputer as effect # noqa: F811
5454
break
55-
if pressed() == GalacticUnicorn.SWITCH_C:
55+
if pressed() == CosmicUnicorn.SWITCH_C:
5656
import rainbow as effect # noqa: F811
5757
break
58-
if pressed() == GalacticUnicorn.SWITCH_D:
59-
import retroprompt as effect # noqa: F811
58+
if pressed() == CosmicUnicorn.SWITCH_D:
59+
import today as effect # noqa: F811
6060
break
6161

6262
# pause for a moment
@@ -79,35 +79,35 @@ def pressed():
7979
if pressed() is not None:
8080
machine.reset()
8181

82-
sleep_pressed = galactic.is_pressed(GalacticUnicorn.SWITCH_SLEEP)
82+
sleep_pressed = cosmic.is_pressed(CosmicUnicorn.SWITCH_SLEEP)
8383
if sleep_pressed and not was_sleep_pressed:
8484
sleep = not sleep
8585

8686
was_sleep_pressed = sleep_pressed
8787

8888
if sleep:
8989
# fade out if screen not off
90-
galactic.set_brightness(galactic.get_brightness() - 0.01)
90+
cosmic.set_brightness(cosmic.get_brightness() - 0.01)
9191

92-
if galactic.get_brightness() > 0.0:
92+
if cosmic.get_brightness() > 0.0:
9393
effect.draw()
9494

9595
# update the display
96-
galactic.update(graphics)
96+
cosmic.update(graphics)
9797
else:
9898
effect.draw()
9999

100100
# update the display
101-
galactic.update(graphics)
101+
cosmic.update(graphics)
102102

103103
# brightness up/down
104-
if galactic.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP):
104+
if cosmic.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_UP):
105105
brightness += 0.01
106-
if galactic.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN):
106+
if cosmic.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_DOWN):
107107
brightness -= 0.01
108108
brightness = max(min(brightness, 1.0), 0.0)
109109

110-
galactic.set_brightness(brightness)
110+
cosmic.set_brightness(brightness)
111111

112112
# pause for a moment (important or the USB serial device will fail
113113
time.sleep(0.001)

micropython/examples/cosmic_unicorn/launch/rainbow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import math
2-
from galactic import GalacticUnicorn
2+
from cosmic import CosmicUnicorn
33

44
graphics = None
55
palette = None
66

7-
width = GalacticUnicorn.WIDTH
8-
height = GalacticUnicorn.HEIGHT
7+
width = CosmicUnicorn.WIDTH
8+
height = CosmicUnicorn.HEIGHT
99

1010

1111
@micropython.native # noqa: F821

micropython/examples/cosmic_unicorn/launch/retroprompt.py

Lines changed: 0 additions & 100 deletions
This file was deleted.

micropython/examples/cosmic_unicorn/launch/supercomputer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import random
2-
from galactic import GalacticUnicorn
2+
from cosmic import CosmicUnicorn
33

44
graphics = None
55

@@ -8,8 +8,8 @@
88

99
def init():
1010
global width, height, lifetime, age
11-
width = GalacticUnicorn.WIDTH
12-
height = GalacticUnicorn.HEIGHT
11+
width = CosmicUnicorn.WIDTH
12+
height = CosmicUnicorn.HEIGHT
1313
lifetime = [[0.0 for y in range(height)] for x in range(width)]
1414
age = [[0.0 for y in range(height)] for x in range(width)]
1515
for y in range(height):
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import time
2+
import network
3+
import ntptime
4+
import machine
5+
6+
# You will need to create or update the file secrets.py with your network credentials using Thonny
7+
# in order for the example to update using the NTP.
8+
9+
# secrets.py should contain:
10+
# WIFI_SSID = ""
11+
# WIFI_PASSWORD = ""
12+
13+
try:
14+
from secrets import WIFI_SSID, WIFI_PASSWORD
15+
except ImportError:
16+
print("Create secrets.py with your WiFi credentials")
17+
18+
graphics = None
19+
20+
WIDTH = 32 # CosmicUnicorn.WIDTH
21+
HEIGHT = 32 # CosmicUnicorn.HEIGHT
22+
23+
rtc = machine.RTC()
24+
25+
DAYS = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
26+
27+
28+
def network_connect(SSID, PSK):
29+
# Enable the Wireless
30+
wlan = network.WLAN(network.STA_IF)
31+
wlan.active(True)
32+
33+
# Number of attempts to make before timeout
34+
max_wait = 5
35+
36+
# Sets the Wireless LED pulsing and attempts to connect to your local network.
37+
print("connecting...")
38+
wlan.connect(SSID, PSK)
39+
40+
while max_wait > 0:
41+
if wlan.status() < 0 or wlan.status() >= 3:
42+
break
43+
max_wait -= 1
44+
print('waiting for connection...')
45+
time.sleep(1)
46+
47+
# Handle connection error. Switches the Warn LED on.
48+
if wlan.status() != 3:
49+
print("Unable to connect. Attempting connection again")
50+
51+
52+
# Function to sync the Pico RTC using NTP
53+
def sync_time():
54+
55+
network_connect(WIFI_SSID, WIFI_PASSWORD)
56+
57+
try:
58+
ntptime.settime()
59+
except OSError:
60+
print("Unable to sync with NTP server. Check network and try again.")
61+
62+
63+
def init():
64+
65+
sync_time()
66+
67+
68+
def draw():
69+
70+
# Pens
71+
RED = graphics.create_pen(120, 0, 0)
72+
WHITE = graphics.create_pen(255, 255, 255)
73+
74+
current_t = rtc.datetime()
75+
76+
# Set the pen to Red and clear the screen.
77+
graphics.set_pen(WHITE)
78+
graphics.clear()
79+
80+
# Measures the length of the text to help us with centring later.
81+
day_length = graphics.measure_text(DAYS[current_t[3]], 1)
82+
date_length = graphics.measure_text(str(current_t[2]), 3)
83+
84+
graphics.set_font("bitmap6")
85+
graphics.set_pen(RED)
86+
graphics.rectangle(0, 0, WIDTH, 7)
87+
graphics.set_pen(WHITE)
88+
graphics.text(DAYS[current_t[3]], (WIDTH // 2) - (day_length // 2) - 1, 0, 32, 1)
89+
90+
graphics.set_pen(RED)
91+
graphics.set_font("bitmap8")
92+
graphics.text(str(current_t[2]), (WIDTH // 2) - (date_length // 2) + 1, 9, 32, 3)
93+
94+
graphics.set_pen(graphics.create_pen(0, 0, 0))

0 commit comments

Comments
 (0)