Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _G.peripheral = {}
- local function getPath(side)
- for k,v in pairs(fs.list("/dev/")) do
- if v:find("ttyS") then
- local fPath = "/dev/"..v
- return fPath
- end
- end
- end
- function peripheral.wrap(side)
- local fPath = getPath(side)
- local funcs = {}
- local file = fs.open(fPath,"w")
- local rFile = fs.open(fPath,"r")
- file.write("getFunctions")
- local ret = rFile.readAll()
- file.close()
- rFile.close()
- for x,y in pairs(ret) do
- funcs[y] = function(...)
- local f = fs.open(fPath,"w")
- local rF = fs.open(fPath,"r")
- local args = {...}
- local params = ""
- for j,l in pairs(args) do
- if type(l) == "string" and l:find("% ") then
- l = l:gsub("% ", "\\% ")
- end
- params = params.. " " .. l
- end
- f.write(y..params)
- local returned = rF.readAll()
- f.close()
- rF.close()
- return returned
- end
- end
- return funcs
- end
- function peripheral.isPresent(side)
- if peripheral.wrap(side) then
- return true
- end
- return false
- end
- function peripheral.getType(side)
- local file = fs.open(getPath(side),"w")
- local f2 = fs.open(getPath(side),"r")
- file.write("getType")
- file.close()
- local ret = f2.readAll()
- f2.close()
- return ret
- end
- function peripheral.getMethods(side)
- local file = fs.open(getPath(side),"w")
- local f2 = fs.open(getPath(side),"r")
- file.write("getFunctions")
- file.close()
- local ret = f2.readAll()
- f2.close()
- return ret
- end
- function peripheral.call(side,func,...)
- local file = fs.open(getPath(side),"w")
- local f2 = fs.open(getPath(side),"r")
- local args = {...}
- local params = ""
- for k,v in pairs(args) do
- if type(v) == "string" and v:find("% ") then
- v = v:gsub("% ","\\% ")
- end
- params = params.." "..v
- end
- file.write(func..params)
- file.close()
- local ret = f2.readAll()
- end
- function peripheral.getNames()
- local sPeriphs = {}
- local fPath =""
- for k,v in pairs(fs.list("/dev/")) do
- if v:find("ttyS") then
- fPath = "/dev/"..v
- local file = fs.open(fPath,"w")
- local f2 = fs.open(fPath,"r")
- file.write("getSide")
- local side = f2.readAll()
- file.close()
- f2.close()
- table.insert(sPeriphs,side)
- end
- end
- return sPeriphs
- end
Add Comment
Please, Sign In to add comment