gknova61

Timer

Oct 16th, 2012
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.81 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usage:" )
  3.     print( shell.getRunningProgram().." <interval> (config)" )
  4.     print("Parameters:")
  5.     print("<> = required")
  6.     print("() = optional")
  7. end
  8.  
  9. local x,y = term.getSize()
  10.  
  11. local function cPrint( txt )
  12.     local function printC( text )
  13.         x2,y2 = term.getCursorPos()
  14.         term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  15.         write(text.. "\n")
  16.     end
  17.        
  18.     if type(txt) == "string" then
  19.         printC( txt )
  20.     elseif type(txt) == "table" then
  21.         for i=1,#txt do
  22.             printC( txt[i] )
  23.         end
  24.     end
  25. end
  26.  
  27. local function autoUpdate()
  28.     local programName = shell.getRunningProgram()
  29.     term.clear()
  30.     term.setCursorPos(1,6)
  31.     cPrint("+--------------------+")
  32.     cPrint("|     Loading...     |")
  33.     cPrint("| ||||||             |")
  34.     cPrint("+--------------------+")
  35.     while true do
  36.         local downloadedFile = http.request("http://pastebin.com/raw.php?i=XR9C8z75")
  37.         local event, url, body = os.pullEvent()
  38.         if event == "http_success" then
  39.             _tBody = body.readAll()
  40.             break
  41.         elseif event == "http_failed" then
  42.             break
  43.         end
  44.     end
  45.     term.clear()
  46.     term.setCursorPos(1,6)
  47.     cPrint("+--------------------+")
  48.     cPrint("|     Loading...     |")
  49.     cPrint("| ||||||||||||       |")
  50.     cPrint("+--------------------+")
  51.     file = fs.open(programName, "r")
  52.     _fBody = file.readAll()
  53.     file.close()
  54.     sleep(0.5)
  55.     term.clear()
  56.     term.setCursorPos(1,6)
  57.     cPrint("+--------------------+")
  58.     cPrint("|     Loading...     |")
  59.     cPrint("| |||||||||||||||||| |")
  60.     cPrint("+--------------------+")
  61.     if not fs.exists("archiveAPI") then
  62.     fs.makeDir("archiveAPI")
  63.     end
  64.  
  65.     if not fs.exists("archiveAPI/config") then        
  66.     local sCode = "fkcMT9CE"
  67.     local sFile = "archiveAPI/config"
  68.     local sPath = shell.resolve(sFile)
  69.     local response = http.get(
  70.     "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  71.     )    
  72.     if response then
  73.         local sResponse = response.readAll()
  74.         response.close()
  75.         local file = fs.open( sPath, "w" )
  76.         file.write( sResponse )
  77.         file.close()
  78.     end    
  79.     end
  80.     local selection = 1
  81.     if _fBody ~= _tBody then
  82.         cPrint("+--------------------+")
  83.         cPrint("|     Updating...    |")
  84.         cPrint("+--------------------+")
  85.         sleep(2)
  86.         fs.delete(programName)
  87.         file = fs.open(programName, "w")
  88.         file.write(_tBody)
  89.         file.close()
  90.         shell.run(programName, sInterval)
  91.         error()
  92.     end
  93. end
  94. autoUpdate()
  95.  
  96. if not fs.exists("archiveAPI") then
  97.     fs.makeDir("archiveAPI")
  98. end
  99.  
  100. if not fs.exists("archiveAPI/config") then        
  101.     local sCode = "fkcMT9CE"
  102.     local sFile = "archiveAPI/config"
  103.     local sPath = shell.resolve(sFile)
  104.     local response = http.get(
  105.     "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  106.     )    
  107.     if response then
  108.         local sResponse = response.readAll()
  109.         response.close()
  110.         local file = fs.open( sPath, "w" )
  111.         file.write( sResponse )
  112.         file.close()
  113.     end    
  114. end
  115. os.loadAPI("archiveAPI/config")
  116.  
  117. local function editConfig()
  118.     term.clear()
  119.     term.setCursorPos(1,1)
  120.     fs.delete("configs")
  121.     cPrint("Interactive Configuration Editor (I.C.E.)")
  122.     config.load("configs","Timer.cfg")
  123.     term.setCursorPos(1,3)
  124.     print("Please set a computer id to receive all the rednet messages")
  125.     local sMid = read("*")
  126.     config.writeVal("mId",sMid)
  127.     print("Please type out the sides you'd like to output redstone on (one at a time and leave blank for nil)")
  128.     for i=1,rs.getSides() do
  129.         redstoneSide[i] = read()
  130.         if redstoneSide[i] ~= nil then
  131.             if checkValidSide(input) then
  132.                 table.insert(redstoneSides,i,redstoneSide[i])
  133.                 config.writeVal("redstoneSide"..tostring(i),redstoneSide[i])
  134.             elseif not checkValidSide(input) then
  135.                 print("Not a valid side!")
  136.                 i=i-1
  137.             end
  138.         elseif redstoneSide[i] == nil then
  139.             config.writeVal("numOfActiveSides",tostring((i-1)))
  140.             break
  141.         end
  142.     end
  143.     config.save()
  144.     print("Success, please restart your timer")
  145.     sleep(2)
  146. end
  147.  
  148. local function checkValidSide(input)
  149.     local availableRedstoneSides = rs.getSides()
  150.     for i2=1,#availableRedstoneSides do
  151.         if input == availableRedstoneSides[i] then
  152.             return true
  153.         end
  154.     end
  155.     return false
  156. end
  157.  
  158. local function readConfig()
  159.     redstoneSides = {}
  160.     config.load("configs","Timer.cfg")
  161.     mId = tonumber(config.readVal(mId))
  162.     sInterval = tonumber(config.readVal(sInterval))
  163.     if tonumber(config.readVal(numOfActiveSides)) == nil then
  164.         local activeSides = 6
  165.     else
  166.         local activeSides = tonumber(config.readVal(numOfActiveSides))
  167.         if activeSides == nil then
  168.             activeSides = 6
  169.         end
  170.     end
  171.     for i=1,activeSides do
  172.         redstoneSide[i] = tostring(config.readVal(redstoneSide[i]))
  173.     end
  174.     config.save()
  175. end
  176.  
  177. local function redstoneOut(boolean)
  178.     for i=1,#redstoneSides do
  179.     redstone.setOutput(redstoneSides[i],boolean)
  180.     end
  181. end
  182.  
  183. local function pWrapper()
  184.     local bOpen, sFreeSide = false, nil
  185.     for n,sSide in pairs(rs.getSides()) do 
  186.         if peripheral.getType( sSide ) == "modem" then
  187.             sFreeSide = sSide
  188.             if rednet.isOpen( sSide ) then
  189.                 bOpen = true
  190.                 break
  191.             end
  192.         end
  193.     end
  194.    
  195.     if not bOpen then
  196.         if sFreeSide then
  197.             term.clear()
  198.             term.setCursorPos(1,1)
  199.             cPrint("+------------------------------------+")
  200.             cPrint("| Opening Modem on "..sFreeSide.." |")
  201.             cPrint("+------------------------------------+")
  202.             rednet.open( sFreeSide )
  203.             sOpenedSide = sFreeSide
  204.             sleep(2)
  205.             return true
  206.         else
  207.             print( "No modem attached. Please try again." )
  208.             sleep(2)
  209.             os.reboot()
  210.         end
  211.     end
  212.     return true
  213. end
  214.  
  215. if sConfig == "config" then editConfig()
  216. end
  217.  
  218. local tArgs = { ... }
  219. local sOpenedSide = nil
  220. local sInterval = tonumber(tArgs[1])
  221. local sConfig = tArgs[2]
  222. config.load("configs","Timer.cfg")
  223. if #tArgs < 1 and config.readVal("sInterval") == nil then
  224.     config.save()
  225.     printUsage()
  226.     return false
  227. end
  228. config.save()
  229.  
  230. if mId == nil then
  231.     mId = 0
  232. end
  233.  
  234. if tArgs[1] ~= nil then
  235.     config.load("configs","Timer.cfg")
  236.     config.writeVal("sInterval",tArgs[1])
  237.     config.save()
  238. end
  239.  
  240. if not rednet.broadcast("PING") then pWrapper() end
  241.     readConfig()
  242.     term.clear()
  243.     term.setCursorPos(1,1)
  244.     cPrint("+--------------------+")
  245.     cPrint("| Triggers Done: 0 |")
  246.     cPrint("+--------------------+")
  247. local spaceTimerLength, spaceTimer, pulseTimer, pulseCount = 0.8, nil, nil, 0
  248. local updateInterval, updateTarget = 100, mId
  249. rednet.send(updateTarget,"timer_check_in_"..os.computerID())
  250. os.unloadAPI("archiveAPI/config")
  251. while true do
  252.         e, p1, p2 = os.pullEvent()
  253.         if e == "timer" then
  254.             if p1 == spaceTimer then
  255.                 redstone.setOutput("back", true)
  256.                 pulseTimer = os.startTimer(0.1)
  257.             elseif p1 == pulseTimer then
  258.                 redstone.setOutput("back", false)
  259.                 spaceTimer = os.startTimer(spaceTimerLength)
  260.                 pulseCount = pulseCount + 1
  261.                 term.setCursorPos(1,2)
  262.                 term.clearLine()
  263.                 cPrint("| Triggers Done: "..pulseCount.." |")
  264.                 if pulseCount % updateInterval == 0 then
  265.                     rednet.send(updateTarget, tostring(pulseCount / updateInterval))
  266.                 end
  267.             end
  268.         elseif e == "rednet_message" then
  269.             if p1 == updateTarget and p2 == "reset_"..os.computerID() then
  270.                 pulseCount = 0
  271.                 term.setCursorPos(1,2)
  272.                 term.clearLine()
  273.                 cPrint("| Triggers Done: "..pulseCount.." |")
  274.             elseif p1 == updateTarget and p2 == "stop_"..os.computerID() then
  275.                 spaceTimer = nil
  276.                 pulseTimer = nil
  277.                 redstone.setOutput("back", false)
  278.             elseif p1 == updateTarget and p2 == "start_"..os.computerID() then
  279.                 spaceTimer = os.startTimer(0)
  280.              end
  281.         end
  282. end
Advertisement
Add Comment
Please, Sign In to add comment