Skip to content

code & files for nfc movie player #2919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Raspberry_Pi_NFC_Movie_Player/add_movie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import csv
import os
import sys
import board
import busio
from digitalio import DigitalInOut
from adafruit_pn532.spi import PN532_SPI

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs_pin = DigitalInOut(board.D24)
pn532 = PN532_SPI(spi, cs_pin, debug=False)

csv_file = "movies.csv"
if not os.path.isfile(csv_file):
with open(csv_file, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['uid', 'movie'])

def is_duplicate(uid, mov):
with open(csv_file, mode='r') as f:
reader = csv.DictReader(f)
for row in reader:
if row['uid'] == uid or row['movie'] == mov:
return True
return False

ic, ver, rev, support = pn532.firmware_version
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))

# Configure PN532 to communicate with MiFare cards
pn532.SAM_configuration()

print("Waiting for new RFID/NFC card...")

while True:
the_uid = pn532.read_passive_target(timeout=0.05)
if the_uid is not None:
uid_str = f"{[hex(i) for i in the_uid]}"
movie = input("Enter the name of the new movie: ")
movie = f"{movie}"
if is_duplicate(uid_str, movie):
print("UID {uid_str} or movie {movie} already exists, skipping.")
else:
with open(csv_file, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([uid_str, movie])
print(f"Added UID {uid_str} with movie title {movie} to movies.csv")
sys.exit()
Binary file added Raspberry_Pi_NFC_Movie_Player/blinka.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Raspberry_Pi_NFC_Movie_Player/movies.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid,movie
125 changes: 125 additions & 0 deletions Raspberry_Pi_NFC_Movie_Player/nfc_movie_player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import subprocess
import csv
from datetime import datetime
import os
import tkinter as tk
from PIL import Image, ImageTk
import board
import busio
from digitalio import DigitalInOut
from adafruit_pn532.spi import PN532_SPI

# ---- Update these file paths for your raspberry pi! ----
home_path = "/home/YOUR-USERNAME"
image_path = f"{home_path}/Pictures/blinka.png"
movie_path = f"{home_path}/Videos"
csv_file = "movies.csv"
# ----

if not os.path.isfile(csv_file):
with open(csv_file, mode='w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['uid', 'movie'])

root = tk.Tk()
root.attributes("-fullscreen", True)
root.configure(background="black")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry(f"{screen_width}x{screen_height}+0+0")

def toggle_fullscreen():
root.attributes("-fullscreen", not root.attributes("-fullscreen"))

def exit_fullscreen():
root.destroy()

root.bind("<F11>", toggle_fullscreen)
root.bind("<Escape>", exit_fullscreen)

image = Image.open(image_path)
photo = ImageTk.PhotoImage(image)

image_label = tk.Label(root, image=photo, bg="black")
image_label.place(x=0, y=0)

info_label = tk.Label(root, text = "00:00: AM", font=("Helvetica", 68), fg="white", bg="black")
info_label.place(x=1870, y = 1030, anchor="se")

x_pos, y_pos = 100, 100
x_speed, y_speed = 1, 1

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs_pin = DigitalInOut(board.D24)
pn532 = PN532_SPI(spi, cs_pin, debug=False)

ic, ver, rev, support = pn532.firmware_version
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))

# Configure PN532 to communicate with MiFare cards
pn532.SAM_configuration()

print("Waiting for RFID/NFC card...")

now_playing = False
# pylint: disable=global-statement
def move():
global x_pos, y_pos, x_speed, y_speed
x_pos += x_speed
y_pos += y_speed
if x_pos + photo.width() >= root.winfo_screenwidth() or x_pos <= 0:
x_speed = -x_speed
if y_pos + photo.height() >= root.winfo_screenheight() or y_pos <= 0:
y_speed = -y_speed
image_label.place(x=x_pos, y=y_pos)
root.after(30, move)

def get_movie(uid):
with open(f"{home_path}/{csv_file}", "r") as file:
reader = csv.DictReader(file)
for row in reader:
if row["uid"] == uid:
return row["movie"]
return None

def read_card():
global now_playing
uid = pn532.read_passive_target(timeout=0.05)
# Try again if no card is available.
if uid is not None:
print("Found card with UID:", [hex(i) for i in uid])
title = str([hex(i) for i in uid])
movie_name = get_movie(title)
if movie_path:
video = f"{movie_path}/{movie_name}.mp4"
subprocess.run(["sudo", "-u", "adafruit", "vlc", "--fullscreen", video], check=False)
now_playing = True
else:
current_time = datetime.now().strftime("%I:%M %p")
info_label.config(text=f"{current_time}")
root.after(2000, read_card)

def vlc_check():
global now_playing
try:
result = subprocess.check_output(["pgrep", "-x", "vlc"])
if result:
return True
else:
return False
except subprocess.CalledProcessError:
now_playing = False
return False
root.after(5000, vlc_check)

if not now_playing:
move()
read_card()
if now_playing:
vlc_check()

root.mainloop()
13 changes: 13 additions & 0 deletions Raspberry_Pi_NFC_Movie_Player/nfcmedia_systemd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=NFC Media Player
After=x-session-manager.service

[Service]
User=USERNAME
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/USERNAME/.Xauthority
ExecStartPre=/bin/sleep 10
ExecStart=sudo /home/USERNAME/blinka/bin/python /home/USERNAME/nfc_movie_player.py

[Install]
WantedBy=default.target