mrWhiskasss

программа для двери с пинкодом

Jul 27th, 2021 (edited)
485
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local door = require("component").os_rolldoorcontroller
  2.  
  3. keypad = require("component").os_keypad
  4. event = require("event")
  5.  
  6. local pin = "5797"
  7. local keypadInput = ""
  8.  
  9. -- set this to true if you want to run the script as daemon
  10. local runScriptInBackground = false
  11.  
  12. function updateDisplay()
  13.     local displayString = ""
  14.     for i=1,#keypadInput do
  15.         displayString = displayString .. "*"
  16.     end
  17.  
  18.     keypad.setDisplay(displayString, 7)
  19. end
  20.  
  21. function checkPin()
  22.     if keypadInput == pin then
  23.         keypad.setDisplay("granted", 2)
  24.     door.toggle()
  25.     else
  26.         keypad.setDisplay("denied", 4)
  27.     end    
  28.     keypadInput = ""
  29.     os.sleep(1)
  30. end
  31.  
  32. function keypadEvent(eventName, address, button, button_label)
  33.     print("button pressed: " )
  34.  
  35.     if button_label == "*" then
  36.         -- remove last character from input cache
  37.         keypadInput = string.sub(keypadInput, 1, -2)
  38.     elseif button_label == "#" then
  39.         -- check the pin when the user confirmed the input
  40.         checkPin()
  41.     else
  42.         -- add key to input cache if none of the above action apply
  43.         keypadInput = keypadInput .. button_label
  44.     end
  45.  
  46.     updateDisplay()  
  47. end
  48.  
  49. -- listen to keypad events
  50. event.listen("keypad", keypadEvent)
  51.  
  52. -- clear keypad display
  53. keypad.setDisplay("")
  54.  
  55.  
  56. if not runScriptInBackground then
  57.     -- sleep until the user interrupts the program with CTRL + C
  58.     local stopMe = false
  59.     event.listen("interrupted", function() stopMe = true; end)
  60.     while not stopMe do os.sleep(0.1) end
  61.  
  62.     -- ignore keypad events on exit
  63.     event.ignore("keypad", keypadEvent)
  64.  
  65.     -- show that the keypad is inactive
  66.     keypad.setDisplay("inactive", 6)
  67. end
RAW Paste Data