Guest User

Untitled

a guest
Jan 24th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import lcd
  4. import os
  5. import subprocess
  6. from subprocess import call
  7.  
  8. mylcd = lcd.lcd()
  9.  
  10. GPIO.setwarnings(False)
  11. GPIO.setmode(GPIO.BCM)
  12.  
  13. MATRIX = [['1', '2', '3', 'A'],
  14. ['4', '5', '6', 'B'],
  15. ['7', '8', '9', 'C'],
  16. ['*', '0', '#', 'D']]
  17.  
  18. ROW = [5,6,13,19]
  19. COL = [26,16,20,21]
  20.  
  21. for j in range(4):
  22. GPIO.setup(COL[j], GPIO.OUT)
  23. GPIO.output(COL[j], 1)
  24.  
  25. for i in range(4):
  26. GPIO.setup(ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  27.  
  28. password = "1234"
  29. attempt = ""
  30. try:
  31. while (True):
  32. mylcd.lcd_display_string("Enter pw",1)
  33. for j in range(4):
  34. GPIO.output(COL[j], 0)
  35.  
  36. for i in range(4):
  37. if GPIO.input(ROW[i]) == 0:
  38. mylcd.lcd_display_string("*",2,0)
  39. time.sleep(0.01)
  40. while (GPIO.input(ROW[i]) == 0):
  41. pass
  42. attempt += MATRIX[i][j]
  43. if len(attempt) == len(password):
  44. if attempt == password:
  45. mylcd.lcd_clear()
  46. print "Password OK"
  47. mylcd.lcd_display_string("Password",1 ,4 )
  48. mylcd.lcd_display_string("Ok!",2 ,6 )
  49. time.sleep(2)
  50. mylcd.lcd_clear()
  51. os.chdir('/home/pi/nfc/py532lib')
  52. call('./nfc.sh')
  53. mylcd.lcd_clear()
  54. else:
  55. print(attempt)
  56. print "Password incorrect"
  57. mylcd.lcd_display_string("Password",1,4)
  58. mylcd.lcd_display_string("Incorrect!",2,3)
  59. time.sleep(2)
  60. mylcd.lcd_clear()
  61. attempt = ""
  62. time.sleep(0.01)
  63. GPIO.output(COL[j], 1)
  64. except KeyboardInterrupt:
  65. GPIO.cleanup()
  66.  
  67. # -*- coding: utf-8 -*-
  68. # Original code found at:
  69. # https://gist.github.com/DenisFromHR/cc863375a6e19dce359d
  70.  
  71. """
  72. Compiled, mashed and generally mutilated 2014-2015 by Denis Pleic
  73. Made available under GNU GENERAL PUBLIC LICENSE
  74.  
  75. # Modified Python I2C library for Raspberry Pi
  76. # as found on http://www.recantha.co.uk/blog/?p=4849
  77. # Joined existing 'i2c_lib.py' and 'lcddriver.py' into a single library
  78. # added bits and pieces from various sources
  79. # By DenisFromHR (Denis Pleic)
  80. # 2015-02-10, ver 0.1
  81.  
  82. """
  83.  
  84. # i2c bus (0 -- original Pi, 1 -- Rev 2 Pi)
  85. I2CBUS = 0
  86.  
  87. # LCD Address
  88. ADDRESS = 0x27
  89.  
  90. import smbus
  91. from time import sleep
  92.  
  93. class i2c_device:
  94. def __init__(self, addr, port=I2CBUS):
  95. self.addr = addr
  96. self.bus = smbus.SMBus(port)
  97.  
  98. # Write a single command
  99. def write_cmd(self, cmd):
  100. self.bus.write_byte(self.addr, cmd)
  101. sleep(0.0001)
  102.  
  103. # Write a command and argument
  104. def write_cmd_arg(self, cmd, data):
  105. self.bus.write_byte_data(self.addr, cmd, data)
  106. sleep(0.0001)
  107.  
  108. # Write a block of data
  109. def write_block_data(self, cmd, data):
  110. self.bus.write_block_data(self.addr, cmd, data)
  111. sleep(0.0001)
  112.  
  113. # Read a single byte
  114. def read(self):
  115. return self.bus.read_byte(self.addr)
  116.  
  117. # Read
  118. def read_data(self, cmd):
  119. return self.bus.read_byte_data(self.addr, cmd)
  120.  
  121. # Read a block of data
  122. def read_block_data(self, cmd):
  123. return self.bus.read_block_data(self.addr, cmd)
  124.  
  125.  
  126. # commands
  127. LCD_CLEARDISPLAY = 0x01
  128. LCD_RETURNHOME = 0x02
  129. LCD_ENTRYMODESET = 0x04
  130. LCD_DISPLAYCONTROL = 0x08
  131. LCD_CURSORSHIFT = 0x10
  132. LCD_FUNCTIONSET = 0x20
  133. LCD_SETCGRAMADDR = 0x40
  134. LCD_SETDDRAMADDR = 0x80
  135.  
  136. # flags for display entry mode
  137. LCD_ENTRYRIGHT = 0x00
  138. LCD_ENTRYLEFT = 0x02
  139. LCD_ENTRYSHIFTINCREMENT = 0x01
  140. LCD_ENTRYSHIFTDECREMENT = 0x00
  141.  
  142. # flags for display on/off control
  143. LCD_DISPLAYON = 0x04
  144. LCD_DISPLAYOFF = 0x00
  145. LCD_CURSORON = 0x02
  146. LCD_CURSOROFF = 0x00
  147. LCD_BLINKON = 0x01
  148. LCD_BLINKOFF = 0x00
  149.  
  150. # flags for display/cursor shift
  151. LCD_DISPLAYMOVE = 0x08
  152. LCD_CURSORMOVE = 0x00
  153. LCD_MOVERIGHT = 0x04
  154. LCD_MOVELEFT = 0x00
  155.  
  156. # flags for function set
  157. LCD_8BITMODE = 0x10
  158. LCD_4BITMODE = 0x00
  159. LCD_2LINE = 0x08
  160. LCD_1LINE = 0x00
  161. LCD_5x10DOTS = 0x04
  162. LCD_5x8DOTS = 0x00
  163.  
  164. # flags for backlight control
  165. LCD_BACKLIGHT = 0x08
  166. LCD_NOBACKLIGHT = 0x00
  167.  
  168. En = 0b00000100 # Enable bit
  169. Rw = 0b00000010 # Read/Write bit
  170. Rs = 0b00000001 # Register select bit
  171.  
  172. class lcd:
  173. #initializes objects and lcd
  174. def __init__(self):
  175. self.lcd_device = i2c_device(ADDRESS)
  176.  
  177. self.lcd_write(0x03)
  178. self.lcd_write(0x03)
  179. self.lcd_write(0x03)
  180. self.lcd_write(0x02)
  181.  
  182. self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
  183. self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON)
  184. self.lcd_write(LCD_CLEARDISPLAY)
  185. self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
  186. sleep(0.2)
  187.  
  188.  
  189. # clocks EN to latch command
  190. def lcd_strobe(self, data):
  191. self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
  192. sleep(.0005)
  193. self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))
  194. sleep(.0001)
  195.  
  196. def lcd_write_four_bits(self, data):
  197. self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
  198. self.lcd_strobe(data)
  199.  
  200. # write a command to lcd
  201. def lcd_write(self, cmd, mode=0):
  202. self.lcd_write_four_bits(mode | (cmd & 0xF0))
  203. self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))
  204.  
  205. # write a character to lcd (or character rom) 0x09: backlight | RS=DR<
  206. # works!
  207. def lcd_write_char(self, charvalue, mode=1):
  208. self.lcd_write_four_bits(mode | (charvalue & 0xF0))
  209. self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0))
  210.  
  211. # put string function with optional char positioning
  212. def lcd_display_string(self, string, line=1, pos=0):
  213. if line == 1:
  214. pos_new = pos
  215. elif line == 2:
  216. pos_new = 0x40 + pos
  217. elif line == 3:
  218. pos_new = 0x14 + pos
  219. elif line == 4:
  220. pos_new = 0x54 + pos
  221.  
  222. self.lcd_write(0x80 + pos_new)
  223.  
  224. for char in string:
  225. self.lcd_write(ord(char), Rs)
  226.  
  227. # clear lcd and set to home
  228. def lcd_clear(self):
  229. self.lcd_write(LCD_CLEARDISPLAY)
  230. self.lcd_write(LCD_RETURNHOME)
  231.  
  232. # define backlight on/off (lcd.backlight(1); off= lcd.backlight(0)
  233. def backlight(self, state): # for state, 1 = on, 0 = off
  234. if state == 1:
  235. self.lcd_device.write_cmd(LCD_BACKLIGHT)
  236. elif state == 0:
  237. self.lcd_device.write_cmd(LCD_NOBACKLIGHT)
  238.  
  239. # add custom characters (0 - 7)
  240. def lcd_load_custom_chars(self, fontdata):
  241. self.lcd_write(0x40);
  242. for char in fontdata:
  243. for line in char:
  244. self.lcd_write_char(line)
Add Comment
Please, Sign In to add comment