-
Notifications
You must be signed in to change notification settings - Fork 8
/
Project 28.py
119 lines (96 loc) · 2.87 KB
/
Project 28.py
1
import Adafruit_BBIO.GPIO as GPIOfrom time import sleepclass keypad(): def __init__(self, columnCount = 3): pin1 = "P8_14" pin2 = "P8_16" pin3 = "P8_11" pin4 = "P9_13" pin5 = "P9_12" pin6 = "P9_26" pin7 = "P9_11" pin8 = "P9_24" if columnCount is 3: self.KEYPAD = [ [1,2,3], [4,5,6], [7,8,9], ["*",0,"#"] ] self.ROW = [pin7, pin6, pin5, pin4] self.COLUMN = [pin3, pin2, pin1] elif columnCount is 4: self.KEYPAD = [ [1,2,3,"A"], [4,5,6,"B"], [7,8,9,"C"], ["*",0,"#","D"] ] self.ROW = [pin8, pin7, pin6, pin5] self.COLUMN = [pin4, pin3, pin2, pin1] else: return def getKey(self): for j in range(len(self.COLUMN)): GPIO.setup(self.COLUMN[j], GPIO.OUT) GPIO.output(self.COLUMN[j], GPIO.LOW) for i in range(len(self.ROW)): GPIO.setup(self.ROW[i], GPIO.IN, GPIO.PUD_DOWN) rowVal = -1 for i in range(len(self.ROW)): tmpRead = GPIO.input(self.ROW[i]) if tmpRead == 0: rowVal = i if rowVal <0 or rowVal >3: self.exit() return for j in range(len(self.COLUMN)): GPIO.setup(self.COLUMN[j], GPIO.IN, GPIO.PUD_DOWN) GPIO.setup(self.ROW[rowVal], GPIO.OUT) GPIO.output(self.ROW[rowVal], GPIO.HIGH) colVal = -1 for j in range(len(self.COLUMN)): tmpRead = GPIO.input(self.COLUMN[j]) if tmpRead == 1: colVal=j if colVal <0 or colVal >2: self.exit() return self.exit() return self.KEYPAD[rowVal][colVal] def exit(self): for i in range(len(self.ROW)): GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP) for j in range(len(self.COLUMN)): GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP)while True: kp = keypad(columnCount = 3) def digit(): r = None while r == None: r = kp.getKey() return r print "Please enter a 4 digit code: " d1 = digit() print d1 sleep(1) d2 = digit() print d2 sleep(1) d3 = digit() print d3 sleep(1) d4 = digit() print d4 sleep(1)if ("%s%s%s%s"%(d1,d2,d3,d4) == "1234"): print "You Entered %s%s%s%s "%(d1,d2,d3,d4) print "Access granted unlocking......" GPIO.output("P8_32", GPIO.HIGH) sleep(5) GPIO.output("P8_32", GPIO.LOW) sleep(1) GPIO.cleanup() else: print "Access denied" print "You Entered %s%s%s%s "%(d1,d2,d3,d4)