Jetpack_Maniac

Timer Test

Jul 7th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. -- Test to demonstrat potential timer bug
  2.  
  3. running = true
  4.  
  5. -- config
  6. local timers = {"One", "Two", "Three", "Four"}
  7. local delay = false
  8. local printTimers = true
  9. local printMap = true
  10. -- End config
  11.  
  12. local timerMap = {}
  13.  
  14. -- returns the size of a data structure
  15. function size(input)
  16.   local count = 0
  17.   for _, _ in pairs(input) do
  18.     count = count + 1
  19.   end
  20.   return count
  21. end
  22.  
  23. function printInfo()
  24.     print("Timer Test running.  Press W to stop.")
  25.     print("Press L to clear terminal.")
  26.     print("Press T to turn on/off timer printing.")
  27.     print("Press M to build a new map.")
  28.     print("Press D to turn on/off delay.")
  29.     print("Press H for this message.")
  30. end
  31.  
  32. function buildTimerMap()
  33.     timerMap = {}
  34.     for timer = 1,#timers do
  35.         -- start the timer and add to the map
  36.         -- Key: timer ID returned from OS
  37.         -- Value: number from timers table
  38.         timerMap[os.startTimer(1)] = timers[timer]
  39.     end
  40. end
  41.  
  42. function handleTimer()
  43.     local _, data = os.pullEvent("timer")
  44.     if timerMap[data] == nil then return end
  45.     -- start a new timer with the same value being transferred
  46.     timerMap[os.startTimer(1)] = timerMap[data]
  47.     if printTimers == true then
  48.         print(data..": "..timerMap[data])
  49.     end
  50.     -- now delete the old timer
  51.     timerMap[data] = nil
  52.     if delay == true then
  53.         os.sleep(0.05)
  54.     end
  55. end
  56.  
  57. function humanInteraction()
  58.     local _, key = os.pullEvent("key_up")
  59.     if key == keys.w then
  60.         print("Interrupt detected. Closing program.")
  61.         for timerID, _ in pairs(timerMap) do
  62.             os.cancelTimer(timerID)
  63.         end
  64.         running = false
  65.     elseif key == keys.l then
  66.         term.clear()
  67.         term.setCursorPos(1,1)
  68.     elseif key == keys.t then
  69.         if printTimers == true then
  70.             printTimers = false
  71.             print("Timer printing off.")
  72.         elseif printTimers == false then
  73.             printTimers = true
  74.             print("Timer printing on.")
  75.         end
  76.     elseif key == keys.m then
  77.         buildTimerMap()
  78.         print("Map rebuilt.")
  79.     elseif key == keys.d then
  80.         if delay == true then
  81.             delay = false
  82.             print("Delay disabled.")
  83.         else
  84.             delay = true
  85.             print("Delay enabled.")
  86.         end
  87.     elseif key == keys.s then
  88.         for k,v in pairs(timerMap) do
  89.             print(k..": "..v)
  90.         end
  91.     elseif key == keys.h then
  92.         printInfo()
  93.     end
  94. end
  95.  
  96. -------------
  97. -- Main loop
  98.  
  99. buildTimerMap()
  100. printInfo()
  101.  
  102. while running do
  103.     parallel.waitForAny(handleTimer, humanInteraction)
  104. end
Advertisement
Add Comment
Please, Sign In to add comment