SHARE
TWEET

ComputerCraft Tutorial: touchpanel_password_door_0_1

hevohevo Feb 10th, 2014 3,121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ##################################
  2. -- password door with touch panel
  3. --  (using h2touchpanel API, http://pastebin.com/eK9BW600)
  4. -- version 0.1a
  5. -- http://hevohevo.hatenablog.com/
  6.  
  7. os.loadAPI("h2touchpanel")
  8.  
  9. -- config parameter
  10. local sideDoor = "right"
  11. local sideMon = "top"
  12. local pass = "12345"
  13.  
  14. -- ############### functions
  15. local tmp = ""
  16. local success_flag = false
  17.  
  18. function addStr(s)
  19.   tmp = tmp..tostring(s)
  20.   print('current: ',tmp)
  21. end
  22.  
  23. function clearStr()
  24.   tmp = ""
  25.   print('current: ',tmp)
  26. end
  27.  
  28. function certStr()
  29.   if tmp == pass then
  30.     success_flag =true
  31.     print('ok! opened the door.')
  32.   else
  33.     success_flag =false
  34.     print('bad! Please retry!')
  35.   end
  36.   tmp = ""
  37. end
  38.  
  39. -- ############### button_config_table: "name" and "cmd" are required.
  40. local bCfg = {}
  41. bCfg[1]={name="1", cmd=function() addStr(1) end}
  42. bCfg[2]={name="2", cmd=function() addStr(2) end}
  43. bCfg[3]={name="3", cmd=function() addStr(3) end}
  44. bCfg[4]={name="4", cmd=function() addStr(4) end}
  45. bCfg[5]={name="5", cmd=function() addStr(5) end}
  46. bCfg[6]={name="6", cmd=function() addStr(6) end}
  47. bCfg[7]={name="7", cmd=function() addStr(7) end}
  48. bCfg[8]={name="8", cmd=function() addStr(8) end}
  49. bCfg[9]={name="9", cmd=function() addStr(9) end}
  50. bCfg[10]={name="0", cmd=function() addStr(0) end}
  51. bCfg[11]={name="clr", cmd=clearStr}
  52. bCfg[12]={name="enter", cmd=certStr}
  53.  
  54. -- ############### main
  55. local mon = peripheral.wrap(sideMon)
  56.  
  57. local opt = {topPos=2, bottomPos=9}
  58. local p1 = h2touchpanel.makePanel(bCfg, 3, 4, mon, opt)
  59. p1:draw()
  60.  
  61. while true do
  62.   tmp = ""
  63.   success_flag = false
  64.   term.clear()
  65.   term.setCursorPos(1,1)
  66.   rs.setOutput(sideDoor, false)
  67.   print("Input password.")
  68.  
  69.   repeat
  70.     local event, b1 = p1:pullButtonPushEvent() -- b1 is the pushed button
  71.     b1:run() -- run the function of the pushed button.
  72.     p1:drawPushedButtonEffect(b1, 0.5) -- pushed button b1 is highlighted for 0.5sec.
  73.   until success_flag
  74.  
  75.   rs.setOutput(sideDoor, true)
  76.   sleep(5)
  77. end
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top