Advertisement
drpepper240

advanced timer

Oct 10th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.20 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. --usage string
  4. usageString = "Clock & Timer\nParameters:\n\'clock\' to display current time,\n\'timer <seconds>\' 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. --returns time as days:H:mm:ss
  70. function PrintReadableTime(kiloticks)
  71.     return(math.floor(kiloticks/24.0)..":"..textutils.formatTime(kiloticks%24, true)..":"..(kiloticks%24*50))
  72. end
  73.  
  74.  
  75.  
  76.  
  77. if fs.exists("resume.f") then
  78.     --resuming
  79.     ReadResume()
  80. elseif #tArgs < 1 then
  81.     --running by hand without args
  82.     print( usageString )
  83. end
  84.  
  85. if (tArgs[1] == "clock") then
  86.     resumeT["mode"] = "clock"
  87. end
  88.  
  89. if (tArgs[1] == "timer") then
  90.     if #tArgs < 2 then
  91.         print(usageString)
  92.         return
  93.     else
  94.         resumeT["mode"] = "timer"
  95.         resumeT["timerGoal"] = tArgs[2]*20
  96.     end
  97. end
  98.  
  99. WriteResume()
  100.  
  101. if (resumeT["mode"] == "clock") then
  102.     while (true) do
  103.         term.clear()
  104.         term.setCursorPos(1,1)
  105.         currentTime = textutils.formatTime(os.time(), true)
  106.         print(currentTime)
  107.         sleep(clockUpdate)
  108.     end
  109. end
  110.  
  111. if (resumeT["mode"] == "timer") then
  112.     local ticksToGo = resumeT["timerGoal"] - resumeT["timerVal"]
  113.     --check if time interval has already passed
  114.     if (ticksToGo < timerLow) and (KiloTicksPassed(startupTime,os.time())*1000>ticksToGo) then
  115.         TimerFinish()
  116.         return
  117.     end
  118.  
  119.     local curTime = 0.0
  120.    
  121.     while (true) do
  122.         curTime = os.time()
  123.         WriteResume()
  124.         ticksToGo = resumeT["timerGoal"] - resumeT["timerVal"]
  125.         if ticksToGo < resumeT["precision"] then
  126.             TimerFinish()
  127.             return     
  128.         else
  129.        
  130.         term.clear()
  131.         term.setCursorPos(1,1)
  132.         print("Passed:")
  133.         print(PrintReadableTime(resumeT["timerVal"]/1000).." or "..resumeT["timerVal"]/20.." seconds")
  134.  
  135.         print("Remaining:")
  136.         print(PrintReadableTime(ticksToGo).." or "..(ticksToGo/20).." seconds")
  137.        
  138. --      os.startTimer(resumeT["precision"])
  139. --      os.pullEvent(timer)
  140.         sleep(resumeT["precision"]/20)
  141.         resumeT["timerVal"] = resumeT["timerVal"] + KiloTicksPassed(curTime,os.time())*1000
  142.         end
  143.     end
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement