Code

import time
import os
import board
import busio
import pygame
import cv2 # Import OpenCV for video playback
from adafruit_neotrellis.neotrellis import NeoTrellis
from pygame.locals import FULLSCREEN

# Initialize pygame for screen display
pygame.init()
screen = pygame.display.set_mode((0, 0), FULLSCREEN) # Set full screen
clock = pygame.time.Clock()

# Get the directory where the script is located (Desktop folder)
script_dir = os.path.dirname(os.path.realpath(__file__))

# Directory paths relative to the script location
VIDEO_DIR = os.path.join(script_dir, "videos")
IMAGE_DIR = os.path.join(script_dir, "images")

# Video file names: 001.mp4, 002.mp4, ..., 010.mp4
VIDEO_FILES = [f"{str(i).zfill(3)}.mp4" for i in range(1, 11)]

# Path to warning image
WARNING_IMAGE = os.path.join(IMAGE_DIR, "paowarning.png")

# Initialize I2C bus and NeoTrellis object
i2c = busio.I2C(board.SCL, board.SDA)
trellis = NeoTrellis(i2c)

# Initialize some state variables
playing_video = None # Keeps track of the current video being played

# Button callback function
def button_callback(event):
global playing_video
if event.edge == NeoTrellis.EDGE_RISING:
video_index = event.number
video_file = os.path.join(VIDEO_DIR, VIDEO_FILES[video_index])

if os.path.exists(video_file):
print(f"Stopping current video and playing: {video_file}")
# Stop the current video if one is playing
if playing_video:
stop_video() # Stop the current video
# Play the new video
playing_video = play_video(video_file)
else:
print(f"Video file not found: {video_file}")

def play_video(video_file):
"""Function to play a video file using OpenCV"""
cap = cv2.VideoCapture(video_file)

if not cap.isOpened():
print("Error: Unable to open video file")
return None

# Get the video frame width and height
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Create a pygame surface to display video frames
video_surface = pygame.Surface((frame_width, frame_height))

# Play the video
while cap.isOpened():
ret, frame = cap.read()

if not ret:
break # Exit if video ends

# Convert OpenCV frame (BGR) to Pygame surface (RGB)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_surface = pygame.surfarray.make_surface(frame_rgb)

# Rotate the video 90 degrees clockwise
frame_surface = pygame.transform.rotate(frame_surface, -90)

# Flip the frame horizontally (left to right)
frame_surface = pygame.transform.flip(frame_surface, True, False)

# Scale the video frame to fit the screen
frame_surface = pygame.transform.scale(frame_surface, screen.get_size())

# Blit the video frame to the screen
screen.blit(frame_surface, (0, 0))
pygame.display.update() # Update the display

# Handle pygame events (e.g., quitting the video)
for event in pygame.event.get():
if event.type == pygame.QUIT:
cap.release()
pygame.quit()
return None

# Handle NeoTrellis events here as well
trellis.sync() # Ensure NeoTrellis events are processed
time.sleep(0.02) # Sleep to avoid overloading the CPU

clock.tick(30) # Limit to 30 frames per second

# Release the video capture object when done
cap.release()
return cap # Return the video capture object to track it in the global state

def stop_video():
"""Gracefully stop the current video"""
global playing_video
if playing_video:
playing_video.release() # Release the video capture object
playing_video = None # Reset the state

def show_warning_image():
"""Show the warning image in full-screen"""
warning_image = pygame.image.load(WARNING_IMAGE)
warning_image = pygame.transform.scale(warning_image, screen.get_size()) # Scale image to full screen
screen.blit(warning_image, (0, 0)) # Draw the image on the screen
pygame.display.update() # Update the display

# Initialize buttons and set up callbacks
for i in range(10):
trellis.activate_key(i, NeoTrellis.EDGE_RISING)
trellis.callbacks[i] = button_callback

# Show the warning image initially
show_warning_image()

# Initial feedback: Set all NeoTrellis LEDs to blue (inactive)
for i in range(16):
trellis.pixels[i] = (0, 0, 50)

# Main loop
print("NeoTrellis video controller ready!")

while True:
trellis.sync() # Ensure NeoTrellis events are handled
time.sleep(0.02) # Sleep to avoid overloading the CPU


pao9.py