This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
darkrising's profile picture

[Solved] Wrap multiple peripherals of the same kind

Started by darkrising, 07 July 2013 - 03:39 PM
darkrising #1
Posted 07 July 2013 - 05:39 PM
Hello, I was wondering if it was possible to wrap multiple peripherals of the same kind to one table. In this case more than one monitor with some on cables and others not.

I just wrote quite a long program and realized that "loop wrapping" monitors is not really an option.

It would be cool if I could just write mon.write("hi!") to all the monitors.

Any ideas? :)/>
Yevano #2
Posted 07 July 2013 - 06:07 PM
for _, mon in pairs(monitors) do mon.write("hi") end

What exactly do you mean by "loop wrapping"?
AgentE382 #3
Posted 07 July 2013 - 06:44 PM
I wrote a peripheral detection / interfacing API.

Basically, you'll want to loop through all sides and use `peripheral.isPresent()` and `peripheral.getType()` to find your monitors.

Wrap each periperal then call mons:add(–[[wrapped peripheral here]])

Then call whatever function on `mons` and it'll work.

mons:

mons =
{
    add = function (self, mon) self[#self + 1] = mon end,
    __index = function (tbl, key) return function(...) for i = 1, #tbl do tbl[i][key](...) end end end
}
setmetatable(mons,mons)
darkrising #4
Posted 07 July 2013 - 07:49 PM
Thanks, that did the trick :)/>
AgentE382 #5
Posted 07 July 2013 - 09:42 PM
You're welcome :D/>
theoriginalbit #6
Posted 08 July 2013 - 01:43 AM
Basically, you'll want to loop through all sides and use `peripheral.isPresent()` and `peripheral.getType()` to find your monitors.
Just a slight note here, through testing a long while ago I discovered that you do not actually need to use peripheral.isPresent() as peripheral.getType() will return nil if there is not peripheral present on the side you're checking. :)/>
AgentE382 #7
Posted 08 July 2013 - 07:07 AM
Basically, you'll want to loop through all sides and use `peripheral.isPresent()` and `peripheral.getType()` to find your monitors.
Just a slight note here, through testing a long while ago I discovered that you do not actually need to use peripheral.isPresent() as peripheral.getType() will return nil if there is not peripheral present on the side you're checking. :)/>/>
Thanks! Now I can remove some function calls before I publish my API. :D/>
darkrising #8
Posted 08 July 2013 - 07:28 AM
Basically, you'll want to loop through all sides and use `peripheral.isPresent()` and `peripheral.getType()` to find your monitors.
Just a slight note here, through testing a long while ago I discovered that you do not actually need to use peripheral.isPresent() as peripheral.getType() will return nil if there is not peripheral present on the side you're checking. :)/>/>
Thanks! Now I can remove some function calls before I publish my API. :D/>

On some testing, the background colour won't set for multiple monitors which is odd…
AgentE382 #9
Posted 08 July 2013 - 12:34 PM
Basically, you'll want to loop through all sides and use `peripheral.isPresent()` and `peripheral.getType()` to find your monitors.
Just a slight note here, through testing a long while ago I discovered that you do not actually need to use peripheral.isPresent() as peripheral.getType() will return nil if there is not peripheral present on the side you're checking. :)/>/>
Thanks! Now I can remove some function calls before I publish my API. :D/>

On some testing, the background colour won't set for multiple monitors which is odd…

Alright. First, make sure you're using an Advanced Monitor. Then make sure you can change the background color on each monitor individually. Then you could try using this implementation of `mons` if the other one doesn't work:

mons =
{
    add = function (self, mon) self[#self + 1] = mon end,
    __index = function (tbl, key) return function(...) local funcs = {} for i = 1, #tbl do funcs[#funcs + 1] = function() tbl[i][key](...) end end paralel.waitForAll(unpack(funcs)) end end
}
setmetatable(mons,mons)
darkrising #10
Posted 08 July 2013 - 01:43 PM
Basically, you'll want to loop through all sides and use `peripheral.isPresent()` and `peripheral.getType()` to find your monitors.
Just a slight note here, through testing a long while ago I discovered that you do not actually need to use peripheral.isPresent() as peripheral.getType() will return nil if there is not peripheral present on the side you're checking. :)/>/>
Thanks! Now I can remove some function calls before I publish my API. :D/>

On some testing, the background colour won't set for multiple monitors which is odd…

Alright. First, make sure you're using an Advanced Monitor. Then make sure you can change the background color on each monitor individually. Then you could try using this implementation of `mons` if the other one doesn't work:

mons =
{
	add = function (self, mon) self[#self + 1] = mon end,
	__index = function (tbl, key) return function(...) local funcs = {} for i = 1, #tbl do funcs[#funcs + 1] = function() tbl[i][key](...) end end paralel.waitForAll(unpack(funcs)) end end
}
setmetatable(mons,mons)

I managed to get it to work, thanks :)/>