-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
177 lines (151 loc) · 4.08 KB
/
main.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
#https://www.tomshardware.com/how-to/raspberry-pi-pico-powered-rfid-lighting
from libs.mfrc522 import MFRC522 #libreria RFID
from libs.neopixel import Neopixel #libreria neopixel
from random import randint
import utime
from machine import Pin, PWM
from utime import sleep
from libs.servodegree import servo
#Definisco lettore RFID
reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)
#Numero di pixel dell'anello
numpix = 24
#Imposto numpixel, 0, pin di connessione, metodo
pixels = Neopixel(numpix, 0, 28, "GRB")
#Imposto il pin per il Buzzer
buzzer = PWM(Pin(15))
#Imposto lo stato della serratura iniziale (0 = Chiuso / 1 = Aperto)
stato = 0
#Spengo tutti i led
pixels.fill((0,0,0))
#Scrivo il valore sul led
pixels.show()
####################################################
# FUNZIONI PER IL LED NEOPIXEL RING
####################################################
#Spegne i pixel
def spegni_pixel():
pixels.fill((0,0,0))
pixels.show()
#Luce fissa rossa
def lucerossa():
pixels.fill((128,0,0))
pixels.show()
#Luce fissa verde
def luceverde():
pixels.fill((0,128,0))
pixels.show()
#Lampeggia per X volte in rosso
def red_blynk(times):
for i in range(times):
pixels.fill((128,0,0))
pixels.show()
beep(0.1)
utime.sleep_ms(100)
beep(0.1)
pixels.fill((0,0,0))
pixels.show()
utime.sleep_ms(100)
#Non in uso, sostituito da "loading"
def spirale():
color = 100,100,100
for i in range(24):
print(i)
pixels.set_pixel(i, (color))
pixels.show()
utime.sleep_ms(10)
utime.sleep_ms(100)
spegni_pixel()
#Spirale di caricamento bianca
def loading(speed):
for i in range(24):
pixels.set_pixel(i, (100, 100, 100))
pixels.show()
utime.sleep_ms(speed)
beep(0.03)
utime.sleep_ms(10)
spegni_pixel()
#Lampeggia in fade verde
def success_boot():
hue = 0
for i in range(50):
color = pixels.colorHSV(20000, 255, hue)
pixels.fill(color)
pixels.show()
hue += 1
hue1 = 50
beep(0.1)
for i in range(50):
color = pixels.colorHSV(20000, 255, hue1)
pixels.fill(color)
pixels.show()
hue1 -= 1
spegni_pixel()
#####################################################
# INIZIO CODICE BUZZER
#####################################################
def beep(durata):
buzzer.freq(1500)
buzzer.duty_u16(3000)
sleep(durata)
buzzer.duty_u16(0)
#####################################################
# INIZIO CODICE SERVO
#####################################################
def apri():
servo(10)
print("Serratura aperta")
def chiudi():
servo(70)
print("Serratura chiusa")
def toggle():
global stato
#Se lo stato è uno, chiudo la serratura e imposto la luce su rosso
if stato == 1:
stato = 0
chiudi()
lucerossa()
utime.sleep_ms(1000)
#Se lo stato è zero, apro la serratura e imposto la luce su verde
elif stato == 0:
stato = 1
apri()
luceverde()
utime.sleep_ms(1000)
else:
red_blynk(5)
##############################################
# INIZIO CODICE ##############################
##############################################
print("Avvio in corso..")
chiudi()
utime.sleep_ms(500)
print("Attendere...")
for i in range(1):
loading(20)
utime.sleep_ms(1000)
lucerossa()
while True:
reader.init()
(stat, tag_type) = reader.request(reader.REQIDL)
if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if stat == reader.OK:
card = int.from_bytes(bytes(uid),"little",False)
print(card)
if card == 1274987924:
beep(0.1)
toggle()
elif card == 825572532:
beep(0.1)
toggle()
else:
print("Carta non trovata")
red_blynk(3)
chiudi()
lucerossa()
else:
print("Reader KO")
red_blynk(10)
pass
utime.sleep_ms(50)