import board
import time
import busio
import digitalio
import lcd
import i2c_pcf8574_interface
# Initialize I2C and LCD
i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
address = 0x27
i2c = i2c_pcf8574_interface.I2CPCF8574Interface(i2c, address)
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 messages for each line
line1_message = "Line 1 Scrolling Right"
line2_message = "Line 2 Scrolling Left"
# Add spaces before and after to create proper smooth scrolling effect
padding = " " * 16 # Create 16 spaces for padding (width of LCD)
line1_padded = padding + line1_message + padding # Add padding to both sides of message
line2_padded = padding + line2_message + padding
# Get total length for scrolling calculations
line1_length = len(line1_padded)
line2_length = len(line2_padded)
# Initialize position counters - these track the current "window" position in the padded strings
line1_pos = 0
line2_pos = 0
while True:
# Clear display for new content
display.clear()
# Display current window of line 1 (scrolling right)
display.set_cursor_pos(0, 0) # Set cursor to first line
# Extract 16 characters from the current position for line 1
display.print(line1_padded[line1_pos:line1_pos+16]) # Show current 16-char window
# Display current window of line 2 (scrolling left)
display.set_cursor_pos(1, 0) # Set cursor to second line
# Extract 16 characters from the current position for line 2
display.print(line2_padded[line2_pos:line2_pos+16]) # Show current 16-char window
# Update positions for next iteration with opposite directions
line1_pos = (line1_pos - 1) % (line1_length - 16) #Scroll right by decrementing position
line2_pos = (line2_pos + 1) % (line2_length - 16) #Scroll left by incrementing 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
# Delay for smooth scrolling
time.sleep(0.2)