Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local door = require("component").os_rolldoorcontroller
- keypad = require("component").os_keypad
- event = require("event")
- local pin = "5797"
- local keypadInput = ""
- -- set this to true if you want to run the script as daemon
- local runScriptInBackground = false
- function updateDisplay()
- local displayString = ""
- for i=1,#keypadInput do
- displayString = displayString .. "*"
- end
- keypad.setDisplay(displayString, 7)
- end
- function checkPin()
- if keypadInput == pin then
- keypad.setDisplay("granted", 2)
- door.toggle()
- else
- keypad.setDisplay("denied", 4)
- end
- keypadInput = ""
- os.sleep(1)
- end
- function keypadEvent(eventName, address, button, button_label)
- print("button pressed: " )
- if button_label == "*" then
- -- remove last character from input cache
- keypadInput = string.sub(keypadInput, 1, -2)
- elseif button_label == "#" then
- -- check the pin when the user confirmed the input
- checkPin()
- else
- -- add key to input cache if none of the above action apply
- keypadInput = keypadInput .. button_label
- end
- updateDisplay()
- end
- -- listen to keypad events
- event.listen("keypad", keypadEvent)
- -- clear keypad display
- keypad.setDisplay("")
- if not runScriptInBackground then
- -- sleep until the user interrupts the program with CTRL + C
- local stopMe = false
- event.listen("interrupted", function() stopMe = true; end)
- while not stopMe do os.sleep(0.1) end
- -- ignore keypad events on exit
- event.ignore("keypad", keypadEvent)
- -- show that the keypad is inactive
- keypad.setDisplay("inactive", 6)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement