diff --git a/micropython/examples/cosmic_unicorn/launch/fire.py b/micropython/examples/cosmic_unicorn/launch/fire.py index 31238a523..da7b1637e 100644 --- a/micropython/examples/cosmic_unicorn/launch/fire.py +++ b/micropython/examples/cosmic_unicorn/launch/fire.py @@ -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) diff --git a/micropython/examples/cosmic_unicorn/launch/main.py b/micropython/examples/cosmic_unicorn/launch/main.py index d40c22a5f..a51751af0 100644 --- a/micropython/examples/cosmic_unicorn/launch/main.py +++ b/micropython/examples/cosmic_unicorn/launch/main.py @@ -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 @@ # 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 @@ 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 @@ -79,7 +79,7 @@ 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 @@ -87,27 +87,27 @@ def 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) diff --git a/micropython/examples/cosmic_unicorn/launch/rainbow.py b/micropython/examples/cosmic_unicorn/launch/rainbow.py index 388e9e04e..9008e25c8 100644 --- a/micropython/examples/cosmic_unicorn/launch/rainbow.py +++ b/micropython/examples/cosmic_unicorn/launch/rainbow.py @@ -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 diff --git a/micropython/examples/cosmic_unicorn/launch/retroprompt.py b/micropython/examples/cosmic_unicorn/launch/retroprompt.py index 58c2cd361..6b7ac67e9 100644 --- a/micropython/examples/cosmic_unicorn/launch/retroprompt.py +++ b/micropython/examples/cosmic_unicorn/launch/retroprompt.py @@ -2,52 +2,86 @@ graphics = None palette = None - +prompt_x = 0 +prompt_y = 4 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 ", - " " + " ", + " ", + " OOOOO OOOOOO OO OOOO ", + " OO OO OO OOOO OO OO ", + " OO OO OO OO OO OO OO ", + " OOOOO OOOO OOOOOO OO OO ", + " OOOO OO OO OO OO OO ", + " OO OO OO OO OO OO OO ", + " OO OO OOOOOO OO OO OOOO ", + " ", + " ", + " ", + " ", + " OO OO XXXXXXX ", + " OO OO XXXXXXX ", + " OO OO XXXXXXX ", + " OOOO XXXXXXX ", + " OO XXXXXXX ", + " OO OO XXXXXXX ", + " 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 ", - " " + " ", + " ", + " O OOOO OOOO OOOOO ", + " O O O O O O O ", + " O O O O O O O ", + " O O O OOOOOO O O ", + " O O O O O O O ", + " OOOOOO OOOO O O OOOOO ", + " ", + " ", + " ", + " ", + " ", + " O O O O XXXXXXXX ", + " O O O O X XXXXXX ", + " X XXXXXX ", + " X XXXXXX ", + " X XXXXXX ", + " 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 ", - " " + " ", + " ", + " OOOOO OO OOOO OOO ", + " O O O O O O O ", + " O O O O O O ", + " OOOOO O O OOOO O ", + " O O OOOOOO O O ", + " O O O O O O O ", + " OOOOO O O OOOO OOO ", + " ", + " ", + " ", + " ", + " OOOO O ", + " O O O ", + " O O ", + " O O ", + " O O ", + " O O O ", + " OOOO O ", + " XXXXXXX ", + " " ] FOREGROUND_BBC_MICRO = (255, 255, 255) BACKGROUND_BBC_MICRO = (0, 0, 0) @@ -82,6 +116,8 @@ def draw(): fg_pen = graphics.create_pen(fg[0], fg[1], fg[2]) bg_pen = graphics.create_pen(bg[0], bg[1], bg[2]) + graphics.set_pen(bg_pen) + graphics.clear() for y in range(len(image)): row = image[y] for x in range(len(row)): @@ -97,4 +133,4 @@ def draw(): else: graphics.set_pen(bg_pen) - graphics.pixel(x, y) + graphics.pixel(x + prompt_x, y + prompt_y) diff --git a/micropython/examples/cosmic_unicorn/launch/supercomputer.py b/micropython/examples/cosmic_unicorn/launch/supercomputer.py index 9123ae539..8b67bc36a 100644 --- a/micropython/examples/cosmic_unicorn/launch/supercomputer.py +++ b/micropython/examples/cosmic_unicorn/launch/supercomputer.py @@ -1,5 +1,5 @@ import random -from galactic import GalacticUnicorn +from cosmic import CosmicUnicorn graphics = None @@ -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): diff --git a/micropython/examples/cosmic_unicorn/launch/today.py b/micropython/examples/cosmic_unicorn/launch/today.py new file mode 100644 index 000000000..98f5d3c76 --- /dev/null +++ b/micropython/examples/cosmic_unicorn/launch/today.py @@ -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))