Advertisement
KRITSADA

RaspberryPi #5 Matrix Switch and RFID Show OLED

Jan 12th, 2017
6,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.95 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. GPIO.setwarnings(False)
  4. GPIO.setmode(GPIO.BCM)
  5. col=[26,19,13,6]
  6. row=[21,20,16,7]
  7. for x in row:
  8.     GPIO.setup(x, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  9. for y in col:
  10.     GPIO.setup(y,GPIO.OUT)
  11.     GPIO.output(y,0)
  12. keypad=[
  13.                     [1,2,3,"A"],
  14.                     [4,5,6,"B"],
  15.                     [7,8,9,"C"],
  16.                     ["E",0,"F","D"]
  17.                     ]
  18. k=''
  19. cc=0
  20. a=0
  21. def button_pressed(channel):
  22.     global keypad,cc,k
  23.     a=1
  24.     while a:
  25.         for x in range(4):
  26.             GPIO.output(col[x],0)
  27.             for y in range (4):
  28.                 if GPIO.input(row[y])==0:
  29.                     while  GPIO.input(row[y])==0:
  30.                         pass
  31.                     k+=str(keypad[y][x])
  32.                     cc+=1
  33.                     a=0
  34.                     print (k,end='\r')
  35.             GPIO.output(col[x],1)
  36. GPIO.add_event_detect(21, GPIO.RISING, callback=button_pressed,
  37. bouncetime=300)
  38. GPIO.add_event_detect(20, GPIO.RISING, callback=button_pressed,
  39. bouncetime=300)
  40. GPIO.add_event_detect(16, GPIO.RISING, callback=button_pressed,
  41. bouncetime=300)
  42. GPIO.add_event_detect(7, GPIO.RISING, callback=button_pressed,
  43. bouncetime=300)
  44. import time
  45. import Adafruit_SSD1306
  46.  
  47. from PIL import Image,ImageDraw,ImageFont
  48.  
  49. # Raspberry Pi pin configuration:
  50. RST = 1
  51.  
  52. # 128x64 display with hardware I2C:
  53. disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
  54.  
  55. # Note you can change the I2C address by passing an i2c_address parameter like:
  56. # disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_address=0x3C)
  57.  
  58. # Initialize library.
  59. disp.begin()
  60. # Clear display.
  61. disp.clear()
  62. import datetime
  63. import binascii
  64. import sys
  65. import Adafruit_PN532 as PN532
  66. # Configuration for a Raspberry Pi:
  67. CS   = 8
  68. MOSI = 10
  69. MISO = 9
  70. SCLK = 11
  71.  
  72. # Create an instance of the PN532 class.
  73. pn532 = PN532.PN532(cs=CS, sclk=SCLK, mosi=MOSI, miso=MISO)
  74.  
  75. # Call begin to initialize communication with the PN532.  Must be done before
  76. # any other calls to the PN532!
  77. pn532.begin()
  78.  
  79. # Get the firmware version from the chip and print(it out.)
  80. ic, ver, rev, support = pn532.get_firmware_version()
  81. print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
  82.  
  83. # Configure PN532 to communicate with MiFare cards.
  84. pn532.SAM_configuration()
  85.  
  86. # Main loop to detect cards and read a block.
  87. print('Waiting for MiFare card...')
  88. while 1:
  89.     now = datetime.datetime.now()
  90.     now2 =now.strftime("%H:%M")
  91.     now1=now.strftime("%d/%m/%Y")
  92.     if cc==8:
  93.         if k=="01234567":
  94.             width = disp.width
  95.             height = disp.height  
  96.             image = Image.new('1', (width, height))
  97.             draw = ImageDraw.Draw(image)
  98.             font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',22)
  99.             draw.text((0,0), 'Antman',font=font, fill=1)
  100.             disp.image(image)
  101.             disp.display()
  102.             k=''
  103.             cc=0
  104.             with open("December.txt", "a") as text_file:
  105.                 text_file.write("Code:012345678  Name:Antman  Date: %s Time: %s\n" % (now1,now2))
  106.         elif k=="89ABCDEF":
  107.             width = disp.width
  108.             height = disp.height  
  109.             image = Image.new('1', (width, height))
  110.             draw = ImageDraw.Draw(image)
  111.             font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',22)
  112.             draw.text((0,0), 'Ironman',font=font, fill=1)
  113.             disp.image(image)
  114.             disp.display()
  115.             k=''
  116.             cc=0
  117.             with open("December.txt", "a") as text_file:
  118.                 text_file.write("Code:89ABCDEF  Name:Ironman  Date: %s Time: %s\n" % (now1,now2))
  119.         else:
  120.             width = disp.width
  121.             height = disp.height  
  122.             image = Image.new('1', (width, height))
  123.             draw = ImageDraw.Draw(image)
  124.             font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',18)
  125.             draw.text((0,0), 'Access Denied',font=font, fill=1)
  126.             disp.image(image)
  127.             disp.display()
  128.             k=''
  129.             cc=0
  130.     uid = pn532.read_passive_target()
  131.     # Try again if no card is available.
  132.     if uid is None:
  133.         continue
  134.     uid_str = str(binascii.hexlify(uid),'ascii')
  135.     if uid_str=="6a93305b":
  136.             width = disp.width
  137.             height = disp.height  
  138.             image = Image.new('1', (width, height))
  139.             draw = ImageDraw.Draw(image)
  140.             font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',22)
  141.             draw.text((0,0), 'Spiderman',font=font, fill=1)
  142.             print ('Found card with UID:0x{}'.format(uid_str))
  143.             disp.image(image)
  144.             disp.display()
  145.             k=''
  146.             cc=0
  147.             with open("December.txt", "a") as text_file:
  148.                 text_file.write("Code:0x6a93305b  Name:Spiderman  Date: %s Time: %s\n" % (now1,now2))
  149.     elif uid_str=="0665d093":
  150.             width = disp.width
  151.             height = disp.height  
  152.             image = Image.new('1', (width, height))
  153.             draw = ImageDraw.Draw(image)
  154.             font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',22)
  155.             draw.text((0,0), 'Superman',font=font, fill=1)
  156.             print ('Found card with UID:0x{}'.format(uid_str))
  157.             disp.image(image)
  158.             disp.display()
  159.             k=''
  160.             cc=0
  161.             with open("December.txt", "a") as text_file:
  162.                 text_file.write("Code:0x0665d093  Name:Superman  Date: %s Time: %s\n" % (now1,now2))
  163.     else:
  164.         width = disp.width
  165.         height = disp.height  
  166.         image = Image.new('1', (width, height))
  167.         draw = ImageDraw.Draw(image)
  168.         font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf',18)
  169.         draw.text((0,0), 'Access denied',font=font, fill=1)
  170.         print ('Found card with UID:0x{}'.format(uid_str))
  171.         disp.image(image)
  172.         disp.display()
  173.         k=''
  174.         cc=0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement