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

rs.setBundledOutput documented

Started by Narathena, 16 April 2012 - 03:08 AM
Narathena #1
Posted 16 April 2012 - 05:08 AM
Ok I looked several places but never saw the base code written out in a manner that made sense to me.

To make changes to a wire in a bundled cable attached to your computer use the following:

rs.setBundledOutput("back",rs.getBundledOutput("back") + colors.orange)

What to change:

rs.setBundledOutput("back",rs.getBundledOutput("back") + colors.orange)

Side in this case back is where the bundled cable you want to make changes to connects to your computer.
On/Off the plus sign tells the code to turn on the wire, the minus will turn the wire off.
Color which color wire do you want to turn on?


What does what:

rs.setBundledOutput("back",rs.getBundledOutput("back") + colors.orange)

rs.setBundledOutput (side, value)
This sets the level of the redstone bundle to a certain value overwriting the previous value completely. if you don't tell it what the current state is and what changes you want to make are you will turn on or off all the wires in the bundle.

rs.getBundledOutput("back")
This gets the current value of the bundled cable from the back of the computer. If you look in the colors file you'll find a longer list of the following:

white = 1
orange = 2
magenta = 4
lightBlue = 8

+ colors.orange

This tells the code you want to add two to the current value to turn on the orange wire.


Example:
You currently have the white and magenta wires powered ( white = 1, magenta = 4 ) you want to turn on the orange wire (orange = 2)

rs.setBundledOutput ("back", rs.getBundledOutput("back") + colors.orange)
ie
Set the "back" bundled cable equal to rs.getBundledOutput("back") plus 2

The bundled cable's value pulled by rs.getBundledOutput("back") will be equal to the sum of the cables in the bundle that are powered, in this case 1+4=5.

So this code is telling the computer to Set the bundled cable in back equal to 7 (5 + 2). The computer is smart enough to figure out to get 7 it needs 1+2+4. For how look up binary on google.


Bugs you can run into:

What happens without rs.getBundledOutput(side) or a empty variable?
The value of the cable will be set to orange 2 (0+2=2) and the white and magenta cables would be turned off.

I'm not certain but…. it looks like if you attempted to subtract orange from white and magenta without orange being on to start (1+4-2=3) would get you white and orange powered on (3=1+2) with magenta off. I haven't tested it.

If your math is off or you don't check your variables you'll get screwy results.
Luanub #2
Posted 16 April 2012 - 08:27 AM
Ok I looked several places but never saw the base code written out in a manner that made sense to me.

To make changes to a wire in a bundled cable attached to your computer use the following:

rs.setBundledOutput("back",rs.getBundledOutput("back") + colors.orange)

What to change:

rs.setBundledOutput("back",rs.getBundledOutput("back") + colors.orange)

Side in this case back is where the bundled cable you want to make changes to connects to your computer.
On/Off the plus sign tells the code to turn on the wire, the minus will turn the wire off.
Color which color wire do you want to turn on?


What does what:

rs.setBundledOutput("back",rs.getBundledOutput("back") + colors.orange)

rs.setBundledOutput (side, value)
This sets the level of the redstone bundle to a certain value overwriting the previous value completely. if you don't tell it what the current state is and what changes you want to make are you will turn on or off all the wires in the bundle.

rs.getBundledOutput("back")
This gets the current value of the bundled cable from the back of the computer. If you look in the colors file you'll find a longer list of the following:

white = 1
orange = 2
magenta = 4
lightBlue = 8

+ colors.orange

This tells the code you want to add two to the current value to turn on the orange wire.


Example:
You currently have the white and magenta wires powered ( white = 1, magenta = 4 ) you want to turn on the orange wire (orange = 2)

rs.setBundledOutput ("back", rs.getBundledOutput("back") + colors.orange)
ie
Set the "back" bundled cable equal to rs.getBundledOutput("back") plus 2

The bundled cable's value pulled by rs.getBundledOutput("back") will be equal to the sum of the cables in the bundle that are powered, in this case 1+4=5.

So this code is telling the computer to Set the bundled cable in back equal to 7 (5 + 2). The computer is smart enough to figure out to get 7 it needs 1+2+4. For how look up binary on google.


Bugs you can run into:

What happens without rs.getBundledOutput(side) or a empty variable?
The value of the cable will be set to orange 2 (0+2=2) and the white and magenta cables would be turned off.

I'm not certain but…. it looks like if you attempted to subtract orange from white and magenta without orange being on to start (1+4-2=3) would get you white and orange powered on (3=1+2) with magenta off. I haven't tested it.

If your math is off or you don't check your variables you'll get screwy results.

Its better if you are using multiple colors to use the colors API in conjunction with the redstone API. Type help redpower from a terminal for more information an example..


-- set a color and turn it on
c = colors.combine(colors.red)
rs.setBundledInput("side", c )

-- add a color and turn it on, leaving red on as well
c = colors.combne( c, colors.orange )
rs.setBundledInput("side", c )

-- turn off red, leave orange on
c = colors.subtract( c, colors.red )
rs.setBundledInput("side", c )

-- add multiple colors
c = colors.combine ( c , colors.white, colors.blue, colors.black, etc.. ) -- without the c will create a new list
rs.setBundledInput("side", c )

-- remove multiple colors
c = colors.subtract ( c , colors.white, colors.blue, colors.black, etc.. )
rs.setBundledInput("side", c )

This should keep your vars in check and avoid any weird problems when handling multiple colors on a bundle. You can also use the c var for other functions.
Hawk777 #3
Posted 16 April 2012 - 10:10 AM
To clarify the above: colours are handled as binary bits within an integer. Using plus and minus to manipulate them will work properly, if, as Narathena said, you ensure the colour is present before subtracting or absent before adding. The colours API delegates to the bit API which does logical binary operations, which work properly even if the precondition isn't met. For example, (colours.white + colours.white) will give colours.orange, but colours.combine(colours.white, colours.white) will give colours.white. That’s why the API functions are preferred.
Hawk777 #4
Posted 16 April 2012 - 10:12 AM
Also, you may use this function I wrote if you wish which turns one or more colours on or off without affecting the other colours (the comment says it only works for one colour, but it should also work if you pass a combination of colours as “colour”):

-- Function setWire(side, colour, value)
--
-- Sets a single wire in a bundled cable in a single direction to a specific value.
--
-- Parameters:
--   side - the side of the computer console on which the bundled cable is attached
--   colour - the colour of the wire to turn on or off
--   value - true to turn the wire on, or false to turn it off
setWire = function(side, colour, value)
	assert(type(side) == "string", "rwutil.setWire: "side" must be a string")
	assert(type(colour) == "number", "rwutil.setWire: "colour" must be a number")
	assert(type(value) == "boolean", "rwutil.setWire: "value" must be a boolean")

	local old = redstone.getBundledOutput(side)
	if value then
		redstone.setBundledOutput(side, bit.bor(old, colour))
	else
		redstone.setBundledOutput(side, bit.band(old, bit.bnot(colour)))
	end
end
Narathena #5
Posted 16 April 2012 - 03:58 PM
Lots of stuff here

Its better if you are using multiple colors to use the colors API in conjunction with the redstone API. Type help redpower from a terminal for more information an example..


-- set a color and turn it on
c = colors.combine(colors.red)
rs.setBundledInput("side", c )

-- add a color and turn it on, leaving red on as well
c = colors.combne( c, colors.orange )
rs.setBundledInput("side", c )

-- turn off red, leave orange on
c = colors.subtract( c, colors.red )
rs.setBundledInput("side", c )

-- add multiple colors
c = colors.combine ( c , colors.white, colors.blue, colors.black, etc.. ) -- without the c will create a new list
rs.setBundledInput("side", c )

-- remove multiple colors
c = colors.subtract ( c , colors.white, colors.blue, colors.black, etc.. )
rs.setBundledInput("side", c )

This should keep your vars in check and avoid any weird problems when handling multiple colors on a bundle. You can also use the c var for other functions.

I didn't use the code you've posted because I didn't understand that a empty var 'c' would cause only one color to be on at a time. I do refer to the colors api ect but that doesn't explain why it knows which color is which, that I attempted to explain. (because it wasn't clear to me)

:)/>/>

But weither you are using the API or the hard numbers can you screw up your results by double adding or double subtracting? or is there some check in the system to verify you're not breaking this?

Nevermind I seem to have missed Hawk777's first post.