Created
March 2, 2017 18:51
-
-
Save wtrssn/6b2d45e3367deb0acf7ca6a666a20d6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import threading | |
from gpiozero import LED, RGBLED, Button, Buzzer | |
from time import time, sleep | |
from signal import pause | |
from random import randint | |
STATUS_BLOCKED = (0,0,0) | |
STATUS_LOCK = (1,0,0) | |
STATUS_UNLOCK = (0,1,0) | |
STATUS_FIRST_EDIT = (0,0,1) | |
STATUS_SECOND_EDIT = (1,0,1) | |
STATUS_CONFIRMATION = (1,1,1) | |
STATUS_SUCESSFUL = (0.1,1,0) | |
button0 = Button(24, False) | |
button1 = Button(25, False) | |
button2 = Button(8, False) | |
button3 = Button(7, False) | |
buttons = [button0, button1, button2, button3] | |
led0 = LED(12) | |
led1 = LED(16) | |
led2 = LED(20) | |
led3 = LED(21) | |
leds = [led0,led1,led2,led3] | |
ledRGB = RGBLED(26, 19, 13) | |
bz = Buzzer(17) | |
MAX_ATTEMPTS = 3 | |
DEFAULT_PWD = [0,1,2,3] | |
class Keypad(): | |
t0 = time() | |
password = DEFAULT_PWD | |
keypad_status = STATUS_LOCK | |
keypad_blocked = False | |
keypad_attempts = MAX_ATTEMPTS | |
selected_key = "" | |
edit_type = STATUS_LOCK | |
def ___init___(self): | |
self.reset() | |
def reset(self): | |
self.t0 = time() | |
def set_password(self, passw): | |
self.password = passw | |
def set_keypad_status(self, set): | |
self.keypad_status = set | |
self.edit_type = set | |
def set_keypad_blocked(self,set): | |
self.keypad_blocked = set | |
def set_keypad_attempts(self, set): | |
self.keypad_attempts = set | |
def reduce_attempts(self): | |
self.keypad_attempts -= 1 | |
def check_attempts(self): | |
if self.keypad_attempts <= 0: | |
self.reset_attempts() | |
return False | |
else: | |
return True | |
def reset_attempts(self): | |
self.keypad_attempts = MAX_ATTEMPTS | |
def set_edit_type(self, set): | |
self.edit_type=set | |
def set_selected_key(self, key): | |
self.selected_key = key | |
def waif_for_press_any_button(msg, buttons): | |
k.reset() | |
print(msg) | |
while True: | |
for i in range(len(buttons)): | |
if check_button_release(i): | |
k.set_selected_key(str(i)) | |
return str(i) | |
return False | |
def check_button_release(i): | |
if buttons[i].is_pressed: | |
while buttons[i].is_pressed: | |
leds[i].on() | |
pass | |
k.reset() | |
leds[i].off() | |
return True | |
return False | |
def get_button_pressed(i, p): | |
if buttons[i].is_pressed: | |
while buttons[i].is_pressed: | |
leds[i].on() | |
pass | |
k.reset() | |
p.append(i) | |
leds[i].off() | |
def get_password(): | |
p=list() | |
k.reset() | |
while True: | |
for i in range(len(buttons)): | |
get_button_pressed(i, p) | |
if time()-k.t0 > 4: | |
break | |
return p | |
def validate_password(): | |
validation = False | |
k.set_edit_type(STATUS_CONFIRMATION) | |
if k.password == get_password(): | |
validation = True | |
k.reset_attempts() | |
else: | |
k.reduce_attempts() | |
k.set_edit_type(k.keypad_status) | |
return validation | |
def get_new_password(): | |
k.set_edit_type(STATUS_FIRST_EDIT) #<--- blink blue | |
p = list() | |
print ("\nEnter the new password :") | |
p1 = get_password() | |
if len(p1) < 4 : | |
print("\nThe password must have 4 or more keys") | |
else: | |
if(p1 == k.password): | |
print("\nThe new password must be different from the current one") | |
else: | |
k.set_edit_type(STATUS_SECOND_EDIT) #<--- blink violeta | |
print ("\nRetype the new password :") | |
p2 = get_password() | |
if p1 == p2: | |
k.set_password(p2) | |
print("\nCongratulations you have a new password") | |
k.set_edit_type(STATUS_SUCESSFUL) | |
sleep(2) #<--- blink to confirm sucess | |
k.set_edit_type(k.keypad_status) | |
return True | |
else: | |
print("\nWrong Password Entry. Try Again ") | |
k.set_edit_type(k.keypad_status) | |
return False | |
def set_color (color): | |
ledRGB.color = color | |
def alternate_color(): | |
while True: | |
if k.keypad_status!=STATUS_LOCK or k.keypad_status!=STATUS_UNLOCK: | |
ledRGB.color = k.keypad_status | |
sleep(0.1) | |
set_color(k.edit_type) | |
sleep(0.1) | |
if k.keypad_blocked: | |
set_color(k.keypad_status) | |
sleep(0.1) | |
set_color(STATUS_BLOCKED) | |
sleep(0.05) | |
set_color(k.keypad_status) | |
sleep(0.1) | |
set_color(STATUS_BLOCKED) | |
sleep(1) | |
def thread_colorRGB(): | |
t = threading.Thread(target=alternate_color, args=()) | |
t.daemon = True | |
t.start() | |
def buzz(pitch, duration): | |
if (pitch == 0): | |
sleep(duration) | |
return | |
period = 1.0 / pitch | |
delay = period / 2 | |
cycles = int(duration * pitch) | |
for i in range(cycles): | |
bz.on() | |
sleep(delay) | |
bz.off() | |
sleep(delay) | |
def toneFail(): | |
pitches=[1047, 988,659] | |
duration=0.1 | |
for p in pitches: | |
buzz(p, duration) | |
sleep(duration *0.5) | |
def toneSucess(): | |
pitches=[262,294,330,349,392,440,494,523, 587,659,698,784,880,988,1047] | |
duration=0.04 | |
for p in pitches: | |
buzz(p, duration) | |
sleep(duration *0.5) | |
k=Keypad() | |
menu0=""" | |
::: KEYPAD ACCESS OPTIONS ::: | |
0 . Modify The Current Password | |
1 . Lock / Unlock Keypad | |
""" | |
thread_colorRGB() | |
theEnd = False | |
while theEnd == False: | |
set_color(k.keypad_status) | |
print(k.password) | |
if not k.check_attempts(): | |
k.set_keypad_blocked(True) | |
t=randint(5,10) | |
print("\nThere have been three failed validation attempts, the keypad will be temporarily locked") | |
sleep(t) | |
k.set_keypad_blocked(False) | |
print("\nThe keypad is again operational") | |
try: | |
#command = input(menu0) | |
command = waif_for_press_any_button(menu0, buttons) | |
if (command=="0"): #<-- change password | |
print("\nTo change the password before you must enter the current password") | |
if validate_password(): | |
print("\nCorrect Current Password !!!") | |
if get_new_password(): | |
toneSucess() | |
else: | |
toneFail() | |
else: | |
toneFail() | |
print("\nX X X Invalid Current Password X X X ") | |
elif (command=="1"): #<-- lock & unlock keypad | |
if k.keypad_status==STATUS_LOCK: #<-- unlock | |
print("\nTo UNLOCK the system enter the current password") | |
if validate_password(): | |
print("\nCorrect Current Password !!!") | |
print("Unlocked System !!!") | |
k.set_keypad_status(STATUS_UNLOCK) | |
toneSucess() | |
else: | |
print("\nX X X Invalid Current Password X X X ") | |
print("X X X Locked System X X X") | |
toneFail() | |
elif k.keypad_status==STATUS_UNLOCK: #<-- lock | |
print("\nTo LOCK the system enter the current password") | |
if validate_password(): | |
print("\nCorrect Current Password !!!") | |
print("Locked System !!!") | |
k.set_keypad_status(STATUS_LOCK) | |
toneSucess() | |
else: | |
print("\nX X X Invalid Current Password X X X ") | |
print("X X X Unlocked System X X X") | |
toneFail() | |
elif (command=="2"): | |
pass | |
elif (command=="3"): | |
pass | |
except: | |
print("\nUnexpected Error", sys.exc_info()[0]) | |
theEnd = True | |
quit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment