Advertisement
Guest User

timer

a guest
May 31st, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1. os.loadAPI("button")
  2. rednet.open("right")
  3. m = peripheral.wrap("top")
  4. local clockComputers = {}
  5. local timerPaused = false
  6. local minTime = 30
  7. local secTime = 0
  8.  
  9. local min
  10. local sec
  11.  
  12. function mainMenu()
  13.   m.clear()
  14.   button.label(11,1,"Welcome to WRENCHED!")
  15.   button.setTable("Start Timer",startTimer,"",3,18,6,8)
  16.   button.setTable("Pause Timer",pauseTimer,"",22,37,6,8)
  17.   button.setTable("Reset Timer",resetTimer,"",3,18,12,14)
  18.   button.setTable("Edit Timer" ,editTimer ,"",22,37,12,14)
  19.   button.screen()
  20. end
  21.  
  22. function startTimer()
  23.   timerPaused = false
  24.   button.flash("Start Timer")
  25.   button.setButton("Pause Timer", false)
  26.   runTimer()
  27. end
  28.  
  29. function pauseTimer()
  30.   button.toggleButton("Pause Timer")
  31.   timerPaused = not timerPaused
  32.   if not timerPaused then
  33.     runTimer()
  34.   end
  35. end
  36.  
  37. function resetTimer()
  38.   timerPaused = true
  39.   button.toggleButton("Reset Timer")
  40.   findClocks()
  41.   button.setButton("Pause Timer", false)
  42.   button.toggleButton("Reset Timer")
  43.   min = minTime
  44.   sec = secTime
  45.   sendTime()
  46. end
  47.  
  48. function editTimer()
  49.   timerPaused = true
  50.   button.toggleButton("Edit Timer")
  51.   findClocks()
  52.   button.setButton("Pause Timer", false)
  53.   button.toggleButton("Edit Timer")
  54.   min = read()
  55.   sec = read()
  56.   sendTime()
  57. end
  58.  
  59. function findClocks()
  60.   rednet.broadcast("IsClock")
  61.   doneClocks = false
  62.  
  63.   while not doneClocks do
  64.     local id,msg,dist = rednet.receive(0.25)
  65.     if not id then
  66.       doneClocks = true
  67.     else
  68.       print("Clock Found: "..id)
  69.       clockComputers[#clockComputers+1] = id
  70.     end
  71.   end
  72. end
  73.  
  74. function sendTime()
  75.   local data = {}
  76.   data["min"] = min
  77.   data["sec"] = sec
  78.   msg = textutils.serialize(data)
  79.   --print("Sending: "..msg)
  80.   for id = 1,#clockComputers do
  81.     --print(id)
  82.     rednet.send(clockComputers[id],msg)
  83.   end
  84.   button.label(10,3, "Time Remaining: "..string.format("%02d:%02d",min,sec))
  85. end
  86.  
  87. function runTimer()
  88.   while min >=0 do
  89.     while sec >0 do
  90.       --getClick()
  91.       if not timerPaused then
  92.         sec = sec - 1
  93.       else
  94.         break
  95.       end
  96.       sendTime()
  97.       getClick()
  98.     end
  99.     --getClick()
  100.     if not timerPaused then
  101.       if min > 0 then
  102.         min = min-1
  103.         sec = 59
  104.       end
  105.     else
  106.       break
  107.     end
  108.     sendTime()
  109.     getClick()
  110.   end
  111. end
  112.  
  113. function getClick()
  114.   local timerCode = 0
  115.   timerCode = os.startTimer(1)
  116.   local event,side,x,y
  117.   repeat
  118.     event,side,x,y = os.pullEvent()
  119.   until side == timerCode or event == "monitor_touch"
  120.   if event == "monitor_touch" then
  121.     button.checkxy(x,y)
  122.   end
  123. end
  124.  
  125. mainMenu()
  126. --findClocks()
  127. resetTimer()
  128. while true do
  129.   getClick()
  130. end
  131. --runTimer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement