import machine
import utime
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# Constants for the I2C and LCD
I2C_ADDR = 0x27
I2C_NUM_ROWS = 4 # Number of rows in the LCD
I2C_NUM_COLS = 20 # Number of columns in the LCD
# Initialize I2C and LCD
i2c = machine.I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
led_pin = machine.Pin(27, machine.Pin.OUT)
button_pin = machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_DOWN)
buzzer_pin = machine.Pin(28, machine.Pin.OUT)
buses = [
{"name": "InterBair 5 -", "time": 8},
{"name": "InterBair 3 -", "time": 7},
{"name": "InterBair 2 -", "time": 13},
{"name": "Inter 2 -", "time": 10},
{"name": "Pinherinho -", "time": 12},
{"name": "Savoia -", "time": 15}
]
current_index = 0
def debounce(pin):
"""Debounce function to ensure stable button state."""
stable_state = pin.value()
utime.sleep_ms(20) # Wait for 20 milliseconds
return pin.value() == stable_state
def scroll_text(lcd, text, row, delay=0.3):
"""Scroll text horizontally across the LCD."""
if len(text) <= I2C_NUM_COLS:
lcd.move_to(0, row)
lcd.putstr(text)
return
for i in range(len(text) - I2C_NUM_COLS + 1):
lcd.move_to(0, row)
lcd.putstr(text[i:i + I2C_NUM_COLS])
utime.sleep(delay)
def update_display():
global current_index
lcd.clear()
for i in range(min(I2C_NUM_ROWS, len(buses))):
bus = buses[(current_index + i) % len(buses)]
bus_info = f"{bus['name']} {bus['time']} min"
scroll_text(lcd, bus_info, i)
current_index = (current_index + 1) % len(buses)
def handle_button_press(pin):
if debounce(pin):
led_pin.value(1)
buzzer_pin.value(1)
utime.sleep(0.2) # Turn on the buzzer for 200 milliseconds
led_pin.value(0)
buzzer_pin.value(0)
def decrease_times():
for bus in buses:
if bus['time'] > 0:
bus['time'] -= 1
# Set up button press interrupt
button_pin.irq(trigger=machine.Pin.IRQ_RISING, handler=handle_button_press)
# Main loop
lcd.putstr("Initializing...")
last_update_time = utime.time()
while True:
current_time = utime.time()
if current_time - last_update_time >= 1: # Update every second
decrease_times()
update_display()
last_update_time = current_time
utime.sleep(0.1) # Sleep for a short duration to allow other processes to run