svennp

touch monitor lock

Feb 24th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1. -- svennp is awesome!
  2.  
  3. --variables
  4.  
  5. for k,v in pairs(rs.getSides()) do
  6.     if peripheral.getType(v) == "monitor" then
  7.         mon = peripheral.wrap(v)
  8.         break
  9.     end
  10. end
  11. side = "front"
  12. unterminatable = false
  13. timeout = 3
  14. input = ""
  15. if fs.exists(".pass") then
  16.     f = fs.open(".pass","r")
  17.     pass = f.readLine()
  18.     f.close()
  19. else write("input new code (4 digits): ")
  20.     pass = read("*")
  21.     if tonumber(pass) and #pass == 4 then
  22.         f = fs.open(".pass","w")
  23.         f.writeLine(pass)
  24.         f.close()
  25.     else error("please use numbers only!  (4 digits!)")
  26.     end
  27. end
  28.  
  29. --functions
  30.  
  31. function resetcolor()
  32.     mon.setTextColor(colors.white)
  33.     mon.setBackgroundColor(colors.black)
  34. end
  35.  
  36. function numcolor(num)
  37.     mon.setTextColor(colors.blue)
  38.     mon.setBackgroundColor(colors.black)
  39.     mon.write(num)
  40. end
  41.  
  42. function linecolor(line)
  43.     mon.setTextColor(colors.brown)
  44.     mon.setBackgroundColor(colors.brown)
  45.     mon.write(line)
  46. end
  47.  
  48. function getnum()
  49.     e,p1,p2,p3 = os.pullEvent("monitor_touch")
  50.     if p3 == 2 then
  51.         if p2 == 2 then
  52.             return 1
  53.         elseif p2 == 4 then
  54.             return 2
  55.         elseif p2 == 6 then
  56.             return 3
  57.         end
  58.     elseif p3 == 3 then
  59.         if p2 == 2 then
  60.             return 4
  61.         elseif p2 == 4 then
  62.             return 5
  63.         elseif p2 == 6 then
  64.             return 6
  65.         end
  66.     elseif p3 == 4 then
  67.         if p2 == 2 then
  68.             return 7
  69.         elseif p2 == 4 then
  70.             return 8
  71.         elseif p2 == 6 then
  72.             return 9
  73.         end
  74.     elseif p3 == 5 then
  75.         if p2 == 1 or p2 == 2 or p2 == 3 then
  76.             return "cancel"
  77.         elseif p2 == 5 or p2 == 6 or p2 == 7 then
  78.             return "ok"
  79.         end
  80.     else return getnum()
  81.     end
  82. end
  83.  
  84. function printinterface()
  85.     mon.setCursorPos(1,1)
  86.     resetcolor()
  87.     mon.write(">      ")
  88.     mon.setCursorPos(1,2)
  89.     linecolor("|") numcolor("1") linecolor("|") numcolor("2") linecolor("|") numcolor("3") linecolor("|")
  90.     mon.setCursorPos(1,3)
  91.     linecolor("|") numcolor("4") linecolor("|") numcolor("5") linecolor("|") numcolor("6") linecolor("|")
  92.     mon.setCursorPos(1,4)
  93.     linecolor("|") numcolor("7") linecolor("|") numcolor("8") linecolor("|") numcolor("9") linecolor("|")
  94.     mon.setCursorPos(1,5)
  95.     mon.setBackgroundColor(colors.red) mon.write("   ") mon.setBackgroundColor(colors.black) mon.write(" ") mon.setBackgroundColor(colors.green) mon.write("   ")
  96. end
  97.  
  98. function printcorrect()
  99.     mon.setBackgroundColor(colors.green)
  100.     mon.setTextColor(colors.green)
  101.     for i = 1,5 do
  102.         mon.setCursorPos(1,i)
  103.         mon.write("       ")
  104.     end
  105.     resetcolor()
  106. end
  107.  
  108. function printwrong()
  109.     mon.setBackgroundColor(colors.red)
  110.     mon.setTextColor(colors.red)
  111.     for i = 1,5 do
  112.         mon.setCursorPos(1,i)
  113.         mon.write("       ")
  114.     end
  115.     resetcolor()
  116. end
  117.  
  118. -- other stuff
  119.  
  120. term.clear()
  121. term.setCursorPos(1,1)
  122. print("running svennp's lock program")
  123.  
  124. if unterminatable then
  125.     print("termination of program not possible!")
  126. else
  127.     print("termination of program possible!")
  128. end
  129.  
  130. if unterminatable then
  131.     os.pullEvent = os.oullEventRaw
  132. end
  133.  
  134. --main loop
  135.  
  136. printinterface()
  137. while true do
  138.     mon.setCursorPos(1,1)
  139.     resetcolor()
  140.     mon.write(">      ")
  141.     mon.setCursorPos(3,1)
  142.  
  143.     for i = 1,#input do
  144.         mon.write("*")
  145.     end
  146.  
  147.     q = nil
  148.     while q == nil do
  149.         q = getnum()
  150.     end
  151.  
  152.     if type(q) == "string" then
  153.         if q == "cancel" then
  154.             input = ""
  155.         elseif q == "ok" then
  156.             if input == pass then
  157.                 printcorrect()
  158.                 rs.setOutput(side,true)
  159.                 sleep(timeout)
  160.                 rs.setOutput(side,false)
  161.             else
  162.                 printwrong()
  163.                 sleep(timeout)
  164.             end
  165.             input = ""
  166.             printinterface()
  167.         end
  168.     elseif type(q) == "number" then
  169.         if #input < 4 then
  170.             input = input..tostring(q)
  171.         end
  172.     end
  173. end
Advertisement
Add Comment
Please, Sign In to add comment