Advertisement
VGToolBox

Key Card Lock

Aug 11th, 2013
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. --rom/programs/http/pastebin get xdqCUyim keycard
  2.  
  3. local SIDES = {"left", "right", "top", "bottom", "front", "back"}
  4.  
  5. local OUTPUT_SIDE = "bottom"
  6.  
  7. local COMBO_SUCCESS = {11,13}
  8. local COMBO_FAIL = {4,3}
  9.  
  10. local passDoorOpen = false
  11. local PASS_DOOR = {}
  12. PASS_DOOR.CLOSE = colours.green
  13. PASS_DOOR.OPEN = colours.red
  14. PASS_DOOR.PULSES = 4
  15.  
  16. local note
  17.  
  18. local diskIDs = {}
  19. diskIDs[1] = true
  20. diskIDs[2] = true
  21.  
  22.  
  23. local function getNoteBlock()
  24.     for i, v in ipairs(SIDES) do
  25.         if peripheral.getType(v) == "note" then
  26.             return peripheral.wrap(v)
  27.         end
  28.     end
  29. end
  30.  
  31. --[[
  32. local function getDisks()
  33.     local disks = {}
  34.     for i, v in ipairs(SIDES) do
  35.         if peripheral.getType(v) == "disk" then
  36.             table.insert(disks, peripheral.wrap(v))
  37.         end
  38.     end
  39.  
  40.     return disks
  41. end
  42. ]]
  43.  
  44. local function clearEvents()
  45.     os.queueEvent("clear")
  46.     while true do
  47.         e = os.pullEvent()
  48.         if e == "clear" then
  49.             break
  50.         end
  51.     end
  52. end
  53.  
  54. local function pulseRS(side, n, colour)
  55.    
  56.     if colour then
  57.         for i = 1, n do
  58.             rs.setBundledOutput(side, colours.combine(rs.getBundledOutput(side), colour))
  59.             sleep(0.45)
  60.             rs.setBundledOutput(side, colours.subtract(rs.getBundledOutput(side), colour))
  61.             sleep(0.45)
  62.         end
  63.     else
  64.         for i = 1, n do
  65.             rs.setOutput(side, true)
  66.             sleep(0.45)
  67.             rs.setOutput(side, false)
  68.             sleep(0.45)
  69.         end
  70.     end
  71. end
  72.  
  73. local function playNote(notes)
  74.     for i = 1, table.getn(notes) do
  75.         note.playNote(0,notes[i])
  76.         sleep(0.2)
  77.     end
  78. end
  79.  
  80. local function moveDoor(door, open)
  81.     if open then
  82.         pulseRS(OUTPUT_SIDE, door.PULSES, door.OPEN)
  83.     else
  84.         pulseRS(OUTPUT_SIDE, door.PULSES, door.CLOSE)
  85.     end
  86. end
  87.  
  88. local function checkDisk(side)
  89.     if diskIDs[disk.getID(side)] then
  90.         return true
  91.     end
  92.     return false
  93. end
  94.  
  95. local function main()
  96.  
  97. local eventTimer
  98.    
  99.     note = getNoteBlock()
  100.  
  101.     print("closing door")
  102.     moveDoor(PASS_DOOR)
  103.  
  104.     sleep(1)
  105.  
  106.     clearEvents()
  107.  
  108.     while true do
  109.  
  110.         eventTimer = os.startTimer(6)
  111.  
  112.         e, side = os.pullEvent()
  113.         if e == "disk" then
  114.             print("disk in")
  115.             if(checkDisk(side)) then
  116.                 print("openening door")
  117.                 sleep(0.2)
  118.                 playNote(COMBO_SUCCESS)
  119.                 disk.eject(side)
  120.                 moveDoor(PASS_DOOR, true)
  121.                 sleep(5)
  122.                 os.reboot()
  123.             else
  124.                 playNote(COMBO_FAIL)
  125.                 disk.eject(side)
  126.  
  127.             end
  128.            
  129.         end
  130.  
  131.     end
  132.  
  133. end
  134.  
  135. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement