|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import subprocess |
| 6 | +import csv |
| 7 | +from datetime import datetime |
| 8 | +import os |
| 9 | +import tkinter as tk |
| 10 | +from PIL import Image, ImageTk |
| 11 | +import board |
| 12 | +import busio |
| 13 | +from digitalio import DigitalInOut |
| 14 | +from adafruit_pn532.spi import PN532_SPI |
| 15 | + |
| 16 | +# ---- Update these file paths for your raspberry pi! ---- |
| 17 | +home_path = "/home/YOUR-USERNAME" |
| 18 | +image_path = f"{home_path}/Pictures/blinka.png" |
| 19 | +movie_path = f"{home_path}/Videos" |
| 20 | +csv_file = "movies.csv" |
| 21 | +# ---- |
| 22 | + |
| 23 | +if not os.path.isfile(csv_file): |
| 24 | + with open(csv_file, mode='w', newline='') as f: |
| 25 | + writer = csv.writer(f) |
| 26 | + writer.writerow(['uid', 'movie']) |
| 27 | + |
| 28 | +root = tk.Tk() |
| 29 | +root.attributes("-fullscreen", True) |
| 30 | +root.configure(background="black") |
| 31 | +screen_width = root.winfo_screenwidth() |
| 32 | +screen_height = root.winfo_screenheight() |
| 33 | +root.geometry(f"{screen_width}x{screen_height}+0+0") |
| 34 | + |
| 35 | +def toggle_fullscreen(): |
| 36 | + root.attributes("-fullscreen", not root.attributes("-fullscreen")) |
| 37 | + |
| 38 | +def exit_fullscreen(): |
| 39 | + root.destroy() |
| 40 | + |
| 41 | +root.bind("<F11>", toggle_fullscreen) |
| 42 | +root.bind("<Escape>", exit_fullscreen) |
| 43 | + |
| 44 | +image = Image.open(image_path) |
| 45 | +photo = ImageTk.PhotoImage(image) |
| 46 | + |
| 47 | +image_label = tk.Label(root, image=photo, bg="black") |
| 48 | +image_label.place(x=0, y=0) |
| 49 | + |
| 50 | +info_label = tk.Label(root, text = "00:00: AM", font=("Helvetica", 68), fg="white", bg="black") |
| 51 | +info_label.place(x=1870, y = 1030, anchor="se") |
| 52 | + |
| 53 | +x_pos, y_pos = 100, 100 |
| 54 | +x_speed, y_speed = 1, 1 |
| 55 | + |
| 56 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 57 | +cs_pin = DigitalInOut(board.D24) |
| 58 | +pn532 = PN532_SPI(spi, cs_pin, debug=False) |
| 59 | + |
| 60 | +ic, ver, rev, support = pn532.firmware_version |
| 61 | +print("Found PN532 with firmware version: {0}.{1}".format(ver, rev)) |
| 62 | + |
| 63 | +# Configure PN532 to communicate with MiFare cards |
| 64 | +pn532.SAM_configuration() |
| 65 | + |
| 66 | +print("Waiting for RFID/NFC card...") |
| 67 | + |
| 68 | +now_playing = False |
| 69 | +# pylint: disable=global-statement |
| 70 | +def move(): |
| 71 | + global x_pos, y_pos, x_speed, y_speed |
| 72 | + x_pos += x_speed |
| 73 | + y_pos += y_speed |
| 74 | + if x_pos + photo.width() >= root.winfo_screenwidth() or x_pos <= 0: |
| 75 | + x_speed = -x_speed |
| 76 | + if y_pos + photo.height() >= root.winfo_screenheight() or y_pos <= 0: |
| 77 | + y_speed = -y_speed |
| 78 | + image_label.place(x=x_pos, y=y_pos) |
| 79 | + root.after(30, move) |
| 80 | + |
| 81 | +def get_movie(uid): |
| 82 | + with open(f"{home_path}/{csv_file}", "r") as file: |
| 83 | + reader = csv.DictReader(file) |
| 84 | + for row in reader: |
| 85 | + if row["uid"] == uid: |
| 86 | + return row["movie"] |
| 87 | + return None |
| 88 | + |
| 89 | +def read_card(): |
| 90 | + global now_playing |
| 91 | + uid = pn532.read_passive_target(timeout=0.05) |
| 92 | + # Try again if no card is available. |
| 93 | + if uid is not None: |
| 94 | + print("Found card with UID:", [hex(i) for i in uid]) |
| 95 | + title = str([hex(i) for i in uid]) |
| 96 | + movie_name = get_movie(title) |
| 97 | + if movie_path: |
| 98 | + video = f"{movie_path}/{movie_name}.mp4" |
| 99 | + subprocess.run(["sudo", "-u", "adafruit", "vlc", "--fullscreen", video], check=False) |
| 100 | + now_playing = True |
| 101 | + else: |
| 102 | + current_time = datetime.now().strftime("%I:%M %p") |
| 103 | + info_label.config(text=f"{current_time}") |
| 104 | + root.after(2000, read_card) |
| 105 | + |
| 106 | +def vlc_check(): |
| 107 | + global now_playing |
| 108 | + try: |
| 109 | + result = subprocess.check_output(["pgrep", "-x", "vlc"]) |
| 110 | + if result: |
| 111 | + return True |
| 112 | + else: |
| 113 | + return False |
| 114 | + except subprocess.CalledProcessError: |
| 115 | + now_playing = False |
| 116 | + return False |
| 117 | + root.after(5000, vlc_check) |
| 118 | + |
| 119 | +if not now_playing: |
| 120 | + move() |
| 121 | + read_card() |
| 122 | +if now_playing: |
| 123 | + vlc_check() |
| 124 | + |
| 125 | +root.mainloop() |
0 commit comments