Skip to content

Commit 9b85a43

Browse files
authored
Merge pull request #2919 from adafruit/nfc_media
code & files for nfc movie player
2 parents 57f5b16 + 60ecc7e commit 9b85a43

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import csv
6+
import os
7+
import sys
8+
import board
9+
import busio
10+
from digitalio import DigitalInOut
11+
from adafruit_pn532.spi import PN532_SPI
12+
13+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
14+
cs_pin = DigitalInOut(board.D24)
15+
pn532 = PN532_SPI(spi, cs_pin, debug=False)
16+
17+
csv_file = "movies.csv"
18+
if not os.path.isfile(csv_file):
19+
with open(csv_file, mode='w', newline='') as file:
20+
writer = csv.writer(file)
21+
writer.writerow(['uid', 'movie'])
22+
23+
def is_duplicate(uid, mov):
24+
with open(csv_file, mode='r') as f:
25+
reader = csv.DictReader(f)
26+
for row in reader:
27+
if row['uid'] == uid or row['movie'] == mov:
28+
return True
29+
return False
30+
31+
ic, ver, rev, support = pn532.firmware_version
32+
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
33+
34+
# Configure PN532 to communicate with MiFare cards
35+
pn532.SAM_configuration()
36+
37+
print("Waiting for new RFID/NFC card...")
38+
39+
while True:
40+
the_uid = pn532.read_passive_target(timeout=0.05)
41+
if the_uid is not None:
42+
uid_str = f"{[hex(i) for i in the_uid]}"
43+
movie = input("Enter the name of the new movie: ")
44+
movie = f"{movie}"
45+
if is_duplicate(uid_str, movie):
46+
print("UID {uid_str} or movie {movie} already exists, skipping.")
47+
else:
48+
with open(csv_file, mode='a', newline='') as file:
49+
writer = csv.writer(file)
50+
writer.writerow([uid_str, movie])
51+
print(f"Added UID {uid_str} with movie title {movie} to movies.csv")
52+
sys.exit()
54.1 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid,movie
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=NFC Media Player
3+
After=x-session-manager.service
4+
5+
[Service]
6+
User=USERNAME
7+
Environment=DISPLAY=:0
8+
Environment=XAUTHORITY=/home/USERNAME/.Xauthority
9+
ExecStartPre=/bin/sleep 10
10+
ExecStart=sudo /home/USERNAME/blinka/bin/python /home/USERNAME/nfc_movie_player.py
11+
12+
[Install]
13+
WantedBy=default.target

0 commit comments

Comments
 (0)