import time
import board
import busio
import microcontroller
import lcd
import i2c_pcf8574_interface
# I2C setup for Raspberry Pi Pico
i2c = busio.I2C(scl=board.GP1, sda=board.GP0)
address = 0x27
i2c = i2c_pcf8574_interface.I2CPCF8574Interface(i2c, address)
#LCD setup
display = lcd.LCD(i2c, num_rows=2, num_cols=16)
display.set_backlight(True)
display.set_display_enabled(True)
# clear display initially
display.clear()
# define message for each line
line1_message = "Pico ON !!!"
# add spaces before and after to create proper smooth scrolling effect
padding = " " * 16 # create 16 spaces for padding
line1_padded = padding + line1_message + padding
# get total length for scrolling calculation
line1_length = len(line1_padded)
# initialze position counter - these track the current "window" position in the padded strings
line1_pos = 0
prev_sec = -1
while True:
display.set_cursor_pos(0, 0)
display.set_cursor_pos(0, 0) # display current window of line 1(scrolling right)
display.print(line1_padded[line1_pos:line1_pos+16]) # extract 16 character from the current position for line 1
display.set_cursor_pos(1, 0) # display current window of line 2(scrolling left)
# update position for next iteration with opposite direction
line1_pos = (line1_pos - 1) % (line1_length - 16) # scroll right by decrementing position
# critical: handle negative index for line1_pos
if line1_pos < 0:
line1_pos = line1_length - 17 # wrap around to end of string when position becomes negative
# subtract 17 instead of 16 to account for the next decrement
now = int(time.monotonic()) # Ambil total saat sejak Pico ON
# --- PENGIRAAN TIMER ---
minutes = now // 60
seconds = now % 60
display.set_cursor_pos(1, 0)
# Jika saat genap, papar timer. Jika ganjil, kosongkan (Blinking)
if now % 2 == 0:
display.print("Timer: {:02d}:{:02d}".format(minutes, seconds))
else:
display.print(" ") # 16 ruang kosong
# delay for smooth scrolling
time.sleep(0.2)