Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- loaded = false
- dir = ""
- file = ""
- path = ""
- internal = {}
- local tArgs = {...}
- local function create()
- local file = fs.open(path,"a")
- file.close()
- end
- local function load(directory,fileName)
- dir = directory
- file = fileName
- if dir ~= nil then
- path = dir.."/"..file
- fs.makeDir(dir)
- create(path)
- local file = fs.open(path, "r")
- repeat
- line = file.readLine()
- if(line ~= nil) then
- local asWords = line:gsub(":","")
- local parts = {}
- for word in asWords:gmatch("%w+") do table.insert(parts,word) end
- internal[parts[1]] = parts[2]
- end
- until line == nil
- loaded = true
- file.close()
- else
- path = file
- create(path)
- local file = fs.open(path, "r")
- repeat
- line = file.readLine()
- if(line ~= nil) then
- local asWords = line:gsub(":","")
- local parts = {}
- for word in asWords:gmatch("%w+") do table.insert(parts,word) end
- internal[parts[1]] = parts[2]
- end
- until line == nil
- loaded = true
- file.close()
- end
- end
- local function writeVal(key,value)
- if(loaded == false) then
- return false
- else
- local toWrite = value
- if(value == true) then toWrite = "true"
- elseif(value == false) then toWrite = "false" end
- internal[key] = toWrite
- return true
- end
- end
- local function readVal(key)
- if(loaded == false) then
- return nil
- end
- toReturn = internal[key]
- if(toReturn == "true") then return true
- elseif(toReturn == "false") then return false
- else return internal[key] end
- end
- local function save()
- if(loaded == true) then
- local file = fs.open(path,"w")
- for i,v in pairs(internal) do
- file.writeLine(i.." = "..v)
- end
- file.close()
- internal = {}
- loaded = false
- return true
- else
- return false
- end
- end
- local function isEmpty()
- if(loaded == false) then
- return nil
- end
- if(fs.getSize(path) == 0)then
- return true
- else return false end
- end
- local function printUsage()
- print("Usage:")
- print(shell.getRunningProgram().." <interval> <side> <inputSide> (pulseTime)")
- print("Parameters:")
- print("[] = Required")
- print("() = Optional")
- return true
- end
- --Some basic error checking, will be a bit 'better' in future
- if #tArgs < 3 and not fs.exists("config") then
- printUsage()
- return false
- elseif #tArgs < 3 and fs.exists("config") then
- noArgsGood = true
- end
- if tArgs[1] == "usage" then
- printUsage()
- return false
- end
- if noArgsGood == nil then
- intVal = tonumber(tArgs[1])
- side = tArgs[2]
- iStop = tArgs[3]
- pTime = tonumber(tArgs[4])
- load(nil,"config")
- writeVal("intVal",tostring(intVal))
- writeVal("side",side)
- writeVal("iStop",iStop)
- writeVal("pTime",tostring(pTime))
- save()
- elseif noArgsGood then
- load(nil,"config")
- intVal = tonumber(readVal("intVal"))
- side = readVal("side")
- iStop = readVal("iStop")
- pTime = tonumber(readVal("pTime"))
- save()
- end
- if pTime == nil then
- pTime = 0.2
- end
- while true do
- os.startTimer(intVal)
- local event = {os.pullEvent()}
- if event[1] == "redstone" and rs.getInput(iStop) then
- while true do
- local event = {os.pullEvent("redstone")}
- if event[1] == "redstone" and not rs.getInput(iStop) then
- break
- end
- end
- elseif event[1] == "timer" then
- rs.setOutput(side,true)
- sleep(pTime)
- rs.setOutput(side,false)
- end --TODO: Add the optional ability to stop with Rednet
- end
Advertisement
Add Comment
Please, Sign In to add comment