Forgotten_Boy

Stopwatch Timer

Nov 23rd, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.41 KB | None | 0 0
  1. -- Variable definitions
  2. local monitor, screenw, screenh
  3. local args = {...}
  4. local sides = {"left", "right", "top", "bottom", "front", "back"};
  5. local timing = false
  6. local timer = 0
  7. local best = -1
  8. local starttime
  9.  
  10. local function getDeviceSide(deviceType)
  11.     for i,side in pairs(sides) do
  12.         if (peripheral.isPresent(side)) then
  13.             if (peripheral.getType(side)) == string.lower(deviceType) then
  14.                 return side;
  15.             end
  16.         end
  17.     end
  18. end
  19.  
  20. local function displayTime(value)
  21.     if (value) then
  22.         print(value)
  23.         term.redirect(monitor)
  24.         monitor.setTextScale(5)
  25.         monitor.setCursorPos(2, 2)
  26.         monitor.clearLine()
  27.         --write(string.format("%d%d.%d%d", value))
  28.         write(value)
  29.         term.restore()
  30.     end
  31. end
  32.  
  33. local function displayBest(value)
  34.     if (value) then
  35.         term.redirect(monitor)
  36.         monitor.setTextScale(3)
  37.         monitor.setCursorPos(1, 4)
  38.         monitor.clearLine()
  39.         --write(string.format("%d%d.%d%d", value))
  40.         write("Best: " .. tostring(value))
  41.         term.restore()
  42.     end
  43. end
  44.  
  45.  
  46. local function mainLoop()
  47.     while true do
  48.         if (rs.getInput("top")) then
  49.             if (timing) then
  50.                 local endtime = os.clock()
  51.                 final = endtime - starttime
  52.                 print("final: " .. tostring(final))
  53.                 if (final > 1) then -- ignore signals until the second second.
  54.                     if (best == -1 or final < best) then
  55.                         best = final
  56.                         print("New best: " .. tostring(best))
  57.                         displayBest(best)
  58.                     end
  59.                     timing = false
  60.                     displayTime(final)
  61.                     sleep(1)
  62.                 end
  63.             else
  64.                 --monitor.clear()
  65.                 starttime = os.clock()
  66.                 timing = true
  67.             end
  68.         end
  69.         if (timing) then
  70.             local current = os.clock() - starttime
  71. --          print("current time: " .. tostring(current))
  72.             displayTime(current)
  73.             sleep(.01)
  74.         else
  75.             sleep(.1)
  76.         end
  77.     end
  78. end
  79. ---------------------------------------
  80. --the Main
  81. ---------------------------------------
  82. local screenSide = getDeviceSide("monitor");
  83.  
  84. if (screenSide) then
  85.     monitor = peripheral.wrap(screenSide)
  86.     if(not monitor.isColor()) then
  87.         error("The attached monitor must be Advanced.  Get some gold!")
  88.     end
  89.     screenw, screenh = monitor.getSize()
  90.     print("Monitor found, width: " .. tostring(screenw))
  91.     print(monitor)
  92.     monitor.setBackgroundColor(colors.black)
  93.     monitor.setTextScale(5)
  94.     monitor.clear()
  95. else
  96.     error("No attached monitor.  The timer needs a monitor.")
  97. end
  98.  
  99. print("Waiting for a redstone signal.")
  100. displayTime(0)
  101. print("calling mainloop")
  102. parallel.waitForAll(mainLoop)
Add Comment
Please, Sign In to add comment