Cosmic Unicorn: Launch Examples.

Co-authored-by: thirdr <ryan@pimoroni.com>
pull/676/head
Phil Howard 2023-02-21 13:23:37 +00:00
rodzic 049219c7f4
commit 81884448ab
6 zmienionych plików z 136 dodań i 142 usunięć

Wyświetl plik

@ -1,12 +1,12 @@
import random
from galactic import GalacticUnicorn
from cosmic import CosmicUnicorn
graphics = None
palette = None
# setup heat value buffer and fire parameters
width = GalacticUnicorn.WIDTH + 2
height = GalacticUnicorn.HEIGHT + 4
width = CosmicUnicorn.WIDTH + 2
height = CosmicUnicorn.HEIGHT + 4
heat = [[0.0 for y in range(height)] for x in range(width)]
fire_spawns = 5
damping_factor = 0.97
@ -70,8 +70,8 @@ def draw():
heat[x][y] = average
# render the heat values to the graphics buffer
for y in range(GalacticUnicorn.HEIGHT):
for x in range(GalacticUnicorn.WIDTH):
for y in range(CosmicUnicorn.HEIGHT):
for x in range(CosmicUnicorn.WIDTH):
graphics.set_pen(pen_from_value(heat[x + 1][y]))
graphics.pixel(x, y)

Wyświetl plik

@ -1,13 +1,13 @@
import time
import machine
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
from cosmic import CosmicUnicorn
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
# overclock to 200Mhz
machine.freq(200000000)
# create galactic object and graphics surface for drawing
galactic = GalacticUnicorn()
# create cosmic object and graphics surface for drawing
cosmic = CosmicUnicorn()
graphics = PicoGraphics(DISPLAY)
brightness = 0.5
@ -16,14 +16,14 @@ brightness = 0.5
# returns the id of the button that is currently pressed or
# None if none are
def pressed():
if galactic.is_pressed(GalacticUnicorn.SWITCH_A):
return GalacticUnicorn.SWITCH_A
if galactic.is_pressed(GalacticUnicorn.SWITCH_B):
return GalacticUnicorn.SWITCH_B
if galactic.is_pressed(GalacticUnicorn.SWITCH_C):
return GalacticUnicorn.SWITCH_C
if galactic.is_pressed(GalacticUnicorn.SWITCH_D):
return GalacticUnicorn.SWITCH_D
if cosmic.is_pressed(CosmicUnicorn.SWITCH_A):
return CosmicUnicorn.SWITCH_A
if cosmic.is_pressed(CosmicUnicorn.SWITCH_B):
return CosmicUnicorn.SWITCH_B
if cosmic.is_pressed(CosmicUnicorn.SWITCH_C):
return CosmicUnicorn.SWITCH_C
if cosmic.is_pressed(CosmicUnicorn.SWITCH_D):
return CosmicUnicorn.SWITCH_D
return None
@ -33,30 +33,30 @@ while True:
graphics.set_pen(graphics.create_pen(0, 0, 0))
graphics.clear()
graphics.set_pen(graphics.create_pen(155, 155, 155))
graphics.text("PRESS", 12, -1, -1, 1)
graphics.text("A B C OR D!", 2, 5, -1, 1)
graphics.text("PRESS", 3, 6, -1, 1)
graphics.text("A B C OR D!", 5, 14, 32, 1, 0)
# brightness up/down
if galactic.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP):
if cosmic.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_UP):
brightness += 0.01
if galactic.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN):
if cosmic.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_DOWN):
brightness -= 0.01
brightness = max(min(brightness, 1.0), 0.0)
galactic.set_brightness(brightness)
galactic.update(graphics)
cosmic.set_brightness(brightness)
cosmic.update(graphics)
if pressed() == GalacticUnicorn.SWITCH_A:
if pressed() == CosmicUnicorn.SWITCH_A:
import fire as effect
break
if pressed() == GalacticUnicorn.SWITCH_B:
if pressed() == CosmicUnicorn.SWITCH_B:
import supercomputer as effect # noqa: F811
break
if pressed() == GalacticUnicorn.SWITCH_C:
if pressed() == CosmicUnicorn.SWITCH_C:
import rainbow as effect # noqa: F811
break
if pressed() == GalacticUnicorn.SWITCH_D:
import retroprompt as effect # noqa: F811
if pressed() == CosmicUnicorn.SWITCH_D:
import today as effect # noqa: F811
break
# pause for a moment
@ -79,7 +79,7 @@ while True:
if pressed() is not None:
machine.reset()
sleep_pressed = galactic.is_pressed(GalacticUnicorn.SWITCH_SLEEP)
sleep_pressed = cosmic.is_pressed(CosmicUnicorn.SWITCH_SLEEP)
if sleep_pressed and not was_sleep_pressed:
sleep = not sleep
@ -87,27 +87,27 @@ while True:
if sleep:
# fade out if screen not off
galactic.set_brightness(galactic.get_brightness() - 0.01)
cosmic.set_brightness(cosmic.get_brightness() - 0.01)
if galactic.get_brightness() > 0.0:
if cosmic.get_brightness() > 0.0:
effect.draw()
# update the display
galactic.update(graphics)
cosmic.update(graphics)
else:
effect.draw()
# update the display
galactic.update(graphics)
cosmic.update(graphics)
# brightness up/down
if galactic.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP):
if cosmic.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_UP):
brightness += 0.01
if galactic.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN):
if cosmic.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_DOWN):
brightness -= 0.01
brightness = max(min(brightness, 1.0), 0.0)
galactic.set_brightness(brightness)
cosmic.set_brightness(brightness)
# pause for a moment (important or the USB serial device will fail
time.sleep(0.001)

Wyświetl plik

@ -1,11 +1,11 @@
import math
from galactic import GalacticUnicorn
from cosmic import CosmicUnicorn
graphics = None
palette = None
width = GalacticUnicorn.WIDTH
height = GalacticUnicorn.HEIGHT
width = CosmicUnicorn.WIDTH
height = CosmicUnicorn.HEIGHT
@micropython.native # noqa: F821

Wyświetl plik

@ -1,100 +0,0 @@
import time
graphics = None
palette = None
c64 = [
" ",
" ",
" OOOOO OOOOOO OO OOOO OO OO XXXXXXX ",
" OO OO OO OOOO OO OO OO OO XXXXXXX ",
" OO OO OO OO OO OO OO OO OO XXXXXXX ",
" OOOOO OOOO OOOOOO OO OO OOOO XXXXXXX ",
" OOOO OO OO OO OO OO OO XXXXXXX ",
" OO OO OO OO OO OO OO OO OO XXXXXXX ",
" OO OO OOOOOO OO OO OOOO OO OO XXXXXXX ",
" XXXXXXX ",
" "
]
FOREGROUND_C64 = (230, 210, 250)
BACKGROUND_C64 = (20, 20, 120)
spectrum = [
" ",
" ",
" O OOOO OOOO OOOOO O O O O XXXXXXXX ",
" O O O O O O O O O O O X XXXXXX ",
" O O O O O O O X XXXXXX ",
" O O O OOOOOO O O X XXXXXX ",
" O O O O O O O X XXXXXX ",
" OOOOOO OOOO O O OOOOO X XXXXXX ",
" X X ",
" XXXXXXXX ",
" "
]
FOREGROUND_SPECTRUM = (0, 0, 0)
BACKGROUND_SPECTRUM = (180, 150, 150)
bbc_micro = [
" ",
" ",
" OOOOO OO OOOO OOO OOOO O ",
" O O O O O O O O O O ",
" O O O O O O O O ",
" OOOOO O O OOOO O O O ",
" O O OOOOOO O O O O ",
" O O O O O O O O O O ",
" OOOOO O O OOOO OOO OOOO O ",
" XXXXXXX ",
" "
]
FOREGROUND_BBC_MICRO = (255, 255, 255)
BACKGROUND_BBC_MICRO = (0, 0, 0)
PROMPT_C64 = 0
PROMPT_SPECTRUM = 1
PROMPT_BBC_MICRO = 2
prompt = 0
def init():
pass
@micropython.native # noqa: F821
def draw():
time_ms = time.ticks_ms()
prompt = (time_ms // 3000) % 3
if prompt == PROMPT_C64:
image = c64
fg = FOREGROUND_C64
bg = BACKGROUND_C64
elif prompt == PROMPT_SPECTRUM:
image = spectrum
fg = FOREGROUND_SPECTRUM
bg = BACKGROUND_SPECTRUM
elif prompt == PROMPT_BBC_MICRO:
image = bbc_micro
fg = FOREGROUND_BBC_MICRO
bg = BACKGROUND_BBC_MICRO
fg_pen = graphics.create_pen(fg[0], fg[1], fg[2])
bg_pen = graphics.create_pen(bg[0], bg[1], bg[2])
for y in range(len(image)):
row = image[y]
for x in range(len(row)):
pixel = row[x]
# draw the prompt text
if pixel == 'O':
graphics.set_pen(fg_pen)
# draw the caret blinking
elif pixel == 'X' and (time_ms // 300) % 2:
graphics.set_pen(fg_pen)
else:
graphics.set_pen(bg_pen)
graphics.pixel(x, y)

Wyświetl plik

@ -1,5 +1,5 @@
import random
from galactic import GalacticUnicorn
from cosmic import CosmicUnicorn
graphics = None
@ -8,8 +8,8 @@ colour = (230, 150, 0)
def init():
global width, height, lifetime, age
width = GalacticUnicorn.WIDTH
height = GalacticUnicorn.HEIGHT
width = CosmicUnicorn.WIDTH
height = CosmicUnicorn.HEIGHT
lifetime = [[0.0 for y in range(height)] for x in range(width)]
age = [[0.0 for y in range(height)] for x in range(width)]
for y in range(height):

Wyświetl plik

@ -0,0 +1,94 @@
import time
import network
import ntptime
import machine
# You will need to create or update the file secrets.py with your network credentials using Thonny
# in order for the example to update using the NTP.
# secrets.py should contain:
# WIFI_SSID = ""
# WIFI_PASSWORD = ""
try:
from secrets import WIFI_SSID, WIFI_PASSWORD
except ImportError:
print("Create secrets.py with your WiFi credentials")
graphics = None
WIDTH = 32 # CosmicUnicorn.WIDTH
HEIGHT = 32 # CosmicUnicorn.HEIGHT
rtc = machine.RTC()
DAYS = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
def network_connect(SSID, PSK):
# Enable the Wireless
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# Number of attempts to make before timeout
max_wait = 5
# Sets the Wireless LED pulsing and attempts to connect to your local network.
print("connecting...")
wlan.connect(SSID, PSK)
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error. Switches the Warn LED on.
if wlan.status() != 3:
print("Unable to connect. Attempting connection again")
# Function to sync the Pico RTC using NTP
def sync_time():
network_connect(WIFI_SSID, WIFI_PASSWORD)
try:
ntptime.settime()
except OSError:
print("Unable to sync with NTP server. Check network and try again.")
def init():
sync_time()
def draw():
# Pens
RED = graphics.create_pen(120, 0, 0)
WHITE = graphics.create_pen(255, 255, 255)
current_t = rtc.datetime()
# Set the pen to Red and clear the screen.
graphics.set_pen(WHITE)
graphics.clear()
# Measures the length of the text to help us with centring later.
day_length = graphics.measure_text(DAYS[current_t[3]], 1)
date_length = graphics.measure_text(str(current_t[2]), 3)
graphics.set_font("bitmap6")
graphics.set_pen(RED)
graphics.rectangle(0, 0, WIDTH, 7)
graphics.set_pen(WHITE)
graphics.text(DAYS[current_t[3]], (WIDTH // 2) - (day_length // 2) - 1, 0, 32, 1)
graphics.set_pen(RED)
graphics.set_font("bitmap8")
graphics.text(str(current_t[2]), (WIDTH // 2) - (date_length // 2) + 1, 9, 32, 3)
graphics.set_pen(graphics.create_pen(0, 0, 0))