Advertisement
tekkitak1

RFID_Writer

Apr 20th, 2022 (edited)
1,369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.06 KB | None | 0 0
  1. function GetRFIDWriters()
  2.     local writers = {}
  3.     for _,name in pairs(peripheral.getNames()) do
  4.         if string.match(name, "rfid writer") then
  5.             writers[#writers + 1] = peripheral.wrap(name)
  6.         end
  7.     end
  8.     return writers
  9. end
  10.  
  11. function CriticalError(msg)
  12.     local oldBG = term.getBackgroundColor()
  13.     local oldFG = term.getTextColor()
  14.     term.clear()
  15.     term.setCursorPos(1,1)
  16.     term.setBackgroundColor(colors.red)
  17.     term.setTextColor(colors.white)
  18.     print(tostring(msg))
  19.     sleep(2)
  20.     term.clear()
  21.     term.setCursorPos(1,1)
  22.     term.setTextColor(oldFG)
  23.     term.setBackgroundColor(oldBG)
  24. end
  25.  
  26. local function MainDraw(writers, selected)
  27.     term.clear()
  28.     term.setCursorPos(1,1)
  29.     local oldBG = term.getBackgroundColor()
  30.     term.setBackgroundColor(colors.black)
  31.     print("\t[UP up DOWN down TAB reload ENTER select]")
  32.     print("Select a card writer to use: (".. #writers .. " available)\n")
  33.     for index,_ in pairs(writers) do
  34.         if selected == index then term.setBackgroundColor(colors.orange) end
  35.         print(" Writer "..tostring(index))
  36.         term.setBackgroundColor(colors.black)
  37.     end
  38.     term.setBackgroundColor(oldBG)
  39. end
  40.  
  41. local function MenuWrite(writer)
  42.     local priv = ""
  43.     local name = ""
  44.     local card = ""
  45.     local label = ""
  46.     local oldBG = term.getBackgroundColor()
  47.     term.clear()
  48.     term.setCursorPos(1,1)
  49.     term.setBackgroundColor(colors.lightGray)
  50.     print("Writing card")
  51.     print("")
  52.     -- Get name
  53.     repeat
  54.         print("Enter name of card owner: ")
  55.         term.setBackgroundColor(colors.black)
  56.         name = read()
  57.         term.setBackgroundColor(colors.lightGray)
  58.     until name ~= ""
  59.     -- Get privilages
  60.     repeat
  61.         print("Enter privilige level [0 1 2 3]: ")
  62.         term.setBackgroundColor(colors.black)
  63.         priv = read()
  64.         term.setBackgroundColor(colors.lightGray)
  65.     until priv == "0" or priv == "1" or priv == "2" or priv == "3"
  66.    
  67.     -- build card data
  68.     card = tostring(name) .. " " .. tostring(priv) .. " " .. "2143"
  69.     label = "Card - "..string.sub(tostring(name),1,14)
  70.  
  71.     -- write card
  72.     ret, error_msg = writer.encode(card, label)
  73.     if not(ret) then CriticalError(card.."\n"..label.."\n"..error_msg) end
  74.     term.clear()
  75.     term.setCursorPos(1,1)
  76.     term.setBackgroundColor(oldBG)
  77. end
  78.  
  79.  
  80. local function Main()
  81.     local writers
  82.     local with_card
  83.     local selected = 1
  84.     while true do
  85.         writers = GetRFIDWriters()
  86.         with_card = {}
  87.         for _,writer in pairs(writers) do
  88.             if writer.getProgress() == -1 and writer.isPresent() and not(writer.isCoded()) then
  89.                 with_card[#with_card + 1] = writer
  90.             end
  91.         end
  92.         while true do
  93.             if #with_card == 0 then
  94.                 CriticalError("No usable card readers found,\nInsert card into empty one, if you have done that,\ncheck that the card is empty.")
  95.                 break
  96.             end
  97.             MainDraw(with_card, selected)
  98.             -- run pull event, if no event for a second, then continue
  99.             local event, key, x, y = os.pullEvent()
  100.             if event == "key" then
  101.                 if key == keys.up then
  102.                     selected = selected - 1
  103.                     if selected < 1 then selected = #with_card end
  104.                 elseif key == keys.down then
  105.                     selected = selected + 1
  106.                     if selected > #with_card then selected = 1 end
  107.                 elseif key == keys.tab then
  108.                     break
  109.                 elseif key == keys.enter then
  110.                     if with_card[selected].getProgress() == -1 and with_card[selected].isPresent() and not(with_card[selected].isCoded()) then
  111.                         MenuWrite(with_card[selected])
  112.                     else
  113.                         CriticalError("Problem with slot " .. selected)
  114.                     end
  115.                 end
  116.                 break
  117.             end
  118.         end
  119.     end
  120. end
  121.  
  122. -- We clear screen and start the program
  123. term.clear()
  124. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement