Sirshark10

libperiph

Mar 20th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. _G.peripheral = {}
  2.  
  3. local function getPath(side)
  4.   for k,v in pairs(fs.list("/dev/")) do
  5.     if v:find("ttyS") then
  6.       local fPath = "/dev/"..v
  7.       return fPath
  8.     end
  9.   end
  10. end
  11.  
  12. function peripheral.wrap(side)
  13.   local fPath = getPath(side)
  14.   local funcs = {}
  15.   local file = fs.open(fPath,"w")
  16.   local rFile = fs.open(fPath,"r")
  17.   file.write("getFunctions")
  18.   local ret = rFile.readAll()
  19.   file.close()
  20.   rFile.close()
  21.   for x,y in pairs(ret) do
  22.     funcs[y] = function(...)
  23.       local f = fs.open(fPath,"w")
  24.       local rF = fs.open(fPath,"r")
  25.       local args = {...}
  26.       local params = ""
  27.       for j,l in pairs(args) do
  28.         if type(l) == "string" and l:find("% ") then
  29.           l = l:gsub("% ", "\\% ")
  30.         end
  31.         params = params.. " " .. l
  32.       end
  33.       f.write(y..params)
  34.       local returned = rF.readAll()
  35.       f.close()
  36.       rF.close()
  37.       return returned
  38.     end
  39.   end
  40.   return funcs
  41. end    
  42.  
  43. function peripheral.isPresent(side)
  44.   if peripheral.wrap(side) then
  45.     return true
  46.   end
  47.   return false
  48. end
  49.  
  50. function peripheral.getType(side)
  51.   local file = fs.open(getPath(side),"w")
  52.   local f2 = fs.open(getPath(side),"r")
  53.   file.write("getType")
  54.   file.close()
  55.   local ret = f2.readAll()
  56.   f2.close()
  57.   return ret
  58. end
  59.  
  60. function peripheral.getMethods(side)
  61.   local file = fs.open(getPath(side),"w")
  62.   local f2 = fs.open(getPath(side),"r")
  63.   file.write("getFunctions")
  64.   file.close()
  65.   local ret = f2.readAll()
  66.   f2.close()
  67.   return ret
  68. end
  69.  
  70. function peripheral.call(side,func,...)
  71.   local file = fs.open(getPath(side),"w")
  72.   local f2 = fs.open(getPath(side),"r")
  73.   local args = {...}
  74.   local params = ""
  75.   for k,v in pairs(args) do
  76.     if type(v) == "string" and v:find("% ") then
  77.       v = v:gsub("% ","\\% ")
  78.     end
  79.     params = params.." "..v
  80.   end
  81.  
  82.   file.write(func..params)
  83.   file.close()
  84.   local ret = f2.readAll()
  85. end
  86.  
  87. function peripheral.getNames()
  88.   local sPeriphs = {}
  89.   local fPath =""
  90.   for k,v in pairs(fs.list("/dev/")) do
  91.     if v:find("ttyS") then
  92.       fPath = "/dev/"..v
  93.       local file = fs.open(fPath,"w")
  94.       local f2 = fs.open(fPath,"r")
  95.       file.write("getSide")
  96.       local side = f2.readAll()
  97.       file.close()
  98.       f2.close()
  99.       table.insert(sPeriphs,side)
  100.     end
  101.   end
  102.   return sPeriphs
  103. end
Add Comment
Please, Sign In to add comment