from time import sleep
from machine import I2C, Pin
from pico_i2c_lcd import I2cLcd
from picozero import Servo
import rp2
from rp2 import PIO
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
@rp2.asm_pio(out_init=[PIO.OUT_LOW])
def echo():
wrap_target()
mov(pins, isr)
mov(isr, invert(isr))
pull(noblock)
mov(x, osr)
mov(y, x)
label("loop")
jmp(y_dec, "loop")
wrap()
sm = rp2.StateMachine(0, echo, freq=1_000_000, out_base=Pin(18))
sm.active(1)
def play(freq):
if freq:
sm.put(1_000_000//freq)
else:
sm.put(0)
password_length = 0
password = []
can_reset = 0
reset_password_length = 0
reset_password = []
login1 = 0
tries = 0
tries1 = 0
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20)
need_check = False
col_list=[6,7,8,9]
row_list=[10,11,12,13]
servo = Servo(20)
red_led = Pin(14, Pin.OUT)
blue_led = Pin(15, Pin.OUT)
for x in range(0,4):
row_list[x]=Pin(row_list[x], Pin.OUT)
row_list[x].value(1)
for x in range(0,4):
col_list[x] = Pin(col_list[x], Pin.IN, Pin.PULL_UP)
key_map=[["D","#","0","*"],\
["C","9","8","7"],\
["B","6","5","4"],\
["A","3","2","1"]]
def Keypad4x4Read(cols,rows):
for r in rows:
r.value(0)
result=[cols[0].value(),cols[1].value(),cols[2].value(),cols[3].value()]
if min(result)==0:
key=key_map[int(rows.index(r))][int(result.index(0))]
r.value(1)
return(key)
r.value(1)
with open('file.txt', 'r') as file:
array = [int(num) for line in file for num in line.split()]
password = array
password_length = len(password)
need_check = password_length == 0
with open('reset_file.txt', 'r') as file:
array = [int(num) for line in file for num in line.split()]
reset_password = array
reset_password_length = len(reset_password)
if reset_password_length > 0:
can_reset = 1
def isInt(s):
try:
int(s)
return True
except ValueError:
return False
def reset_password1():
print("Скидання пароля")
lcd.clear()
lcd.move_to(2,20);
lcd.putstr("Reset Password")
sleep(1)
lcd.clear()
global reset_password_length, reset_password, tries1, password_length, password
if can_reset == 0:
password_length = 0
password = []
lcd.move_to(2,20);
lcd.putstr("password reset")
sleep(0.5)
print("Пароль скинутий")
enter_password()
elif can_reset == 1:
password1 = []
lcd.clear()
lcd.move_to(3,20);
lcd.putstr("Enter password")
lcd.move_to(0,1);
lcd.putstr(">")
lcd.move_to(2,1);
print("Введіть пароль скидання")
while True:
sleep(0.1)
key=Keypad4x4Read(col_list, row_list)
if key != None and isInt(key):
password1.append(int(key))
lcd.putstr("*")
if key != None and key.strip() == 'A':
break
sleep(2)
lcd.clear()
lcd.move_to(4,20);
lcd.putstr("Please wait")
col1 = 3
while col1 > 0:
sleep(1.33)
col1 = col1 - 1
lcd.putstr(".")
if len(password1) != len(reset_password):
print("Пароль не правильний")
tries1 = tries1 + 1
lcd.clear()
lcd.move_to(3,20);
lcd.putstr("Password is")
lcd.move_to(3,1);
lcd.putstr("not correct")
sleep(0.75)
elif password1 != reset_password:
print("Пароль не правильний")
tries1 = tries1 + 1
lcd.clear()
lcd.move_to(3,20);
lcd.putstr("Password is")
lcd.move_to(3,1);
lcd.putstr("not correct")
sleep(0.75)
else:
print("Пароль скинутий")
password_length = 0
password = []
lcd.clear()
lcd.move_to(2,20);
lcd.putstr("password reset")
sleep(0.5)
enter_password()
def configurate_password():
lcd.clear()
lcd.move_to(0,20);
lcd.putstr("A - without password")
lcd.move_to(0,1);
lcd.putstr("B - with password")
print("Налаштування скидання пароля")
print("Введіть: 0 - без пароля скидання, 1 - з паролем скидання")
global can_reset
while True:
sleep(0.1)
key=Keypad4x4Read(col_list, row_list)
if key != None and key.strip() == 'A':
can_reset = 0
lcd.clear()
break
elif key != None and key.strip() == 'B':
can_reset = 1
global reset_password_length, reset_password
lcd.clear()
lcd.move_to(2,20);
lcd.putstr("Please enter")
lcd.move_to(1,1);
lcd.putstr("length password: ")
sleep(0.5)
print("Введіть довжину пароля")
while True:
sleep(0.1)
key=Keypad4x4Read(col_list, row_list)
if key != None and isInt(key):
reset_password_length = 1 + int(key)
print("Длина пароля:", reset_password_length)
lcd.putstr(str(reset_password_length))
break
if reset_password_length != 0:
break
sleep(1)
lcd.clear()
lcd.move_to(0,20);
lcd.putstr("enter password")
lcd.move_to(0,1);
lcd.putstr("Password: ")
print("Введіть комбінацію чисел")
reset_password = []
while len(reset_password) < reset_password_length:
sleep(0.1)
key=Keypad4x4Read(col_list, row_list)
if key != None and isInt(key):
reset_password.append(int(key))
print("Натиснута кнопка:", int(key))
lcd.putstr("*")
sleep(0.2)
sleep(1)
lcd.clear()
lcd.move_to(6,20);
lcd.putstr("Wait")
col1 = 3;
while col1 > 0:
sleep(0.5)
lcd.putstr(".")
col1 = col1 - 1
sleep(0.5)
lcd.clear()
lcd.move_to(2,20);
lcd.putstr("Successfully!")
sleep(1)
lcd.move_to(0,1);
lcd.putstr("Save password")
col1 = 3;
while col1 > 0:
sleep(0.5)
lcd.putstr(".")
col1 = col1 - 1
print("Ваш пароль скидання:", reset_password)
break
def enter_password():
sleep(0.5);
lcd.clear()
lcd.move_to(2,20);
lcd.putstr("Please enter")
lcd.move_to(1,1);
lcd.putstr("length password: ")
sleep(0.5)
global password_length, password, key_names
print("Введіть довжину пароля")
while True:
sleep(0.1)
key=Keypad4x4Read(col_list, row_list)
if key != None and isInt(key):
password_length = 1 + int(key)
print("Длина пароля:", password_length)
lcd.putstr(str(password_length))
break
if password_length != 0:
break
sleep(1)
lcd.clear()
lcd.move_to(0,20);
lcd.putstr("enter password")
lcd.move_to(0,1);
lcd.putstr("Password: ")
print("Введіть комбінацію чисел")
while len(password) < password_length:
sleep(0.1)
key=Keypad4x4Read(col_list, row_list)
if key != None and isInt(key):
password.append(int(key))
print("Натиснута кнопка:", int(key))
lcd.putstr("*")
sleep(0.2)
sleep(1)
lcd.clear()
lcd.move_to(6,20);
lcd.putstr("Wait")
col1 = 3;
while col1 > 0:
sleep(0.5)
lcd.putstr(".")
col1 = col1 - 1
sleep(0.5)
lcd.clear()
lcd.move_to(2,20);
lcd.putstr("Successfully!")
sleep(1)
lcd.move_to(0,1);
lcd.putstr("Save password")
col1 = 3;
while col1 > 0:
sleep(0.5)
lcd.putstr(".")
col1 = col1 - 1
with open('file.txt', 'w') as file:
for element in password:
file.write(str(element) + ' ')
print("Ваш пароль:", password)
def login():
lcd.clear()
lcd.move_to(3,20);
lcd.putstr("Enter password")
lcd.move_to(0,1);
lcd.putstr(">")
lcd.move_to(2,1);
print("Введіть пароль")
password1 = []
global tries, password, login1, secret_code
while True:
sleep(0.1)
key=Keypad4x4Read(col_list, row_list)
if key != None and isInt(key):
password1.append(int(key))
lcd.putstr("*")
if key != None and key.strip() == 'A':
break
if key != None and key.strip() == 'C':
reset_password1()
break
sleep(2)
lcd.clear()
lcd.move_to(4,20);
lcd.putstr("Please wait")
col1 = 3
while col1 > 0:
sleep(1.33)
col1 = col1 - 1
lcd.putstr(".")
if len(password1) != len(password):
lcd.clear()
lcd.move_to(3,20);
lcd.putstr("Password is")
lcd.move_to(3,1);
lcd.putstr("not correct")
sleep(0.75)
print("Пароль не правильний")
tries = tries + 1
elif password1 != password:
lcd.clear()
lcd.move_to(3,20);
lcd.putstr("Password is")
lcd.move_to(3,1);
lcd.putstr("not correct")
sleep(0.75)
print("Пароль не правильний")
tries = tries + 1
else:
login1 = 1
print("Вхід успішний")
lcd.clear()
lcd.move_to(2,20);
lcd.putstr("Successfully!")
servo.value = 1
tries = 0
sleep(0.75)
massive = [[500,1000],[625,1125],[750,1250]]
index = 0
sleep(0.2)
lcd.move_to(6,20);
lcd.putstr("Welcome")
sleep(0.5);
lcd.move_to(4,1);
lcd.putstr("Please wait")
col1 = 3
while col1 > 0:
sleep(1.33)
col1 = col1 - 1
lcd.putstr(".")
if password_length == 0:
enter_password()
if reset_password_length == 0:
print("Налаштувати скидання пароля?")
lcd.clear()
lcd.move_to(3,20);
lcd.putstr("Enable password")
lcd.move_to(6,1);
lcd.putstr("reset?")
while True:
key=Keypad4x4Read(col_list, row_list)
sleep(0.1)
if reset_password_length != 0:
can_reset = 1
lcd.clear()
break
if key != None and key.strip() == 'A':
lcd.clear()
configurate_password()
break
if key != None and key.strip() == 'B':
lcd.clear()
break
while True:
key=Keypad4x4Read(col_list, row_list)
sleep(0.1)
if login1 == 0 and tries < 3:
login()
elif login1 == 1:
if key != None and key.strip() == 'C':
reset_password1()
elif key != None and key.strip() == 'D':
login1 = 0
tries = 0
servo.value = 0.5
login()
key=Keypad4x4Read(col_list, row_list)
if key != None and key.strip() == 'C':
reset_password1()
elif tries >= 3:
while True:
if index == 0:
lcd.clear()
lcd.move_to(7,20);
lcd.putstr("ERROR!")
red_led.toggle()
play(massive[index][0])
sleep(0.5)
red_led.toggle()
play(massive[index][1])
blue_led.toggle()
sleep(0.5)
blue_led.toggle()
play(0)
sleep(0.5)
index = index + 1
index = index % 3