Jump to content




[Solved] Wrap multiple peripherals of the same kind


  • You cannot reply to this topic
9 replies to this topic

#1 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 07 July 2013 - 04: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? :)

#2 Yevano

  • Members
  • 376 posts
  • LocationUSA

Posted 07 July 2013 - 05:07 PM

for _, mon in pairs(monitors) do mon.write("hi") end

What exactly do you mean by "loop wrapping"?

#3 AgentE382

  • Members
  • 119 posts

Posted 07 July 2013 - 05: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)


#4 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 07 July 2013 - 06:49 PM

Thanks, that did the trick :)

#5 AgentE382

  • Members
  • 119 posts

Posted 07 July 2013 - 08:42 PM

You're welcome :D

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 July 2013 - 12:43 AM

View PostAgentE382, on 07 July 2013 - 05:44 PM, said:

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. :)

#7 AgentE382

  • Members
  • 119 posts

Posted 08 July 2013 - 06:07 AM

View Posttheoriginalbit, on 08 July 2013 - 12:43 AM, said:

View PostAgentE382, on 07 July 2013 - 05:44 PM, said:

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

#8 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 08 July 2013 - 06:28 AM

View PostAgentE382, on 08 July 2013 - 06:07 AM, said:

View Posttheoriginalbit, on 08 July 2013 - 12:43 AM, said:

View PostAgentE382, on 07 July 2013 - 05:44 PM, said:

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...

#9 AgentE382

  • Members
  • 119 posts

Posted 08 July 2013 - 11:34 AM

View Postdarkrising, on 08 July 2013 - 06:28 AM, said:

View PostAgentE382, on 08 July 2013 - 06:07 AM, said:

View Posttheoriginalbit, on 08 July 2013 - 12:43 AM, said:

View PostAgentE382, on 07 July 2013 - 05:44 PM, said:

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)


#10 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 08 July 2013 - 12:43 PM

View PostAgentE382, on 08 July 2013 - 11:34 AM, said:

View Postdarkrising, on 08 July 2013 - 06:28 AM, said:

View PostAgentE382, on 08 July 2013 - 06:07 AM, said:

View Posttheoriginalbit, on 08 July 2013 - 12:43 AM, said:

View PostAgentE382, on 07 July 2013 - 05:44 PM, said:

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 :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users