Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Test to demonstrat potential timer bug
- running = true
- -- config
- local timers = {"One", "Two", "Three", "Four"}
- local delay = false
- local printTimers = true
- local printMap = true
- -- End config
- local timerMap = {}
- -- returns the size of a data structure
- function size(input)
- local count = 0
- for _, _ in pairs(input) do
- count = count + 1
- end
- return count
- end
- function printInfo()
- print("Timer Test running. Press W to stop.")
- print("Press L to clear terminal.")
- print("Press T to turn on/off timer printing.")
- print("Press M to build a new map.")
- print("Press D to turn on/off delay.")
- print("Press H for this message.")
- end
- function buildTimerMap()
- timerMap = {}
- for timer = 1,#timers do
- -- start the timer and add to the map
- -- Key: timer ID returned from OS
- -- Value: number from timers table
- timerMap[os.startTimer(1)] = timers[timer]
- end
- end
- function handleTimer()
- local _, data = os.pullEvent("timer")
- if timerMap[data] == nil then return end
- -- start a new timer with the same value being transferred
- timerMap[os.startTimer(1)] = timerMap[data]
- if printTimers == true then
- print(data..": "..timerMap[data])
- end
- -- now delete the old timer
- timerMap[data] = nil
- if delay == true then
- os.sleep(0.05)
- end
- end
- function humanInteraction()
- local _, key = os.pullEvent("key_up")
- if key == keys.w then
- print("Interrupt detected. Closing program.")
- for timerID, _ in pairs(timerMap) do
- os.cancelTimer(timerID)
- end
- running = false
- elseif key == keys.l then
- term.clear()
- term.setCursorPos(1,1)
- elseif key == keys.t then
- if printTimers == true then
- printTimers = false
- print("Timer printing off.")
- elseif printTimers == false then
- printTimers = true
- print("Timer printing on.")
- end
- elseif key == keys.m then
- buildTimerMap()
- print("Map rebuilt.")
- elseif key == keys.d then
- if delay == true then
- delay = false
- print("Delay disabled.")
- else
- delay = true
- print("Delay enabled.")
- end
- elseif key == keys.s then
- for k,v in pairs(timerMap) do
- print(k..": "..v)
- end
- elseif key == keys.h then
- printInfo()
- end
- end
- -------------
- -- Main loop
- buildTimerMap()
- printInfo()
- while running do
- parallel.waitForAny(handleTimer, humanInteraction)
- end
Advertisement
Add Comment
Please, Sign In to add comment