-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfc.py
More file actions
107 lines (93 loc) · 3.73 KB
/
nfc.py
File metadata and controls
107 lines (93 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import threading
import time
class NFC:
READ = 0
WRITE_TRACK = 1
WRITE_PLAYLIST = 2
WRITE = 3
def __init__(self, player, buttons):
self.reader = SimpleMFRC522()
self.player = player
self.buttons = buttons
self.mode = NFC.READ
self.clear_payload = None
self.payload = None
self.write_thread_running = False
if self.player is not None:
self.thread = threading.Thread(target=self.watchdog)
self.thread.start()
def watchdog(self):
try:
while self.player.alive:
if self.clear_payload is not None:
if time.time() > self.clear_payload:
self.payload = None
self.clear_payload = None
if self.mode == NFC.READ:
id, text = self.reader.read_no_block()
if (id != None):
if self.payload is not None and text.strip(
) == self.payload.strip():
continue
splits = text.split()
if len(splits) >= 2:
hashRequest = splits[1]
currentTrack = self.player.currentTrack
if splits[
0] == "t" and currentTrack is not None and hashRequest == str(
currentTrack.hash):
print("requested track already playing", text)
continue
if splits[
0] == "p" and self.player.currentPlaylist is not None and hashRequest == str(
self.player.currentPlaylist.hash):
print("requested playlist already playing",
text)
continue
self.player.request(text)
if self.mode == NFC.WRITE:
if self.payload == None:
continue
id, text = self.reader.write_no_block(self.payload)
if id is not None:
print("nfc written")
self.mode = NFC.READ
finally:
GPIO.cleanup()
def write(self, payload):
if not self.write_thread_running:
print("asking to write:", payload)
self.payload = payload
self.mode = NFC.WRITE
self.thread = threading.Thread(target=self.write_mode_timeout)
self.thread.start()
def write_mode_timeout(self, timeout=5):
self.write_thread_running = True
t0 = time.time()
but_id = 0
for b in self.buttons[:-1]:
b.on()
while self.player.alive and self.mode != NFC.READ and time.time(
) - t0 < timeout:
self.buttons[(but_id - 1) % len(self.buttons)].on()
self.buttons[but_id % len(self.buttons)].off()
time.sleep(0.2)
but_id += 1
for b in self.buttons:
b.off()
self.mode = NFC.READ
self.write_thread_running = False
self.clear_payload = time.time() + 5 # clear payload in 5 secs
if __name__ == "__main__":
class DummyPlayer:
alive = True
def request(self, path):
print("requesting:", path)
import time
nfc = NFC(DummyPlayer())
print("waiting")
while True:
time.sleep(0.1)