Any help would be much appreciated.
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
How To Wrap All Available Peripherals Via Networking Cables
Started by thebadgerherder1, 17 September 2013 - 07:09 PMPosted 17 September 2013 - 09:09 PM
I am trying to use open peripherals to read data from all the energy storage blocks in my base. the thing holding me up is that i would like to simply be able to wire up any new block to the system, and it would recognise and read data from the new block and display it. essentially as the title suggests i am looking for a method to list all the available peripherals. Whilst i am aware there is a method for listing all the currently wrapped peripherals, however this would not return any newly connected peripherals. The end goal would be to have this all display on my HUD using terminal (google) glasses.
Any help would be much appreciated.
Any help would be much appreciated.
Posted 17 September 2013 - 11:52 PM
peripheral.getNames() will return a list of all currently accessible peripherals. You could then look for the type of peripheral you want by iterating this list and using peripheral.getType():
local myPeripherals = {}
for _, name in pairs(peripheral.getNames()) do
if peripheral.getType(name) == "monitor" then --# change "monitor" to your peripheral type.
table.insert(myPeripherals, name)
end
end
--# myPeripherals is now a table of all of the peripherals of a given type attached to the computer.
Posted 18 September 2013 - 03:15 PM
thanks
Posted 18 September 2013 - 06:44 PM
is there a method to say "is this peripheral wired to me directly, or through a modem?" other than saying "if not name == one of the sides".
Posted 18 September 2013 - 06:52 PM
Check the peripheral API page. There may be something like isRemote? If not, just check the name against a pattern like ".*_%d+$".
Posted 18 September 2013 - 09:00 PM
how would i check it against the pattern? i havent really used them before…
Posted 18 September 2013 - 09:05 PM
Well, string.match will return the matching substring if part of the string you pass it matches the provided pattern. All you have to do is check it with a simple string.match:
if string.match(peripheralName, "(.*_%d+$)") then
--# peripheralName contains a remote peripheral name.
end
Posted 18 September 2013 - 09:16 PM
thanks again
Posted 18 September 2013 - 09:20 PM
oh sry and one last thing, i would like to wrap the peripheral automatically to the name provided by for i,name in pairs(peripheral.getNames()). however if i do "name = peripheral.wrap(name)" then it will wrap the variable "name" to the peripheral, not the actual name which might be batbox_somenumber. is there a way around this?
Posted 18 September 2013 - 09:22 PM
Well, what do you want to name it?
Edit: getfenv() will let you access global variables as a table, as in:
Edit: getfenv() will let you access global variables as a table, as in:
getfenv()[name] = peripheral.wrap(name)
Posted 19 September 2013 - 07:06 PM
here is what i have so far: pastebin.com/WjqHSZyz. it errors as "main:32:attempt to concatenate string and nil". any suggestions?
Posted 19 September 2013 - 08:59 PM
if you want to wrap the remote name to the object then try this variation on Lyqids suggestion.
local rNames -- declare empty var to later hold indexed table of present remote names
for _,side in ipairs(rs.getSides()) do
if peripheral.getType(side) == "modem" and peripheral.call(side,"isWireless") == false then
rNames = peripheral.call(side,"getNamesRemote") -- We found the wired modem. Fill our 'rNames' var with a table of remote names
end
end
local myRemotes = {} -- your to be wrapped remotes. Empty...
-- Go through our rNames table of names and create a key in myRemotes with the remote object as the value
for i = 1,#rNames do
-- myRemotes[remoteName] = peripheral.wrap(remoteName)
myRemotes[rNames[i]] = peripheral.wrap(rNames[i])
end
Posted 20 September 2013 - 04:17 AM
getfenv()[name] = peripheral.wrap(name)
table.insert(peripherals,name)
stored = stored+name.getStored()
"name" still means "the variable called name" not "the variable called whatever is in name"
Turns out you don't actually need variables. Why not just do this?
table.insert(peripherals, peripheral.wrap(name))
Posted 20 September 2013 - 04:09 PM
And if you really insisted on wrapping up all of the connected types into a single peripheral.wrap, you could do:
It's untested, but that should do the trick. Obviously, it changes how values are returned, so you couldn't (for instance) redirect to multiple monitors wrapped this way.
function wrapAll(type)
local wrapped, names = {}, {}
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == type then
table.insert(names, name)
end
end
for _, fn in ipairs(peripheral.getMethods(names[1])) do
wrapped[fn] = function(...)
local returns = {}
for i = 1, #names do
returns[i] = peripheral.call(names[i], fn, ...)
end
return returns
end
end
return wrapped
end
It's untested, but that should do the trick. Obviously, it changes how values are returned, so you couldn't (for instance) redirect to multiple monitors wrapped this way.
Posted 22 September 2013 - 04:55 PM
im planning on looping through all of the wrapped peripherals, getting their type, and then translating that into the appropriate "get" function (most of these peripherals will be things such as mfsu's and redstone energy cells) then writing it all out to my terminal glasses as progress bar like displays like the ones found here. the end goal as disscussed above being to be able to simply connect up the new peripherals and such, then it will update and begin outputting to my glasses. im not sure if this would affect in what way the peripherals are wrapped.
thanks for the help
thanks for the help