Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require "component"
- local keypad1 = component.proxy("80a")
- local keypad2 = component.proxy("637")
- event = require("event")
- local door = require("component").os_door
- local pin = "1111"
- local keypadInput = ""
- -- set this to true if you want to run the script as daemon
- local runScriptInBackground = false
- function myFunction(eventName, address, playerName, cardData, cardUniqueId, isCardLocked, side)
- local door = require("component").os_door
- print("player " .. playerName .. " used card " .. cardUniqueId .. ", data: хххххххх")
- if ((cardData) == ("1111")) then
- door.toggle()
- end
- end
- function updateDisplay()
- local displayString = ""
- for i=1,#keypadInput do
- displayString = displayString .. "*"
- end
- keypad1.setDisplay(displayString, 7)
- keypad2.setDisplay(displayString, 7)
- end
- function checkPin()
- local door = require("component").os_door
- if keypadInput == pin then
- keypad1.setDisplay("granted", 2)
- keypad2.setDisplay("granted", 2)
- door.toggle()
- else
- keypad1.setDisplay("denied", 4)
- keypad2.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
- -- subscribe to the event "magData" with myFunction()
- event.listen("magData", myFunction)
- -- listen to keypad events
- event.listen("keypad", keypadEvent)
- -- wait for an interrupt before closing the script (CTRL + C)
- event.pull("interrupted")
- -- unsubscribe from the event bus
- event.ignore("magData", myFunction)
- -- clear keypad display
- keypad1.setDisplay("")
- keypad2.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
- keypad1.setDisplay("inactive", 6)
- keypad2.setDisplay("inactive", 6)
- end
Advertisement
Add Comment
Please, Sign In to add comment