Advertisement
Guest User

advanced timer

a guest
Oct 10th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. --usage string
  4. usageString = "Clock & Timer\nParameters:\n\'clock\' to display current time,\n\'timer <ticks>\' to set timer\n"
  5. --magical constants
  6. --clock update in seconds
  7. clockUpdate = 1
  8. --low timer value in ticks
  9. timerLow = 40
  10.  
  11. --current server time on script startup
  12. startupTime = os.time()
  13. --resume table
  14. resumeT = {}
  15. --operation mode
  16. resumeT["mode"] = ""
  17. --precision in ticks (hardcoded)
  18. resumeT["precision"] = 20
  19. --timer goal setting in ticks
  20. resumeT["timerGoal"] = 0
  21. --last saved timer value in ticks
  22. resumeT["timerVal"] = 0
  23. --alarm side (hardcoded)
  24. resumeT["alarmSide"] = "top"
  25.  
  26. function ReadResume()
  27.     if not fs.exists("resume.f") then
  28.         print("Trying to resume without resume file, aborting...")
  29.         return
  30.     end
  31.     local file = fs.open("resume.f","r")
  32.     local sT = file.readAll()
  33.     resumeT = textutils.unserialize(sT)
  34.     file.close()
  35.     return
  36. end
  37.  
  38. function WriteResume()
  39.     local file = fs.open("resume.f","w")
  40.     local sT = textutils.serialize(resumeT)
  41.     file.write(sT)
  42.     file.close()
  43.     return
  44. end
  45.  
  46. --kiloticks in 24hr format as args
  47. function KiloTicksPassed(from, to)
  48.     if (from > to) then
  49.     --midnight
  50.     to = to+24.0   
  51.     end
  52.     return (to-from)
  53. end
  54.  
  55. function TimerFinish()
  56.     --message
  57.     term.setCursorPos(1,5)
  58.     print("ALARM")
  59.     --alarm
  60.     rs.setOutput(resumeT["alarmSide"], true)
  61.     --delete resume.f
  62.     fs.delete("resume.f")
  63.     print("\nPress any key...")
  64.     os.pullEvent("key")
  65.     rs.setOutput(resumeT["alarmSide"], false)
  66.     return
  67. end
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. if fs.exists("resume.f") then
  75.     --resuming
  76.     ReadResume()
  77. elseif #tArgs < 1 then
  78.     --running by hand without args
  79.     print( usageString )
  80. end
  81.  
  82. if (tArgs[1] == "clock") then
  83.     resumeT["mode"] = "clock"
  84. end
  85.  
  86. if (tArgs[1] == "timer") then
  87.     if #tArgs < 2 then
  88.         print(usageString)
  89.         return
  90.     else
  91.         resumeT["mode"] = "timer"
  92.         resumeT["timerGoal"] = tArgs[2]
  93.     end
  94. end
  95.  
  96. WriteResume()
  97.  
  98. if (resumeT["mode"] == "clock") then
  99.     while (true) do
  100.         term.clear()
  101.         term.setCursorPos(1,1)
  102.         currentTime = textutils.formatTime(os.time(), true)
  103.         print(currentTime)
  104.         sleep(clockUpdate)
  105.     end
  106. end
  107.  
  108. if (resumeT["mode"] == "timer") then
  109.     local ticksToGo = resumeT["timerGoal"] - resumeT["timerVal"]
  110.     --check if time interval has already passed
  111.     if (ticksToGo < timerLow) and (KiloTicksPassed(startupTime,os.time())*1000>ticksToGo) then
  112.         TimerFinish()
  113.         return
  114.     end
  115.  
  116.     local curTime = 0.0
  117.    
  118.     while (true) do
  119.         curTime = os.time()
  120.         WriteResume()
  121.         ticksToGo = resumeT["timerGoal"] - resumeT["timerVal"]
  122.         if ticksToGo < resumeT["precision"] then
  123.             TimerFinish()
  124.             return     
  125.         else
  126.        
  127.         term.clear()
  128.         term.setCursorPos(1,1)
  129.         print("Passed:")
  130.         print(resumeT["timerVal"].." ticks or "..(resumeT["timerVal"]/60).." seconds")
  131.  
  132.         print("Remaining:")
  133.         print(ticksToGo.." ticks or "..(ticksToGo/60).." seconds")
  134.        
  135. --      os.startTimer(resumeT["precision"])
  136. --      os.pullEvent(timer)
  137.         sleep(resumeT["precision"]/20)
  138.         resumeT["timerVal"] = resumeT["timerVal"] + KiloTicksPassed(curTime,os.time())*1000
  139.         end
  140.     end
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement