from machine import Pin, I2C
import utime
import ssd1306
import framebuf
# I2C pins for Pico
sda = Pin(0)
scl = Pin(1)
i2c = I2C(0, sda=sda, scl=scl, freq=400000)
display = ssd1306.SSD1306_I2C(128, 64, i2c, 0x3C)
# Button setup
push_button = Pin(14, Pin.IN, Pin.PULL_DOWN)
# Red LED setup
redled = Pin(15, Pin.OUT)
def draw_pill_image(x, y, width, height): #This prints a image of a "pill" if not necessary can be discarded
"""Draws a pill-like shape."""
buffer = bytearray(width * height // 8)
frame = framebuf.FrameBuffer(buffer, width, height, framebuf.MONO_HLSB)
frame.hline(2, 0, width - 4, 1)
frame.hline(2, height - 1, width - 4, 1)
frame.vline(0, 2, height - 4, 1)
frame.vline(width - 1, 2, height - 4, 1)
frame.pixel(1, 1, 1)
frame.pixel(1, 2, 1)
frame.pixel(1, height - 3, 1)
frame.pixel(1, height - 2, 1)
frame.pixel(width - 2, 1, 1)
frame.pixel(width - 2, 2, 1)
frame.pixel(width - 2, height - 3, 1)
frame.pixel(width - 2, height - 2, 1)
frame.vline(width // 2, 0, height, 1)
display.blit(frame, x, y)
def display_pill_reminder():
"""Displays the pill reminder message and image, blinks red LED."""
display.fill(0)
display.text("Medication time!", 1, 0, 1)
draw_pill_image(40, 20, 48, 24)
display.show()
led_state = 0
blink_start = utime.ticks_ms()
blink_interval = 500 # Adjust blink speed
while push_button.value() == 0: # Wait for button press
now = utime.ticks_ms()
if utime.ticks_diff(now, blink_start) >= blink_interval:
redled.value(led_state)
led_state = 1 - led_state # Toggle LED
blink_start = now
utime.sleep_ms(10) # check the button every 10ms
redled.off() # turn off the led once the button is pressed.
def display_countdown(remaining_time):
"""Displays the countdown timer with text."""
hours = remaining_time // 3600
minutes = (remaining_time % 3600) // 60
seconds = remaining_time % 60
time_str = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
display.fill(0)
display.text("*PILL PUSHER*", (128 - len("*PILL PUSHER*") * 8) // 2, 0, 1)
display.text("Next pill in:", 0, 16, 1)
display.text(time_str, (128 - len(time_str) * 8) // 2, 32, 1)
display.show()
# Main loop
countdown_seconds = 10 # Set countdown to 5 seconds for testing
while True:
if countdown_seconds <= 0:
display_pill_reminder()
countdown_seconds = 10 # Reset countdown
else:
display_countdown(countdown_seconds)
countdown_seconds -= 1
utime.sleep(1)