# Libraries Import
from machine import Pin, I2C
from time import sleep_ms
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# LCD Screen Configuration
LCD_I2C_ADDR = 39 # I2C address
LCD_NUM_ROWS = 2 # number of rows
LCD_NUM_COLS = 16 # number of columns
i2c = I2C(1, sda=Pin(2, Pin.OUT), scl=Pin(3, Pin.OUT), freq=400000)
lcd = I2cLcd(i2c, LCD_I2C_ADDR, LCD_NUM_ROWS, LCD_NUM_COLS)
# Button Inputs Configuration
BUTTON_1 = Pin(10, Pin.IN, Pin.PULL_UP)
BUTTON_2 = Pin(9, Pin.IN, Pin.PULL_UP)
BUTTON_3 = Pin(8, Pin.IN, Pin.PULL_UP)
# Current scrolling process flag
scrolling_process = None
# Function Definitions
def clear_lcd():
lcd.clear()
print("LCD screen cleared")
def scroll_text_line_right_to_left(text):
global scrolling_process
scrolling_process = "button 1"
text_list = list(text)
virtual_char_count = LCD_NUM_COLS + 2 * len(text_list) - 1
screen_list = [""] * LCD_NUM_COLS
text_list_reversed = text_list[::-1]
print("Scrolling text line from right to left")
clear_lcd()
for j in range(virtual_char_count - len(text_list)):
if scrolling_process != "button 1":
return
temp_list = [""] * virtual_char_count
for i in range(len(text_list)):
temp_list[virtual_char_count - i - 1 - j] = text_list_reversed[i]
for k in range(LCD_NUM_COLS):
screen_list[k] = temp_list[k + len(text_list)]
lcd.move_to(k, 0)
lcd.putstr(screen_list[k])
sleep_ms(100)
clear_lcd()
def scroll_text_line_left_to_right(text):
global scrolling_process
scrolling_process = "button 2"
text_list = list(text)
virtual_char_count = LCD_NUM_COLS + 2 * len(text_list) - 1
screen_list = [""] * LCD_NUM_COLS
print("Scrolling text line from left to right")
clear_lcd()
for j in range(virtual_char_count - len(text_list)):
if scrolling_process != "button 2":
return
temp_list = [""] * virtual_char_count
for i in range(len(text_list)):
temp_list[i + j + 1] = text_list[i]
for k in range(LCD_NUM_COLS):
screen_list[k] = temp_list[k + len(text_list)]
lcd.move_to(k, 1)
lcd.putstr(screen_list[k])
sleep_ms(100)
clear_lcd()
def scroll_text_two_lines(text):
global scrolling_process
scrolling_process = "button 3"
print("Scrolling text across two lines")
text_list = list(text)
virtual_char_count = LCD_NUM_COLS + 2 * len(text_list) - 1
screen_list_line1 = [""] * LCD_NUM_COLS
screen_list_line2 = [""] * LCD_NUM_COLS
text_list_reversed = text_list[::-1]
clear_lcd()
for j in range(virtual_char_count - len(text_list)):
if scrolling_process != "button 3":
return
temp_list_line1 = [""] * virtual_char_count
temp_list_line2 = [""] * virtual_char_count
for i in range(len(text_list)):
temp_list_line1[virtual_char_count - i - 1 - j] = text_list_reversed[i]
temp_list_line2[i + j + 1] = text_list[i]
for k in range(LCD_NUM_COLS):
screen_list_line1[k] = temp_list_line1[k + len(text_list)]
lcd.move_to(k, 0)
lcd.putstr(screen_list_line1[k])
screen_list_line2[k] = temp_list_line2[k + len(text_list)]
lcd.move_to(k, 1)
lcd.putstr(screen_list_line2[k])
sleep_ms(500)
clear_lcd()
# Main Program Loop
while True:
if not BUTTON_1.value():
print("Button 1 pressed")
scrolling_process = None
text = input("Enter text to scroll: ")
scroll_text_line_right_to_left(text)
if not BUTTON_2.value():
print("Button 2 pressed")
scrolling_process = None
text = input("Enter text to scroll: ")
scroll_text_line_left_to_right(text)
if not BUTTON_3.value():
print("Button 3 pressed")
scrolling_process = None
text = input("Enter text to scroll: ")
scroll_text_two_lines(text)