import time
import board
import displayio
import terminalio
from adafruit_display_text import label
from adafruit_matrixportal.matrix import Matrix
import busio

matrix = Matrix(width=64, height=32, bit_depth=4)
display = matrix.display

group = displayio.Group()
display.root_group = group

font = terminalio.FONT

text_color = 0xFFFFFF
screen = label.Label(font, text="It's profit time", color=text_color)

screen.anchor_point = (0, 0.5)

screen.anchored_position = (display.width, display.height // 2)
group.append(screen)

uart = busio.UART(board.TX, board.RX, baudrate=115200, receiver_buffer_size=128)

scroll_speed = -1

def display_text(text):
    print(f"Displaying: {text}")
    screen.text = text + "  "

while True:
    try:
        if uart.in_waiting > 0:
            received_bytes = uart.readline()
            if received_bytes:
                received_text = received_bytes.decode('utf-8').strip()
                print(received_text)
                display_text(received_text)

        screen.x += -1
        if screen.x + screen.bounding_box[2] < 0:
            screen.x = display.width
        time.sleep(0.05)

    except Exception as e:
        print(f"General error: {e}")
        time.sleep(1)


