Skip to content

Commit

Permalink
Merge pull request #676 from pimoroni/cosmic-launch-examples
Browse files Browse the repository at this point in the history
Cosmic Unicorn: Launch Examples.
  • Loading branch information
Gadgetoid committed Feb 21, 2023
2 parents c78e85a + 8188444 commit af46e03
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 142 deletions.
10 changes: 5 additions & 5 deletions micropython/examples/cosmic_unicorn/launch/fire.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
62 changes: 31 additions & 31 deletions micropython/examples/cosmic_unicorn/launch/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,14 +16,14 @@
# 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


Expand All @@ -33,30 +33,30 @@ def pressed():
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
Expand All @@ -79,35 +79,35 @@ def pressed():
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

was_sleep_pressed = sleep_pressed

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)
6 changes: 3 additions & 3 deletions micropython/examples/cosmic_unicorn/launch/rainbow.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
100 changes: 0 additions & 100 deletions micropython/examples/cosmic_unicorn/launch/retroprompt.py

This file was deleted.

6 changes: 3 additions & 3 deletions micropython/examples/cosmic_unicorn/launch/supercomputer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import random
from galactic import GalacticUnicorn
from cosmic import CosmicUnicorn

graphics = None

Expand All @@ -8,8 +8,8 @@

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):
Expand Down
94 changes: 94 additions & 0 deletions micropython/examples/cosmic_unicorn/launch/today.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit af46e03

Please sign in to comment.