From 7ee40260c9a7609c23c9db88422db1effefd69a2 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Fri, 28 Oct 2022 13:29:20 +0100 Subject: [PATCH 1/2] Added a basic clock example that uses the Pico's RTC --- .../examples/galactic_unicorn/clock.py | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 micropython/examples/galactic_unicorn/clock.py diff --git a/micropython/examples/galactic_unicorn/clock.py b/micropython/examples/galactic_unicorn/clock.py new file mode 100644 index 00000000..660ab118 --- /dev/null +++ b/micropython/examples/galactic_unicorn/clock.py @@ -0,0 +1,134 @@ +import time +import math +import machine +from galactic import GalacticUnicorn +from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY + +# constants for controlling the background colour throughout the day +MIDDAY_HUE = 1.1 +MIDNIGHT_HUE = 0.8 +HUE_OFFSET = -0.1 + +MIDDAY_SATURATION = 1.0 +MIDNIGHT_SATURATION = 1.0 + +MIDDAY_VALUE = 0.8 +MIDNIGHT_VALUE = 0.3 + + +# create galactic object and graphics surface for drawing +gu = GalacticUnicorn() +graphics = PicoGraphics(DISPLAY) + +# create the rtc object +rtc = machine.RTC() + +width = GalacticUnicorn.WIDTH +height = GalacticUnicorn.HEIGHT + +# set up some pens to use later +WHITE = graphics.create_pen(255, 255, 255) +BLACK = graphics.create_pen(0, 0, 0) + + +@micropython.native # noqa: F821 +def from_hsv(h, s, v): + i = math.floor(h * 6.0) + f = h * 6.0 - i + v *= 255.0 + p = v * (1.0 - s) + q = v * (1.0 - f * s) + t = v * (1.0 - (1.0 - f) * s) + + i = int(i) % 6 + if i == 0: + return int(v), int(t), int(p) + if i == 1: + return int(q), int(v), int(p) + if i == 2: + return int(p), int(v), int(t) + if i == 3: + return int(p), int(q), int(v) + if i == 4: + return int(t), int(p), int(v) + if i == 5: + return int(v), int(p), int(q) + + +# function for drawing a gradient background +def gradient_background(start_hue, start_sat, start_val, end_hue, end_sat, end_val): + half_width = width // 2 + for x in range(0, half_width): + hue = ((end_hue - start_hue) * (x / half_width)) + start_hue + sat = ((end_sat - start_sat) * (x / half_width)) + start_sat + val = ((end_val - start_val) * (x / half_width)) + start_val + colour = from_hsv(hue, sat, val) + graphics.set_pen(graphics.create_pen(int(colour[0]), int(colour[1]), int(colour[2]))) + for y in range(0, height): + graphics.pixel(x, y) + graphics.pixel(width - x - 1, y) + + colour = from_hsv(end_hue, end_sat, end_val) + graphics.set_pen(graphics.create_pen(int(colour[0]), int(colour[1]), int(colour[2]))) + for y in range(0, height): + graphics.pixel(half_width, y) + + +# function for drawing outlined text +def outline_text(text): + graphics.set_font("bitmap8") + w = graphics.measure_text(text, 1) + + x = int(width / 2 - w / 2 + 1) + y = 2 + + graphics.set_pen(BLACK) + graphics.text(text, x - 1, y - 1, -1, 1) + graphics.text(text, x, y - 1, -1, 1) + graphics.text(text, x + 1, y - 1, -1, 1) + graphics.text(text, x - 1, y, -1, 1) + graphics.text(text, x + 1, y, -1, 1) + graphics.text(text, x - 1, y + 1, -1, 1) + graphics.text(text, x, y + 1, -1, 1) + graphics.text(text, x + 1, y + 1, -1, 1) + + graphics.set_pen(WHITE) + graphics.text(text, x, y, -1, 1) + + +year, month, day, wd, hour, minute, second, _ = rtc.datetime() + +last_second = second + +gu.set_brightness(0.5) + +while True: + if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP): + gu.adjust_brightness(+0.01) + + if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN): + gu.adjust_brightness(-0.01) + + year, month, day, wd, hour, minute, second, _ = rtc.datetime() + if second != last_second: + time_through_day = (((hour * 60) + minute) * 60) + second + percent_through_day = time_through_day / 86400 + percent_to_midday = 1.0 - ((math.cos(percent_through_day * math.pi * 2) + 1) / 2) + print(percent_to_midday) + + hue = ((MIDDAY_HUE - MIDNIGHT_HUE) * percent_to_midday) + MIDNIGHT_HUE + sat = ((MIDDAY_SATURATION - MIDNIGHT_SATURATION) * percent_to_midday) + MIDNIGHT_SATURATION + val = ((MIDDAY_VALUE - MIDNIGHT_VALUE) * percent_to_midday) + MIDNIGHT_VALUE + + gradient_background(hue, sat, val, + hue + HUE_OFFSET, sat, val) + + clock = "{:02}:{:02}:{:02}".format(hour, minute, second) + outline_text(clock) + + last_second = second + + # update the display + gu.update(graphics) + + time.sleep(0.01) From f74261474ed8f73f563576ad0a229b8b32f676f4 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Fri, 28 Oct 2022 14:33:01 +0100 Subject: [PATCH 2/2] Added scroll example and improved clock --- .../examples/galactic_unicorn/clock.py | 19 ++-- .../galactic_unicorn/scrolling_text.py | 90 +++++++++++++++++++ 2 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 micropython/examples/galactic_unicorn/scrolling_text.py diff --git a/micropython/examples/galactic_unicorn/clock.py b/micropython/examples/galactic_unicorn/clock.py index 660ab118..e56278d9 100644 --- a/micropython/examples/galactic_unicorn/clock.py +++ b/micropython/examples/galactic_unicorn/clock.py @@ -75,13 +75,7 @@ def gradient_background(start_hue, start_sat, start_val, end_hue, end_sat, end_v # function for drawing outlined text -def outline_text(text): - graphics.set_font("bitmap8") - w = graphics.measure_text(text, 1) - - x = int(width / 2 - w / 2 + 1) - y = 2 - +def outline_text(text, x, y): graphics.set_pen(BLACK) graphics.text(text, x - 1, y - 1, -1, 1) graphics.text(text, x, y - 1, -1, 1) @@ -124,7 +118,16 @@ while True: hue + HUE_OFFSET, sat, val) clock = "{:02}:{:02}:{:02}".format(hour, minute, second) - outline_text(clock) + + # set the font + graphics.set_font("bitmap8") + + # calculate text position so that it is centred + w = graphics.measure_text(clock, 1) + x = int(width / 2 - w / 2 + 1) + y = 2 + + outline_text(clock, x, y) last_second = second diff --git a/micropython/examples/galactic_unicorn/scrolling_text.py b/micropython/examples/galactic_unicorn/scrolling_text.py new file mode 100644 index 00000000..3210dec1 --- /dev/null +++ b/micropython/examples/galactic_unicorn/scrolling_text.py @@ -0,0 +1,90 @@ +import time +from galactic import GalacticUnicorn +from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY + +# constants for controlling scrolling text +PADDING = 5 +MESSAGE_COLOUR = (255, 255, 255) +OUTLINE_COLOUR = (0, 0, 0) +BACKGROUND_COLOUR = (10, 0, 96) +MESSAGE = "\"Space is big. Really big. You just won't believe how vastly hugely mind-bogglingly big it is. I mean, you may think it's a long way down the road to the chemist, but that's just peanuts to space.\" - Douglas Adams" +HOLD_TIME = 2.0 +STEP_TIME = 0.075 + +# create galactic object and graphics surface for drawing +gu = GalacticUnicorn() +graphics = PicoGraphics(DISPLAY) + +width = GalacticUnicorn.WIDTH +height = GalacticUnicorn.HEIGHT + + +# function for drawing outlined text +def outline_text(text, x, y): + graphics.set_pen(graphics.create_pen(int(OUTLINE_COLOUR[0]), int(OUTLINE_COLOUR[1]), int(OUTLINE_COLOUR[2]))) + graphics.text(text, x - 1, y - 1, -1, 1) + graphics.text(text, x, y - 1, -1, 1) + graphics.text(text, x + 1, y - 1, -1, 1) + graphics.text(text, x - 1, y, -1, 1) + graphics.text(text, x + 1, y, -1, 1) + graphics.text(text, x - 1, y + 1, -1, 1) + graphics.text(text, x, y + 1, -1, 1) + graphics.text(text, x + 1, y + 1, -1, 1) + + graphics.set_pen(graphics.create_pen(int(MESSAGE_COLOUR[0]), int(MESSAGE_COLOUR[1]), int(MESSAGE_COLOUR[2]))) + graphics.text(text, x, y, -1, 1) + + +gu.set_brightness(0.5) + +# state constants +STATE_PRE_SCROLL = 0 +STATE_SCROLLING = 1 +STATE_POST_SCROLL = 2 + +shift = 0 +state = STATE_PRE_SCROLL + +# set the font +graphics.set_font("bitmap8") + +# calculate the message width so scrolling can happen +msg_width = graphics.measure_text(MESSAGE, 1) + +last_time = time.ticks_ms() + +while True: + time_ms = time.ticks_ms() + + if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP): + gu.adjust_brightness(+0.01) + + if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN): + gu.adjust_brightness(-0.01) + + if state == STATE_PRE_SCROLL and time_ms - last_time > HOLD_TIME * 1000: + if msg_width + PADDING * 2 >= width: + state = STATE_SCROLLING + last_time = time_ms + + if state == STATE_SCROLLING and time_ms - last_time > STEP_TIME * 1000: + shift += 1 + if shift >= (msg_width + PADDING * 2) - width - 1: + state = STATE_POST_SCROLL + last_time = time_ms + + if state == STATE_POST_SCROLL and time_ms - last_time > HOLD_TIME * 1000: + state = STATE_PRE_SCROLL + shift = 0 + last_time = time_ms + + graphics.set_pen(graphics.create_pen(int(BACKGROUND_COLOUR[0]), int(BACKGROUND_COLOUR[1]), int(BACKGROUND_COLOUR[2]))) + graphics.clear() + + outline_text(MESSAGE, x=PADDING - shift, y=2) + + # update the display + gu.update(graphics) + + # pause for a moment (important or the USB serial device will fail) + time.sleep(0.001)