#Gage Condon and Dan Flores
#Campus School Project
#Matrix Portal Code

import time
import board
import busio
import displayio
import random
from adafruit_matrixportal.matrix import Matrix
import adafruit_imageload

#What are the dimensions of your LED Matrix?
matrix_width = 32 * 1
matrix_height = 32 * 1

#How fast should we flip between images?
animation_speed = 0.1 # speed between frames in fractional seconds

#create matrix display
matrix = Matrix(width=matrix_width, height=matrix_height, bit_depth=6)
display = matrix.display

def prepare_bmp(file):
    bitmap_name = file
    bitmap = displayio.OnDiskBitmap(open("/"+bitmap_name+".bmp", "rb"))
    # Below assumes you have a single horizontal strip of images
    # If you have a single vertical strip, swap out matrix_height for matrix_width
    number_of_images = bitmap.width / matrix_width
    print("number_of_images:", number_of_images)
    image_bit, image_pal = adafruit_imageload.load("/"+bitmap_name+".bmp",
                                                     bitmap=displayio.Bitmap,
                                                     palette=displayio.Palette)
    image_grid = displayio.TileGrid(image_bit, pixel_shader=image_pal,
                                     width=1, height=1,
                                     tile_height=matrix_height, tile_width=matrix_width,
                                     default_tile=0,
                                     x=0, y=0)
    # make a group of sprites (sliced up images) from the grid
    group = displayio.Group()
    group.append(image_grid)

    display.show(group)
    return image_grid, number_of_images

# time.monotonic() is an internal clock value returned in fractional seconds
time_value = 0 #  time.monotonic() holder
grid_index = 0 #  index for tilegrid

#set up connection to the CPB
cpb_connection = busio.UART(board.TX, board.RX, baudrate=9600, timeout=0)

animations = ["fishtank", "flashheart", "heart", "hello", "juggle", "smile_animation"]
current_animation = 0

while True:

    prepare_bmp("black_page")
    message = ""
    incoming_bytes = cpb_connection.in_waiting
    if incoming_bytes:
        bytes_in = cpb_connection.read(incoming_bytes)
        print("Received: ", bytes_in)
        message = bytes_in.decode()

    if message == "a":
        animation_index = random.randint(0,5)
        print("now running animation named: ", animations[animation_index])
        image_grid, number_of_images = prepare_bmp(animations[animation_index])
        t_end = time.time()+5
        while time.time() < t_end:
            #  every animation_speed seconds...
            if (time_value + animation_speed) < time.monotonic():
                #  the animation cycles
                image_grid[0] = grid_index
                #  grid_index is the tilegrid index location
                time_value = time.monotonic()
                print("Now Showing grid_index #:", grid_index)
                grid_index += 1
                #  if an animation cycle ends
                if grid_index > number_of_images-1:
                    #  index is reset
                    grid_index = 0
        grid_index = 0
        prepare_bmp("black_page")
