Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variable definitions
- local monitor, screenw, screenh
- local args = {...}
- local sides = {"left", "right", "top", "bottom", "front", "back"};
- local timing = false
- local timer = 0
- local best = -1
- local starttime
- local function getDeviceSide(deviceType)
- for i,side in pairs(sides) do
- if (peripheral.isPresent(side)) then
- if (peripheral.getType(side)) == string.lower(deviceType) then
- return side;
- end
- end
- end
- end
- local function displayTime(value)
- if (value) then
- print(value)
- term.redirect(monitor)
- monitor.setTextScale(5)
- monitor.setCursorPos(2, 2)
- monitor.clearLine()
- --write(string.format("%d%d.%d%d", value))
- write(value)
- term.restore()
- end
- end
- local function displayBest(value)
- if (value) then
- term.redirect(monitor)
- monitor.setTextScale(3)
- monitor.setCursorPos(1, 4)
- monitor.clearLine()
- --write(string.format("%d%d.%d%d", value))
- write("Best: " .. tostring(value))
- term.restore()
- end
- end
- local function mainLoop()
- while true do
- if (rs.getInput("top")) then
- if (timing) then
- local endtime = os.clock()
- final = endtime - starttime
- print("final: " .. tostring(final))
- if (final > 1) then -- ignore signals until the second second.
- if (best == -1 or final < best) then
- best = final
- print("New best: " .. tostring(best))
- displayBest(best)
- end
- timing = false
- displayTime(final)
- sleep(1)
- end
- else
- --monitor.clear()
- starttime = os.clock()
- timing = true
- end
- end
- if (timing) then
- local current = os.clock() - starttime
- -- print("current time: " .. tostring(current))
- displayTime(current)
- sleep(.01)
- else
- sleep(.1)
- end
- end
- end
- ---------------------------------------
- --the Main
- ---------------------------------------
- local screenSide = getDeviceSide("monitor");
- if (screenSide) then
- monitor = peripheral.wrap(screenSide)
- if(not monitor.isColor()) then
- error("The attached monitor must be Advanced. Get some gold!")
- end
- screenw, screenh = monitor.getSize()
- print("Monitor found, width: " .. tostring(screenw))
- print(monitor)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextScale(5)
- monitor.clear()
- else
- error("No attached monitor. The timer needs a monitor.")
- end
- print("Waiting for a redstone signal.")
- displayTime(0)
- print("calling mainloop")
- parallel.waitForAll(mainLoop)
Add Comment
Please, Sign In to add comment