gknova61

Timer v2

Dec 28th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.26 KB | None | 0 0
  1. loaded = false
  2. dir = ""
  3. file = ""
  4. path = ""
  5. internal = {}
  6. local tArgs = {...}
  7. local function create()
  8.         local file = fs.open(path,"a")
  9.         file.close()
  10. end
  11.  
  12. local function load(directory,fileName)
  13.         dir = directory
  14.         file = fileName
  15.         if dir ~= nil then
  16.                 path = dir.."/"..file
  17.                 fs.makeDir(dir)
  18.                 create(path)
  19.                 local file = fs.open(path, "r")
  20.                 repeat
  21.                         line = file.readLine()
  22.                         if(line ~= nil) then
  23.                                 local asWords = line:gsub(":","")
  24.                                 local parts = {}
  25.                                 for word in asWords:gmatch("%w+") do table.insert(parts,word) end
  26.                                 internal[parts[1]] = parts[2]
  27.                         end
  28.                 until line == nil
  29.                 loaded = true
  30.                 file.close()
  31.         else
  32.                 path = file
  33.                 create(path)
  34.                 local file = fs.open(path, "r")
  35.                 repeat
  36.                         line = file.readLine()
  37.                         if(line ~= nil) then
  38.                                 local asWords = line:gsub(":","")
  39.                                 local parts = {}
  40.                                 for word in asWords:gmatch("%w+") do table.insert(parts,word) end
  41.                                 internal[parts[1]] = parts[2]
  42.                         end
  43.                 until line == nil
  44.                 loaded = true
  45.                 file.close()
  46.         end
  47. end
  48.  
  49. local function writeVal(key,value)
  50.         if(loaded == false) then
  51.                 return false
  52.         else
  53.                 local toWrite = value
  54.                 if(value == true) then toWrite = "true"
  55.                 elseif(value == false) then toWrite = "false" end
  56.                 internal[key] = toWrite
  57.                 return true
  58.         end
  59. end
  60.  
  61. local function readVal(key)
  62.         if(loaded == false) then
  63.                 return nil
  64.         end
  65.         toReturn = internal[key]
  66.         if(toReturn == "true") then return true
  67.         elseif(toReturn == "false") then return false
  68.         else return internal[key] end
  69. end
  70.  
  71. local function save()
  72.         if(loaded == true) then
  73.                 local file = fs.open(path,"w")
  74.                 for i,v in pairs(internal) do
  75.                         file.writeLine(i.." = "..v)
  76.                 end
  77.                 file.close()
  78.                 internal = {}
  79.                 loaded = false
  80.                 return true
  81.         else
  82.                 return false
  83.         end
  84. end
  85.  
  86. local function isEmpty()
  87.         if(loaded == false) then
  88.                 return nil
  89.         end
  90.         if(fs.getSize(path) == 0)then
  91.                 return true
  92.         else return false end
  93. end
  94.  
  95. local function printUsage()
  96.     print("Usage:")
  97.     print(shell.getRunningProgram().." <interval> <side> <inputSide> (pulseTime)")
  98.     print("Parameters:")
  99.     print("[] = Required")
  100.     print("() = Optional")
  101.     return true
  102. end
  103.  
  104. --Some basic error checking, will be a bit 'better' in future
  105. if #tArgs < 3 and not fs.exists("config") then
  106.     printUsage()
  107.     return false
  108. elseif #tArgs < 3 and fs.exists("config") then
  109.     noArgsGood = true
  110. end
  111.  
  112. if tArgs[1] == "usage" then
  113.     printUsage()
  114.     return false
  115. end
  116.  
  117. if noArgsGood == nil then
  118.     intVal = tonumber(tArgs[1])
  119.     side = tArgs[2]
  120.     iStop = tArgs[3]
  121.     pTime = tonumber(tArgs[4])
  122.     load(nil,"config")
  123.     writeVal("intVal",tostring(intVal))
  124.     writeVal("side",side)
  125.     writeVal("iStop",iStop)
  126.     writeVal("pTime",tostring(pTime))
  127.     save()
  128. elseif noArgsGood then
  129.     load(nil,"config")
  130.     intVal = tonumber(readVal("intVal"))
  131.     side = readVal("side")
  132.     iStop = readVal("iStop")
  133.     pTime = tonumber(readVal("pTime"))
  134.     save()
  135. end
  136.  
  137. if pTime == nil then
  138.     pTime = 0.2
  139. end
  140.  
  141. while true do
  142.     os.startTimer(intVal)
  143.     local event = {os.pullEvent()}
  144.     if event[1] == "redstone" and rs.getInput(iStop) then
  145.         while true do
  146.             local event = {os.pullEvent("redstone")}
  147.             if event[1] == "redstone" and not rs.getInput(iStop) then
  148.                 break
  149.             end
  150.         end
  151.     elseif event[1] == "timer" then
  152.         rs.setOutput(side,true)
  153.         sleep(pTime)
  154.         rs.setOutput(side,false)
  155.     end --TODO: Add the optional ability to stop with Rednet
  156. end
Advertisement
Add Comment
Please, Sign In to add comment