I normally run off of battery power, the USB comment was just mentioned as part of my debugging process, but I appreciate that wasn’t clear! The entire script is over 3000 lines long, so might not make sense to paste it all(!). I’ll try to get a dropbox link later, but here is my main function for now:
\\\
def main():
global _is_connected, _has_time, IMAGE_SOURCE
print(“Starting Inky Frame Calendar Display”)
force_garbage_collection()
_safe_remove_clip()
display.set_pen(WHITE)
display.clear()
cleanup_temp_files()
# Retry WiFi connection
for attempt in range(MAX_WIFI_RETRIES):
if connect_wifi():
_is_connected = True
break
else:
print(f"WiFi attempt {attempt + 1} failed. Retrying in {RETRY_DELAY} seconds...")
time.sleep(RETRY_DELAY)
else:
print("WiFi unavailable after retries – switching to SD card mode")
_is_connected = False
IMAGE_SOURCE = "SD_CARD"
# Mount SD card if needed, with fallback to Dropbox if WiFi is available
if IMAGE_SOURCE == "SD_CARD":
if not mount_sd_card():
print("WARNING: SD card mount failed but IMAGE_SOURCE is SD_CARD")
# If SD still not present, fall back to Dropbox when WiFi is up
try:
os.statvfs("/sd")
except Exception:
if _is_connected:
print("SD not available; falling back to DROPBOX")
IMAGE_SOURCE = "DROPBOX"
# Sync time via NTP (+ simple UK DST adjustment)
try:
import ntptime
ntptime.settime()
rtc = machine.RTC()
utc_time = rtc.datetime()
year, month, day = utc_time[0], utc_time[1], utc_time[2]
hour = utc_time[4]
if is_dst_active(year, month, day):
hour += 1
if hour >= 24:
hour -= 24
day += 1
adjusted_time = (year, month, day, utc_time[3], hour, utc_time[5], utc_time[6], 0)
rtc.datetime(adjusted_time)
_has_time = True
print("Time synchronized and adjusted for UK timezone")
except Exception as e:
_has_time = False
print("NTP sync failed:", e)
force_garbage_collection()
# ---------- MOVE IMAGE SELECTION/DECODE EARLY ----------
image_path = get_random_image()
if image_path:
print("Using image:", image_path)
else:
print("No image available")
force_garbage_collection()
# ---------- Fetch weather and calendar AFTER the image ----------
weather_data = None
week_events = {}
if _is_connected:
weather_data = get_weather_forecast()
force_garbage_collection()
week_events = get_week_agenda_ics()
force_garbage_collection()
else:
print("Skipping online data fetch (no connection)")
# Ensure today's weather is available (snapshot fallback)
try:
now = time.localtime()
today_str = "{:04d}{:02d}{:02d}".format(now[0], now[1], now[2])
if not weather_data or today_str not in weather_data:
print("Today's weather missing – attempting snapshot fallback")
snap = get_current_weather_snapshot()
if snap:
weather_data = weather_data or {}
weather_data[today_str] = snap
print("Using current weather snapshot for", today_str)
else:
print("Snapshot fallback failed")
except Exception as e:
print("Header weather fallback failed:", e)
# Moon phases
moon_phase = None
if _is_connected:
moon_phase = get_week_moon_phases_weatherapi(HOME_LON, HOME_LAT)
force_garbage_collection()
# Draw image and agenda
try:
agenda_x = draw_image(image_path, 0, 0, WIDTH, HEIGHT)
print("Image width used / agenda starts at x:", agenda_x)
except Exception as e:
print("Error drawing image:", e)
agenda_x = 10
# Generate AI or local summary
try:
available_width = WIDTH - agenda_x - 10
ai_summary = generate_ai_summary(week_events, weather_data, moon_phase, available_width)
except Exception as e:
print("Error generating summary:", e)
ai_summary = "Unable to generate summary."
# Draw agenda pane & update display
try:
draw_agenda_summary(agenda_x, ai_summary, weather_data)
display.update()
print("Display updated successfully")
except Exception as e:
print("Error updating display:", e)
try:
import sys
sys.print_exception(e)
except Exception:
pass
# Final cleanup
weather_data = None
week_events = None
moon_phase = None
force_garbage_collection()
cleanup_temp_files()
force_garbage_collection()
# Sleep scheduling
try:
sleep_minutes = calculate_sleep_time()
print(f"Entering deep sleep for {sleep_minutes} minutes")
inky_frame.sleep_for(sleep_minutes)
except Exception as e:
print("Error calculating sleep time:", e)
print("Sleeping for default 60 minutes")
inky_frame.sleep_for(60)
\\\