# ADAMSON UNIVERSITY - COMPUTER SCIENCE
# ROBOT HARDWARE FUNDAMENTALS - LED Project
# RAMOS, DON FRANCO A. 03/17/2022

# modules
from machine import Pin
import time

# variables that define the amount of space between letters
# and the dimension of the LED matrix
lines = '--'
dim = 5

# Letter maps for LEDs
F = f'11110{lines}10000{lines}11100{lines}10000{lines}10000'
R = f'11110{lines}10001{lines}11110{lines}10010{lines}10001'
A = f'01110{lines}10001{lines}11111{lines}10001{lines}10001'
N = f'10001{lines}11001{lines}10101{lines}10011{lines}10001'
C = f'01110{lines}10001{lines}10000{lines}10001{lines}01110'
O = f'01110{lines}10001{lines}10001{lines}10001{lines}01110'

# list of letter maps
name = list(map(lambda l: l.split(f'{lines}'), [F, R, A, N, C, O]))

# transpose 2D list of letter maps
def arrangeRows(arr):
    curr = 0
    arranged_rows = []
    row = []

    for h in range(len(arr)):
        for i in range(len(arr)):
            for j in range(len(arr[i])):
                if (j == curr):
                    row.append(arr[i][j])
            if (i == len(arr) - 1):
                arranged_rows.append(f'{lines}'.join(row))
                row = []
                curr = curr + 1

    return list(filter(None, arranged_rows))

# pop index 0 and add move it to the end of the list to create movement
def shiftLeft(arr):
    return list(map(lambda s: s[1:] + s[0], arr))

# LEDs go from 0-22, 26, 27 but list is 2D.
# get 1D index from 2D list to correctly turn on/off LEDs
def showLights(arr):
    for i in range(len(arr)):
        for j in range(dim):
            index = (i*dim)+j
            if (index >= 23):
                index += 3
            Pin(index, Pin.OUT).value(0 if (arr[i][j] == '-') else int(arr[i][j]))

# starting variables
start = True
starting_arr = [s + f'{lines}' for s in arrangeRows(name)]
final_arr = list()

# infinite loop to run the LEDs forever until terminated via the simulator
while True:
    if (start):
        showLights(starting_arr)
        final_arr = shiftLeft(starting_arr)
        start = False
    else:
        final_arr = shiftLeft(final_arr)
        showLights(final_arr)
    time.sleep(.3)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT