Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function GetRFIDWriters()
- local writers = {}
- for _,name in pairs(peripheral.getNames()) do
- if string.match(name, "rfid writer") then
- writers[#writers + 1] = peripheral.wrap(name)
- end
- end
- return writers
- end
- function CriticalError(msg)
- local oldBG = term.getBackgroundColor()
- local oldFG = term.getTextColor()
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- print(tostring(msg))
- sleep(2)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(oldFG)
- term.setBackgroundColor(oldBG)
- end
- local function MainDraw(writers, selected)
- term.clear()
- term.setCursorPos(1,1)
- local oldBG = term.getBackgroundColor()
- term.setBackgroundColor(colors.black)
- print("\t[UP up DOWN down TAB reload ENTER select]")
- print("Select a card writer to use: (".. #writers .. " available)\n")
- for index,_ in pairs(writers) do
- if selected == index then term.setBackgroundColor(colors.orange) end
- print(" Writer "..tostring(index))
- term.setBackgroundColor(colors.black)
- end
- term.setBackgroundColor(oldBG)
- end
- local function MenuWrite(writer)
- local priv = ""
- local name = ""
- local card = ""
- local label = ""
- local oldBG = term.getBackgroundColor()
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.lightGray)
- print("Writing card")
- print("")
- -- Get name
- repeat
- print("Enter name of card owner: ")
- term.setBackgroundColor(colors.black)
- name = read()
- term.setBackgroundColor(colors.lightGray)
- until name ~= ""
- -- Get privilages
- repeat
- print("Enter privilige level [0 1 2 3]: ")
- term.setBackgroundColor(colors.black)
- priv = read()
- term.setBackgroundColor(colors.lightGray)
- until priv == "0" or priv == "1" or priv == "2" or priv == "3"
- -- build card data
- card = tostring(name) .. " " .. tostring(priv) .. " " .. "2143"
- label = "Card - "..string.sub(tostring(name),1,14)
- -- write card
- ret, error_msg = writer.encode(card, label)
- if not(ret) then CriticalError(card.."\n"..label.."\n"..error_msg) end
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(oldBG)
- end
- local function Main()
- local writers
- local with_card
- local selected = 1
- while true do
- writers = GetRFIDWriters()
- with_card = {}
- for _,writer in pairs(writers) do
- if writer.getProgress() == -1 and writer.isPresent() and not(writer.isCoded()) then
- with_card[#with_card + 1] = writer
- end
- end
- while true do
- if #with_card == 0 then
- CriticalError("No usable card readers found,\nInsert card into empty one, if you have done that,\ncheck that the card is empty.")
- break
- end
- MainDraw(with_card, selected)
- -- run pull event, if no event for a second, then continue
- local event, key, x, y = os.pullEvent()
- if event == "key" then
- if key == keys.up then
- selected = selected - 1
- if selected < 1 then selected = #with_card end
- elseif key == keys.down then
- selected = selected + 1
- if selected > #with_card then selected = 1 end
- elseif key == keys.tab then
- break
- elseif key == keys.enter then
- if with_card[selected].getProgress() == -1 and with_card[selected].isPresent() and not(with_card[selected].isCoded()) then
- MenuWrite(with_card[selected])
- else
- CriticalError("Problem with slot " .. selected)
- end
- end
- break
- end
- end
- end
- end
- -- We clear screen and start the program
- term.clear()
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement