Advertisement
Sanwi

CC: Get Peripheral Methods

Feb 6th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. -- This program is written for computercraft
  2. -- Retrieves a table of all components, then displays then on the screen one at a time
  3. -- Press any key to go to the next peripheral
  4.  
  5. -- Finds devices connected to the computer, wraps them
  6. local function getDevices(deviceType) -- This function was taken from https://github.com/sandalle/minecraft_bigreactor_control and has been modified by https://github.com/Sanwi
  7. local deviceName = nil
  8. local deviceIndex = 1
  9. local deviceList, deviceNames = {}, {} -- Empty array, which grows as we need
  10. local peripheralList = peripheral.getNames() -- Get table of connected peripherals
  11. for peripheralIndex = 1, #peripheralList do -- Log every device found
  12. if (string.lower(peripheral.getType(peripheralList[peripheralIndex])) == deviceType) then -- Log devices found which match deviceType and which device index we give them
  13. deviceNames[deviceIndex] = peripheralList[peripheralIndex]
  14. deviceList[deviceIndex] = peripheral.wrap(peripheralList[peripheralIndex])
  15. deviceIndex = deviceIndex + 1
  16. end
  17. end -- for peripheralIndex = 1, #peripheralList do
  18. return deviceList, deviceNames
  19. end
  20.  
  21. -- Prints functions available for deviceType
  22. local function printPerFunctions(deviceType)
  23. local deviceList,deviceNames = getDevices(deviceType)
  24. for k,v in pairs(deviceList[1]) do -- For first wrapped machine table
  25. print(k) -- Print the name of the function
  26. end
  27. end
  28.  
  29. local function printAdvancedMethods(deviceType)
  30. local deviceList, deviceNames = getDevices(deviceType)
  31. if deviceList[1].getAdvancedMethodsData then
  32. for k,v in pairs(deviceList[1].getAdvancedMethodsData()) do -- For each method returned for this device
  33. print("\nMethod Name: "..tostring(k)) -- String of method name
  34. if type(v) == "table" then
  35. for k2,v2 in pairs(v) do
  36. if k2 == "returnTypes" then
  37. if v2[1] then
  38. print("returns: "..tostring(v2[1]))
  39. end
  40. elseif k2 == "args" then
  41. print("args: ")
  42. for k3,v3 in pairs(v2) do -- For each arg
  43. for k4,v4 in pairs(v3) do
  44. print(tostring(k4).." : "..tostring(v4))
  45. end
  46. print("\n")
  47. end
  48. elseif k2 == "description" then
  49. print(tostring(v2))
  50. else
  51. print(tostring(v2))
  52. end
  53. end
  54. else
  55. print("k: "..k)
  56. print("v: "..v)
  57. end
  58. print("\nPress any key to continue")
  59. local event, key = os.pullEvent("key")
  60. end
  61. end
  62. end
  63.  
  64. local function printPerTypes()
  65. local allPers = peripheral.getNames()
  66. local perTypeAnchor = ""
  67. for i=1, #allPers do
  68. if allPers[i] ~= perTypeAnchor then -- Iterate for each peripheral
  69. perTypeAnchor = allPers[i]
  70. local type = peripheral.getType(allPers[i]) -- Get peripheral type
  71.  
  72. -- List all functions for this peripheral
  73. print("\nName: "..type)
  74. printPerFunctions(type)
  75.  
  76. -- Wait for key input
  77. print("\nPress N for next device, or M for advanced methods data")
  78. local event, key = os.pullEvent("key")
  79.  
  80. if key == 50 then
  81. printAdvancedMethods(type)
  82. end
  83.  
  84. end
  85. end
  86. end
  87.  
  88. local monitorList, monitorNames = getDevices("monitor")
  89.  
  90. term.redirect(monitorList[1])
  91. term.clear()
  92. term.setCursorPos(1,1)
  93.  
  94. printPerTypes()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement