2022-10-28 12:29:20 +00:00
|
|
|
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
|
2022-10-28 13:33:01 +00:00
|
|
|
def outline_text(text, x, y):
|
2022-10-28 12:29:20 +00:00
|
|
|
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)
|
2022-10-28 13:33:01 +00:00
|
|
|
|
|
|
|
# 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)
|
2022-10-28 12:29:20 +00:00
|
|
|
|
|
|
|
last_second = second
|
|
|
|
|
|
|
|
# update the display
|
|
|
|
gu.update(graphics)
|
|
|
|
|
|
|
|
time.sleep(0.01)
|