I just did this code up, appears to be working, hasn’t crashed anyway.
Another wall of code though.
Will have to wait until it gets dark to see if I need to tweak some values.
import time
import machine
import micropython
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
width = GalacticUnicorn.WIDTH
height = GalacticUnicorn.HEIGHT
#gu.set_brightness(0.1)
graphics.set_font("bitmap8")
BACKLIGHT_LOW = micropython.const(0.375)
BACKLIGHT_HIGH = micropython.const(1.0)
LUMINANCE_LOW = micropython.const(384)
LUMINANCE_HIGH = micropython.const(2048) # 65535 to use the full range.
SWITCH_A = 0
SWITCH_B = 1
SWITCH_C = 3
SWITCH_D = 6
SWITCH_SLEEP = 27
SWITCH_VOLUME_UP = 7
SWITCH_VOLUME_DOWN = 8
SWITCH_BRIGHTNESS_UP = 21
SWITCH_BRIGHTNESS_DOWN = 26
t_color = graphics.create_pen(0,0,0)
h_color = graphics.create_pen(0,0,0)
p_color = graphics.create_pen(0,0,0)
black = graphics.create_pen(0,0,0)
red = graphics.create_pen(255,0,0)
green = graphics.create_pen(0,255,0)
blue = graphics.create_pen(0,0,255)
yellow = graphics.create_pen(255,255,0)
orange = graphics.create_pen(255,140,0)
white = graphics.create_pen(0,255,255)
graphics.set_pen(black)
graphics.clear()
from breakout_bme280 import BreakoutBME280
from pimoroni_i2c import PimoroniI2C
i2c = PimoroniI2C(sda=(4), scl=(5))
bme = BreakoutBME280(i2c)
temperature, pressure, humidity = bme.read()
pressure = pressure / 100
temperature = round(temperature)
humidity = round(humidity)
pressuremb = round(pressure)
time.sleep(0.5)
from breakout_rtc import BreakoutRTC
from machine import RTC
RV3028 = BreakoutRTC(i2c)
rtc = BreakoutRTC(i2c)
if rtc.is_12_hour:
rtc.set_24_hour()
RV3028.update_time()
hour = rtc.get_hours()
minute = rtc.get_minutes()
month = rtc.get_month()
date = rtc.get_date()
weekday = rtc.get_weekday()
if rtc.read_periodic_update_interrupt_flag():
rtc.clear_periodic_update_interrupt_flag()
if rtc.update_time():
rtc_date = rtc.string_date()
rtc_time = rtc.string_time()
def button():
'''
if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP):
gu.adjust_brightness(+0.1)
if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN):
gu.adjust_brightness(-0.1)
'''
if gu.is_pressed(GalacticUnicorn.SWITCH_SLEEP):
graphics.set_pen(black)
graphics.clear()
gu.update(graphics)
machine.deepsleep()
'''
if gu.is_pressed(GalacticUnicorn.SWITCH_A):
gu.set_brightness(0.9)
if gu.is_pressed(GalacticUnicorn.SWITCH_B):
gu.set_brightness(0.7)
if gu.is_pressed(GalacticUnicorn.SWITCH_C):
gu.set_brightness(0.5)
if gu.is_pressed(GalacticUnicorn.SWITCH_D):
gu.set_brightness(0.3)
'''
return button
gu.update(graphics)
def describe_month(month):
month = rtc.get_month()
if month == 1:
description = "January"
elif month == 2:
description = "February"
elif month == 3:
description = "March"
elif month == 4:
description = "April"
elif month == 5:
description = "May"
elif month == 6:
description = "June"
elif month == 7:
description = "July"
elif month == 8:
description = "August"
elif month == 9:
description = "September"
elif month == 10:
description = "October"
elif month == 11:
description = "November"
elif month == 12:
description = "December"
return description
def describe_date(date):
date = rtc.get_date()
if date == 1:
description = "st"
elif date == 2:
description = "nd"
elif date == 3:
description = "rd"
elif date == 21:
description = "st"
elif date == 22:
description = "nd"
elif date == 23:
description = "rd"
elif date == 31:
description = "st"
else:
description = "th"
return description
def describe_temperature(temperature):
global t_color
if temperature < -10:
description = " Cold"
t_color = white
elif -10 <= temperature <= 0:
description = " Cold"
t_color = blue
elif 0 < temperature <= 12:
description = " Cool"
t_color = yellow
elif 12 < temperature <= 16:
description = " Warm"
t_color = green
elif 16 < temperature <= 24:
description = ""
t_color = green
elif 24 < temperature <= 27:
description = " Hot"
t_color = orange
elif temperature > 27:
description = " Hot+"
t_color = red
return description
def describe_humidity(humidity):
global h_color
if humidity < 30:
description = " Low"
h_color = red
elif 30 <= humidity <= 60:
description = ""
h_color = green
elif 60 < humidity < 80:
description = " High"
h_color = yellow
elif humidity >= 80:
description = " High"
h_color = orange
return description
def describe_pressure(pressuremb):
global p_color
if pressuremb < 982:
description = "-Low-"
p_color = red
elif 982 <= pressuremb < 1004:
description = "Low"
p_color = yellow
elif 1004 <= pressuremb < 1026:
description = ""
p_color = green
elif 1026 <= pressuremb < 1048:
description = "High"
p_color = blue
elif pressuremb >= 1048:
description = "+High+"
p_color = orange
return description
def auto_brightness(previous: float) -> (float, float):
luminance = gu.light()
luminance_frac = max(0.0, float(luminance - LUMINANCE_LOW))
luminance_frac = min(1.0, luminance_frac / (LUMINANCE_HIGH - LUMINANCE_LOW))
backlight = BACKLIGHT_LOW + (luminance_frac * (BACKLIGHT_HIGH - BACKLIGHT_LOW))
# Use the previous value to smooth out changes to reduce flickering.
# The "32" value here controls how quickly it reacts (larger = slower).
# The rate at which the main loop calls us also affects that!
backlight_diff = backlight - previous
backlight = previous + (backlight_diff * (1.0 / 8.0))
return (luminance, backlight)
#gu.set_brightness(BACKLIGHT_LOW)
backlight = BACKLIGHT_LOW
while True:
luminance = gu.light()
print(luminance)
(luminance, backlight) = auto_brightness(backlight)
gu.set_brightness(backlight)
print(backlight)
temperature, pressure, humidity = bme.read()
pressure = pressure / 100
temperature = round(temperature)
humidity = round(humidity)
pressuremb = round(pressure)
time.sleep(0.02)
RV3028.update_time()
hour = rtc.get_hours()
minute = rtc.get_minutes()
month = rtc.get_month()
date = rtc.get_date()
weekday = rtc.get_weekday()
if rtc.read_periodic_update_interrupt_flag():
rtc.clear_periodic_update_interrupt_flag()
if rtc.update_time():
rtc_date = rtc.string_date()
rtc_time = rtc.string_time()
graphics.set_pen(white)
if hour == 0:
graphics.text(f"{12}:{minute:02}AM", 0, 2, scale=1)
elif 0 <= hour < 10:
graphics.text(f"{hour:1}:{minute:02}AM", 0, 2, scale=1)
elif 10 <= hour < 12:
graphics.text(f"{hour:2}:{minute:02}AM", 0, 2, scale=1)
elif hour == 12:
graphics.text(f"{hour:2}:{minute:02}PM", 0, 2, scale=1)
elif hour > 12:
hour = hour - 12
if hour < 10:
graphics.text(f"{hour:1}:{minute:02}PM", 0, 2, scale=1)
elif 10 <= hour < 12:
graphics.text(f"{hour:2}:{minute:02}PM", 0, 2, scale=1)
elif hour == 12:
graphics.text(f"{hour:2}:{minute:02}AM", 0, 2, scale=1)
#button()
gu.update(graphics)
time.sleep(3)
graphics.set_pen(black)
graphics.clear()
graphics.set_pen(white)
if weekday == 0:
graphics.text("Monday", 0, 2, scale=1)
if weekday == 1:
graphics.text("Tuesday", 0, 2, scale=1)
if weekday == 2:
graphics.text("Wednesday", 0, 2, scale=1)
if weekday == 3:
graphics.text("Thursday", 0, 2, scale=1)
if weekday == 4:
graphics.text("Friday", 0, 2, scale=1)
if weekday == 5:
graphics.text("Saturday", 0, 2, scale=1)
if weekday == 6:
graphics.text("Sunday", 0, 2, scale=1)
button()
gu.update(graphics)
time.sleep(3)
graphics.set_pen(black)
graphics.clear()
graphics.set_pen(white)
describe_month(month)
graphics.text(f"{describe_month(month)} {date}{describe_date(date)}", 0, 2, scale=1)
button()
gu.update(graphics)
time.sleep(3)
graphics.set_pen(black)
graphics.clear()
describe_temperature(temperature)
graphics.set_pen(t_color)
graphics.text("{:0.0f}°C" .format(temperature), 0, 2, scale=1)
graphics.text(describe_temperature(temperature), 20, 2, scale=1)
button()
gu.update(graphics)
time.sleep(3)
graphics.set_pen(black)
graphics.clear()
describe_humidity(humidity)
graphics.set_pen(h_color)
graphics.text("{:0.0f}%" .format(humidity), 0, 2, scale=1)
graphics.text(describe_humidity(humidity), 16, 2, scale=1)
button()
gu.update(graphics)
time.sleep(3)
graphics.set_pen(black)
graphics.clear()
describe_pressure(pressuremb)
graphics.set_pen(p_color)
graphics.text("{:0.0f}mb" .format(pressuremb), 0, 2, scale=1)
graphics.text(describe_pressure(pressuremb), 30, 2, scale=1)
button()
gu.update(graphics)
time.sleep(3)
graphics.set_pen(black)
graphics.clear()
gu.update(graphics)
# pause for a moment (important or the USB serial device will fail)
time.sleep(0.001)