Commit 96c13a1e authored by ragulkanth's avatar ragulkanth

refined the code and done with the testing

parent 37bdd04d
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
from threading import Timer
class PidoorDaemon(object):
def __init__(self, keypad, row_pins, col_pins, lock_pin, default_debounce_time = 10, password_prompt_time = 10, max_pass_length = 6, min_pass_length = 4, unlocked_time = 5):
self.keypad = keypad
self.row_pins = row_pins
self.col_pins = col_pins
self.lock_pin = lock_pin
self.default_debounce_time = default_debounce_time
self.password_prompt_time = password_prompt_time
self.max_pass_length = max_pass_length
self.min_pass_length = min_pass_length
self.unlocked_time = unlocked_time
self.timer = Timer(self.password_prompt_time, self.clearPassword)
self.password = ''
self.passwords = ["5033"]
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
self.setLockAsOutput()
self.setRowsAsInput()
self.setColumnsAsOutput()
def setLockAsOutput(self):
GPIO.setup(self.lock_pin, GPIO.OUT)
def setRowsAsInput(self):
# Set all rows as input
for i in range(len(self.row_pins)):
GPIO.setup(self.row_pins[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(self.row_pins[i], GPIO.FALLING, callback=self.onKeyPress, bouncetime=self.default_debounce_time)
def setColumnsAsOutput(self):
# Set all columns as output low
for j in range(len(self.col_pins)):
GPIO.setup(self.col_pins[j], GPIO.OUT)
GPIO.output(self.col_pins[j], GPIO.LOW)
def getKey(self):
keyVal = None
# Scan rows for pressed key
rowVal = None
for i in range(len(self.row_pins)):
tmpRead = GPIO.input(self.row_pins[i])
if tmpRead == 0:
rowVal = i
break
# Scan columns for pressed key
colVal = None
if rowVal is not None:
for i in range(len(self.col_pins)):
GPIO.output(self.col_pins[i], GPIO.HIGH)
if GPIO.input(self.row_pins[rowVal]) == GPIO.HIGH:
GPIO.output(self.col_pins[i], GPIO.LOW)
colVal = i
break
GPIO.output(self.col_pins[i], GPIO.LOW)
# Determine pressed key, if any
if colVal is not None:
keyVal = self.keypad[rowVal][colVal]
return keyVal
def onKeyPress(self, channel):
keyPressed = self.getKey()
if keyPressed is not None:
self.password += keyPressed
if len(self.password) == 1:
self.timer = Timer(self.password_prompt_time, self.clearPassword)
self.timer.start()
if len(self.password) >= self.min_pass_length and len(self.password) <= self.max_pass_length:
if self.password in self.passwords:
self.unlock()
self.timer.cancel()
self.clearPassword()
elif len(self.password) == self.max_pass_length:
self.clearPassword()
def unlock(self):
GPIO.output(self.lock_pin, GPIO.HIGH)
Timer(self.unlocked_time, lambda: GPIO.output(self.lock_pin, GPIO.LOW)).start()
def clearPassword(self):
self.password = ''
if __name__ == "__main__":
# Setup Keypad
KEYPAD = [
["1","2","3","A"],
["4","5","6","B"],
["7","8","9","C"],
["*","0","#","D"]
]
ROW_PINS = [4, 14, 15, 17] # BCM numbering
COL_PINS = [18, 27, 22, 23] # BCM numbering
LOCK_PIN = 24
keypad = PidoorDaemon(KEYPAD, ROW_PINS, COL_PINS, LOCK_PIN)
while True:
time.sleep(0.2)
#from pad4pi import rpi_gpio
import RPi.GPIO as GPIO
import time
from threading import Timer
DEFAULT_KEY_DELAY = 300
DEFAULT_REPEAT_DELAY = 1.0
DEFAULT_REPEAT_RATE = 1.0
DEFAULT_DEBOUNCE_TIME = 10
MAX_PASS_LENGTH = 6
MIN_PASS_LENGTH = 4
UNLOCKED_TIME = 5
PASSWORD_PROMPT_TIME = 10
# Setup Keypad
KEYPAD = [
["1","2","3","A"],
["4","5","6","B"],
["7","8","9","C"],
["*","0","#","D"]
]
ROW_PINS = [4, 14, 15, 17] # BCM numbering
COL_PINS = [18, 27, 22, 23] # BCM numbering
LOCK_PIN = 24
#seting up the GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
password = ""
passwords = ["123456"]
def setLockAsOutput():
GPIO.setup(LOCK_PIN, GPIO.OUT)
def setRowsAsInput(row_pins):
# Set all rows as input
for i in range(len(row_pins)):
GPIO.setup(row_pins[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(row_pins[i], GPIO.FALLING, callback=onKeyPress, bouncetime=DEFAULT_DEBOUNCE_TIME)
def setColumnsAsOutput(col_pins):
# Set all columns as output low
for j in range(len(col_pins)):
GPIO.setup(col_pins[j], GPIO.OUT)
GPIO.output(col_pins[j], GPIO.LOW)
def getKey(keypad, row_pins, col_pins):
keyVal = None
# Scan rows for pressed key
rowVal = None
for i in range(len(row_pins)):
tmpRead = GPIO.input(row_pins[i])
if tmpRead == 0:
rowVal = i
break
# Scan columns for pressed key
colVal = None
if rowVal is not None:
for i in range(len(col_pins)):
GPIO.output(col_pins[i], GPIO.HIGH)
if GPIO.input(row_pins[rowVal]) == GPIO.HIGH:
GPIO.output(col_pins[i], GPIO.LOW)
colVal = i
break
GPIO.output(col_pins[i], GPIO.LOW)
# Determine pressed key, if any
if colVal is not None:
keyVal = keypad[rowVal][colVal]
return keyVal
def onKeyPress(channel):
global password
global timer
keyPressed = getKey(KEYPAD, ROW_PINS, COL_PINS)
if keyPressed is not None:
password += keyPressed
if len(password) == 1:
timer = Timer(PASSWORD_PROMPT_TIME, clearPassword)
timer.start()
if len(password) >= MIN_PASS_LENGTH and len(password) <= MAX_PASS_LENGTH:
if password in passwords:
unlock()
timer.cancel()
clearPassword()
elif len(password) == MAX_PASS_LENGTH:
clearPassword()
def unlock():
GPIO.output(LOCK_PIN, GPIO.HIGH)
Timer(UNLOCKED_TIME, lambda: GPIO.output(LOCK_PIN, GPIO.LOW)).start()
def clearPassword():
global password
password = ''
if __name__ == "__main__":
setLockAsOutput()
setRowsAsInput(ROW_PINS)
setColumnsAsOutput(COL_PINS)
while True:
time.sleep(0.2)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment