I will paste the code below. I am aware that there are likely a lot of mistakes or extra code that probably does nothing, but I am still a beginner and it mostly works as a I need it to so far.
Any advice or tips are appreciated to help me improve š
import time
import machine
from picographics import PicoGraphics, DISPLAY_INKY_PACK
# Buttons
button_a = machine.Pin(12, machine.Pin.IN, pull=machine.Pin.PULL_UP)
button_b = machine.Pin(13, machine.Pin.IN, pull=machine.Pin.PULL_UP)
button_c = machine.Pin(14, machine.Pin.IN, pull=machine.Pin.PULL_UP)
# Display
graphics = PicoGraphics(DISPLAY_INKY_PACK)
WIDTH, HEIGHT = graphics.get_bounds()
graphics.set_update_speed(3)
graphics.set_font("serif")
# RTC
rtc = machine.RTC()
set_clock = False
stopwatch_running = False # Flag for stopwatch state
stopwatch_seconds = 0
stopwatch_minutes = 0
stopwatch_hours = 0
# Timer for stopwatch updates
timer = machine.Timer()
# Managing stopwatch update timing
def update_stopwatch(timer):
global stopwatch_seconds, stopwatch_minutes
if stopwatch_running:
# graphics.clear() - just makes the whole display go black constantly
stopwatch_seconds += 1
if stopwatch_seconds == 60:
stopwatch_seconds = 0
stopwatch_minutes += 1
draw_stopwatch() # Update display to reflect stopwatch changes
# Button handling function
def button(pin):
global stopwatch_running, stopwatch_seconds, stopwatch_minutes
time.sleep(0.01)
if pin.value():
return
if pin == button_a:
if stopwatch_running:
stopwatch_running = False # Stop the stopwatch
stopwatch_seconds = 0 # Reset counter
stopwatch_minutes = 0
stopwatch_hours = 0
else:
stopwatch_running = True # Start the stopwatch
draw_clock()
draw_clock()
button_a.irq(trigger=machine.Pin.IRQ_FALLING, handler=button)
# Main clock display draw function
def draw_clock():
hms = "{:02}:{:02}:{:02}".format(hour, minute, second)
hms_width = graphics.measure_text(hms, 1.8)
hms_offset = int((WIDTH / 2) - (hms_width / 2))
h_width = graphics.measure_text(hms[0:2], 1.8)
mi_width = graphics.measure_text(hms[3:5], 1.8)
mi_offset = graphics.measure_text(hms[0:3], 1.8)
graphics.set_pen(15)
graphics.clear()
graphics.set_pen(0)
# No "thickness" setting in PG so, uh, fake it!
graphics.text(hms, hms_offset, 40, scale=1.8)
graphics.text(hms, hms_offset, 41, scale=1.8)
graphics.text(hms, hms_offset + 1, 40, scale=1.8)
graphics.text(hms, hms_offset - 1, 40, scale=1.8)
graphics.update()
# Main stopwatch display draw function
def draw_stopwatch():
stopwatch_text = f"{stopwatch_hours:02}:{stopwatch_minutes:02}:{stopwatch_seconds:02}"
stopwatch_width = graphics.measure_text(stopwatch_text, 1.0)
stopwatch_offset = int((WIDTH / 2) - (stopwatch_width / 2))
# graphics.clear_area(stopwatch_offset, 100, stopwatch_width, 40) - just refreshes nothing in the stopwatch space
graphics.text(stopwatch_text, stopwatch_offset, 100, scale=1.0)
graphics.text(stopwatch_text, stopwatch_offset, 101, scale=1.0)
graphics.update()
year, month, day, wd, hour, minute, second, _ = rtc.datetime()
last_second = second
# Start the timer with a period of 1 second
timer.init(period=1000, mode=machine.Timer.PERIODIC, callback=update_stopwatch)
while True:
if not set_clock:
# Get current time only for clock display, not stopwatch
if not stopwatch_running:
year, month, day, wd, hour, minute, second, _ = rtc.datetime()
if second != last_second:
draw_clock()
last_second = second
time.sleep(0.01)