Hi,
I wrote a little script for the Badger 2040W to function as a birthday calendar. Basicly, it show the days untill the next persons birthday from a dictionary of people and birthdays. Something like an offline Facebook Reminder, without the need of Facebook.
The first time I run this, the code runs great. Then, I make the device sleep for 8 hours (30 seconds right now for testing purpose), then check the date again to see if a day has passed. When someone has a birthday, the screen colors are inverted, the LED turns on and the display reads āHappy Birthday!ā
import time
import machine
import badger2040
# Define the birthdays dictionary
birthdays = {
"Person 1": (1, 9),
"Person 2": (1, 20),
"Person 3": (2, 4),
}
badger = badger2040.Badger2040()
# Function to get the current date from the onboard Wi-Fi chip
def get_current_date_wifi():
display = badger2040.Badger2040()
display.set_update_speed(2)
display.set_thickness(4)
WIDTH, HEIGHT = display.get_bounds()
if badger2040.is_wireless():
import ntptime
try:
display.connect()
if display.isconnected():
ntptime.settime()
badger2040.pico_rtc_to_pcf()
except (RuntimeError, OSError) as e:
print(f"Wireless Error: {e.value}")
# Thonny overwrites the Pico RTC, so re-sync from the physical RTC if we can
try:
badger2040.pcf_to_pico_rtc()
except RuntimeError:
pass
rtc = machine.RTC()
return list(rtc.datetime())[1:3]
# Function to calculate days in a month
def days_in_month(month, year):
if month == 2 and ((year % 4 == 0 and year % 100 != 0) or year % 400 == 0):
return 29
return (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[month - 1]
# Function to display birthday message
def display_birthday_message(display, next_birthday_person, days_until_birthday):
display.clear() # White background
display.set_pen(15) # Black text
display.set_font("bitmap8")
text_width = display.measure_text(next_birthday_person, scale=6)
display.text(next_birthday_person, (296 - text_width) // 2, 30, scale=6)
if days_until_birthday == 0:
# Display "Gelukkige verjaardag!" centered and larger
birthday_message = "Happy Birthday"
badger.led(255)
text_width = display.measure_text(birthday_message, scale=3)
display.text(birthday_message, (296 - text_width) // 2, 95, scale=3)
else:
# Display remaining days larger and centered
days_text = f"Still {days_until_birthday} days"
badger.led(0)
text_width = display.measure_text(days_text, scale=4)
display.text(days_text, (296 - text_width) // 2, 95, scale=4) # Updated height to 95
display.update()
time.sleep(5) # Display the message for 5 seconds
# Display Setup
display = badger2040.Badger2040()
# Run the script every 8 hours
while True:
current_month, current_day = get_current_date_wifi()
next_birthday_person = None
days_until_birthday = float('inf')
for person, birthday in birthdays.items():
birthday_month, birthday_day = birthday
current_days = current_day + sum(days_in_month(m, 2023) for m in range(1, current_month))
birthday_days = birthday_day + sum(days_in_month(m, 2023) for m in range(1, birthday_month))
days_until = birthday_days - current_days
if days_until < 0:
# Adjust for the next year
days_until += sum(days_in_month(m, 2023) for m in range(current_month, 13))
if days_until < days_until_birthday:
days_until_birthday = days_until
next_birthday_person = person
display.clear()
display.set_pen(15) # Black text
display.set_font("bitmap8")
text_width = display.measure_text(next_birthday_person, scale=6)
display.text(next_birthday_person, (296 - text_width) // 2, 30, scale=6)
days_text = f"Still {days_until_birthday} days"
badger.led(0)
text_width = display.measure_text(days_text, scale=4)
display.text(days_text, (296 - text_width) // 2, 95, scale=4)
display.update()
if days_until_birthday == 0:
display_birthday_message(display, next_birthday_person, days_until_birthday)
time.sleep(30)
The second time this code runs, it shows me a white display. I get the āconnectedā promt and my IP adress, then white. Is this a pen color/background color issue? Is my code incorrect?