-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
187 lines (145 loc) · 4.4 KB
/
scanner.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import time
import board
import busio
import os
import threading
from adafruit_pn532.i2c import PN532_I2C
import neopixel
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
import pygame._sdl2.audio as sdl2_audio
VALID_UID = "04558c92ec5b80"
AUDIO_ENABLED = True
# Global flag to indicate whether the RFID board is found
RFID_FOUND = threading.Event()
def initialize_audio():
try:
pygame.mixer.pre_init(44100, -16, 1, 512)
pygame.mixer.get_init()
pygame.mixer.init()
devices = tuple(sdl2_audio.get_audio_device_names(False))
print(devices)
device = devices[0]
pygame.mixer.init(devicename=device)
except Exception as error:
print("No Audio")
print(error)
return False
return True
def play_sound():
if AUDIO_ENABLED:
pygame.mixer.music.set_volume(0.25)
pygame.mixer.music.load("magicband_fastpass.mp3")
print(pygame.mixer.music.get_volume())
pygame.mixer.music.play()
def swirl_effect(pixels, cycles=12):
num_pixels = len(pixels)
for _ in range(cycles):
for i in range(num_pixels - 1, -1, -1):
pixels[i] = (255, 255, 255) # Full brightness for the current pixel
# Calculate the indices of the neighbors
left_neighbor = (i - 1) % num_pixels
right_neighbor = (i + 1) % num_pixels
l2 = (i - 2) % num_pixels
r2 = (i + 2) % num_pixels
# Set the brightness of the neighbors to 50%
pixels[left_neighbor] = (127, 127, 127)
pixels[right_neighbor] = (127, 127, 127)
pixels[l2] = (127, 127, 127)
pixels[r2] = (127, 127, 127)
pixels.show()
if _ < 3:
time.sleep(0.03)
else:
time.sleep(0.03 * (1 / (_ - 1)))
# Reset the pixels to 0 brightness
pixels[i] = (0, 0, 0)
pixels[left_neighbor] = (0, 0, 0)
pixels[right_neighbor] = (0, 0, 0)
pixels[l2] = (0, 0, 0)
pixels[r2] = (0, 0, 0)
def brightness_transition(pixels, target_brightness, duration):
start_brightness = pixels.brightness
steps = int(duration / 0.1) # 0.1 seconds per step
brightness_step = (target_brightness - start_brightness) / steps
for _ in range(steps):
start_brightness += brightness_step
pixels.brightness = start_brightness
pixels.show()
time.sleep(0.1)
def turn_off_pixels(pixels):
num_pixels = len(pixels)
for i in range(num_pixels):
pixels[num_pixels - i - 1] = (0, 0, 0)
pixels.show()
time.sleep(0.05)
def pulsing_blue_thread(pixels):
pixels.fill((0, 0, 255))
pixels.brightness = 255
pixels.show()
while not RFID_FOUND.is_set():
brightness_transition(pixels, 0.05, 1)
time.sleep(1/100)
brightness_transition(pixels, 0.75, 1)
time.sleep(1/100)
pixels.brightness = 255
pixels.fill((0,255,0))
pixels.show()
time.sleep(1)
pixels.fill((0, 0, 0)) # Turn off pixels after pulsing
pixels.show()
def read_rfid():
i2c = busio.I2C(board.SCL, board.SDA)
pn532 = None
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(
board.D18, 22, brightness=1, auto_write=False, pixel_order=ORDER
)
pulsing_thread = threading.Thread(target=pulsing_blue_thread, args=(pixels,))
pulsing_thread.start()
while pn532 is None:
try:
pn532 = PN532_I2C(i2c, address=0x24)
ic, ver, rev, support = pn532.firmware_version
print(f"Found PN532 with firmware version: {ver}.{rev}")
# Set the flag to stop the pulsing thread
RFID_FOUND.set()
pulsing_thread.join() # Wait for the pulsing thread to finish
except RuntimeError as e:
print(f"Error initializing PN532: {e}")
print("Retrying initialization...")
time.sleep(2)
pn532.SAM_configuration()
print("Waiting for an RFID card...")
while True:
uid = pn532.read_passive_target(timeout=0.5)
if uid is not None:
print("Card detected with UID:", [hex(i) for i in uid])
print(uid)
print(uid.hex())
print("swirling lights")
swirl_effect(pixels)
# Turn all lights solid green
print("lights green")
pixels.brightness = 0.01
if uid.hex() == VALID_UID:
pixels.fill((0, 255, 0))
else:
pixels.fill((255, 0, 0))
pixels.show()
# Increase brightness to 100% over 1 seconds
play_sound()
brightness_transition(pixels, 1, 1)
# Fade off effect
print("fade out")
turn_off_pixels(pixels)
# Turn off all lights
print("turn off")
pixels.fill((0, 0, 0))
pixels.brightness = 1.0
pixels.show()
if __name__ == "__main__":
AUDIO_ENABLED = initialize_audio()
read_rfid()