KRITSADA

RaspberryPi #4 Matrix Switch Password Check

Jan 12th, 2017
6,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.48 KB | None | 0 0
  1. import time
  2. import RPi.GPIO as GPIO
  3. GPIO.setwarnings(False)
  4. class keypad():
  5.     def __init__(self,Row,Column): 
  6.         GPIO.setmode(GPIO.BCM)
  7.         self.KEYPAD = [
  8.                     [1,2,3,"A"],
  9.                     [4,5,6,"B"],
  10.                     [7,8,9,"C"],
  11.                     ["*",0,"#","D"]
  12.                     ]
  13.         self.ROW    = Row
  14.         self.COLUMN = Column  
  15.        
  16.     def getKey(self):
  17.          
  18.         # Set all columns as output low
  19.         for j in range(len(self.COLUMN)):
  20.             GPIO.setup(self.COLUMN[j], GPIO.OUT)
  21.             GPIO.output(self.COLUMN[j], GPIO.LOW)
  22.          
  23.         # Set all rows as input
  24.         for i in range(len(self.ROW)):
  25.             GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  26.          
  27.         # Scan rows for pushed key/button
  28.         # A valid key press should set "rowVal"  between 0 and 3.
  29.         rowVal = -1
  30.         for i in range(len(self.ROW)):
  31.             tmpRead = GPIO.input(self.ROW[i])
  32.             if tmpRead == 0:
  33.                 rowVal = i
  34.                  
  35.         # if rowVal is not 0 thru 3 then no button was pressed and we can exit
  36.         if rowVal <0 or rowVal >3:
  37.             self.exit()
  38.             return
  39.          
  40.         # Convert columns to input
  41.         for j in range(len(self.COLUMN)):
  42.             GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  43.          
  44.         # Switch the i-th row found from scan to output
  45.         GPIO.setup(self.ROW[rowVal], GPIO.OUT)
  46.         GPIO.output(self.ROW[rowVal], GPIO.HIGH)
  47.  
  48.         # Scan columns for still-pushed key/button
  49.         # A valid key press should set "colVal"  between 0 and 3.
  50.         colVal = -1
  51.         for j in range(len(self.COLUMN)):
  52.             tmpRead = GPIO.input(self.COLUMN[j])
  53.             if tmpRead == 1:
  54.                 colVal=j
  55.                  
  56.         # if colVal is not 0 thru 3 then no button was pressed and we can exit
  57.         if colVal <0 or colVal >3:
  58.             self.exit()
  59.             return
  60.            
  61.         # Return the value of the key pressed
  62.         self.exit()
  63.         return self.KEYPAD[rowVal][colVal]
  64.          
  65.     def exit(self):
  66.         # Reinitialize all rows and columns as input at exit
  67.         for i in range(len(self.ROW)):
  68.             GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  69.         for j in range(len(self.COLUMN)):
  70.             GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  71.                
  72. kp = keypad(Row=[21,20,16,7],Column=[26,19,13,6])
  73. st_press=0
  74. digit_str=''
  75. x=0
  76. import RPi.GPIO as GPIO
  77. import time
  78. from datetime import datetime
  79. GPIO.setwarnings(False)
  80. GPIO.setmode(GPIO.BCM)
  81. GPIO.setup(24,GPIO.OUT)
  82. blink = GPIO.PWM(24,500)
  83. blink.start(0)
  84. import time
  85. from neopixel import *
  86. import time
  87. import Adafruit_SSD1306
  88.  
  89. from PIL import Image,ImageDraw,ImageFont
  90.  
  91. # Raspberry Pi pin configuration:
  92. RST = 1
  93.  
  94. # 128x64 display with hardware I2C:
  95. disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
  96.  
  97. # Note you can change the I2C address by passing an i2c_address parameter like:
  98. # disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_address=0x3C)
  99.  
  100. # Initialize library.
  101. disp.begin()
  102. # Clear display.
  103. disp.clear()
  104. # LED strip configuration:
  105. LED_COUNT      = 1       # Number of LED pixels.
  106. LED_PIN        = 12      # GPIO pin connected to the pixels (must support PWM!).
  107. LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
  108. LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
  109. LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
  110. LED_INVERT     = False
  111. strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
  112.     # Intialize the library (must be called once before other functions).
  113. strip.begin()
  114. while 1:    
  115.     # Loop while waiting for a keypress
  116.     digit = None
  117.     digit = kp.getKey()
  118.     if digit == None:
  119.         st_press=1
  120.         blink.ChangeDutyCycle(0)
  121.         strip.setPixelColor(0, Color(0, 0,0))
  122.         strip.show()
  123.         width = disp.width
  124.         height = disp.height  
  125.         image = Image.new('1', (width, height))
  126.         draw = ImageDraw.Draw(image)
  127.         font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',22)
  128.         draw.text((0,0), '           ',font=font, fill=1)
  129.         draw.text((0,20), '             ',font=font, fill=1)
  130.         draw.text((0,40), '           ',font=font, fill=1)
  131.         disp.image(image)
  132.         disp.display()
  133.     else:
  134.         if st_press:
  135.             blink.ChangeDutyCycle(50)
  136.             # Print the result
  137.             x+=1
  138.             digit_str+=str(digit)
  139.             print (digit_str,end='\r')
  140.             st_press=0
  141.             time.sleep(0.3)
  142.             blink.ChangeDutyCycle(0)
  143.             if x==4:
  144.                 if digit_str=="4096":
  145.                     width = disp.width
  146.                     height = disp.height  
  147.                     image = Image.new('1', (width, height))
  148.                     draw = ImageDraw.Draw(image)
  149.                     font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',22)
  150.                     draw.text((0,0), 'Ironman',font=font, fill=1)
  151.                     disp.image(image)
  152.                     disp.display()
  153.                     strip.setPixelColor(0, Color(0, 0, 255))# Red  
  154.                     strip.show()
  155.                     digit_str=''
  156.                     x=0
  157.                     time.sleep(1)
  158.                 elif digit_str=="2048":
  159.                     width = disp.width
  160.                     height = disp.height  
  161.                     image = Image.new('1', (width, height))
  162.                     draw = ImageDraw.Draw(image)
  163.                     font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',22)
  164.                     draw.text((0,0), 'Antman',font=font, fill=1)
  165.                     disp.image(image)
  166.                     disp.display()
  167.                     strip.setPixelColor(0, Color(0,255,0))# Red  
  168.                     strip.show()
  169.                     digit_str=''
  170.                     x=0
  171.                     time.sleep(1)
  172.                 else:
  173.                     width = disp.width
  174.                     height = disp.height  
  175.                     image = Image.new('1', (width, height))
  176.                     draw = ImageDraw.Draw(image)
  177.                     font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',18)
  178.                     draw.text((0,0), 'Access denind',font=font, fill=1)
  179.                     disp.image(image)
  180.                     disp.display()
  181.                     strip.setPixelColor(0, Color(255,0,0))# Red  
  182.                     strip.show()
  183.                     digit_str=''
  184.                     x=0
  185.                     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment