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

Breaking down Bundled Cable Inputs

Started by Cottonbaler, 28 November 2015 - 11:20 PM
Cottonbaler #1
Posted 29 November 2015 - 12:20 AM
My question concerns the use of Bundled Cables; Specifically breaking down results from rs.getBundledInput(side) into a useable result to toggle and print color-specific "on/off" results.

I have been able to retrieve the aggregate numerical value of the total combined input by making use of:

print(textutils.serialize(rs.getBundledInput(side))

However, I have not been able to break that result down into individual colors. I could, theoretically write an if/then statement for every possible combination of color but I have a feeling that there is a much more practical way to do it. Thanks for the help!
Bomb Bloke #2
Posted 29 November 2015 - 12:58 AM
See the colours API. Given that each colour is represented as a power of two, we can loop through them like this:

local side = "right"  -- Or whatever

while true do
	term.clear()
	term.setCursorPos(1, 1)

	local input = rs.getBundledInput(side)

	for i = 0, 15 do
		term.setTextColour(2^i)
		-- 2^0 == 1 == colours.white
		-- 2^1 == 2 == colours.orange
		-- etc

		if colours.test(input, 2^i) then
			print("On")
		else
			print("Off")
		end
	end
	
	os.pullEvent("redstone")  -- Yield (do nothing) until a redstone state change occurs.
end

Also check rs.testBundledInput().
Dragon53535 #3
Posted 29 November 2015 - 04:30 AM
Probably would be better just to loop through the colors table, and test for each color.


local side = "right"
local input = rs.getBundledInput(side)
local colWires = {}
--#Variable initialization
--#Loop through all colors that are possible
for a,v in pairs(colors) do
 
  --# Fill our table to have the words on or off based on if the color is inside our input.
  colWires[a] = colors.test(input,v) and "on" or "off"
end

--#Print out the states
for(a,v in pairs(colWires) do
  print(a.." : "..v)
end
Bomb Bloke #4
Posted 29 November 2015 - 07:03 AM
colours.test() will probably complain once you start passing it the functions in that table.
Cottonbaler #5
Posted 29 November 2015 - 12:17 PM
Thanks for the response guys. Can you please review this to ensure I understand everything correctly? That would be much appreciated.



local side = "right"  ------------------------------------ Variable side
while true do -------------------------------------------- Loop to run constantly (except you threw in an os.pullEvent)
	    term.clear()---------------------------------------- garbage cleanup
	    term.setCursorPos(1, 1)----------------------- keeps the screen from scrolling
	    local input = rs.getBundledInput(side)  -- using the bundled input as a variable that can be read later
	    for i = 0, 15 do --------------------------------- a for loop set to, including 0, have 16 inputs for 16 colors
			    term.setTextColour(2^i)---------------- having the text color change depending on 'i', which is the variable in the for loop
			    -- 2^0 == 1 == colours.white---------- telling the computer that the result of 2^x = whatever the flag register is
			    -- 2^1 == 2 == colours.orange
			    -- etc
			    if colours.test(input, 2^i) then--------- checking to see if the "input" variable has the "2^i" bit switched on.
					    print("On")
			    else
					    print("Off")
			    end
	    end
	   
	    os.pullEvent("redstone")  -- Yield (do nothing) until a redstone state change occurs.
end

My next question is, since this loop will be inside of a larger script, will the computer be constantly checking for

os.pullEvent("redstone")

Or do I have to tell it to check?
Bomb Bloke #6
Posted 29 November 2015 - 02:59 PM
Thanks for the response guys. Can you please review this to ensure I understand everything correctly?

That more or less seems to be in order, so long as you understand that my commented lines aren't telling the computer anything - they're meant for you! ;)/>

I'm also technically using colours.test() to check that the i'th bit is switched on (as opposed to the 2^i'th bit). By raising two to the power of i, the result is a number with bit i set to true.

But I suspect I'm picking at semantics, and you get what's going on.

My next question is, since this loop will be inside of a larger script, will the computer be constantly checking for

os.pullEvent("redstone")

Or do I have to tell it to check?

That line requests a redstone event, and does nothing until one occurs. It doesn't actually bother to assign the data from that event to a variable - its sole purpose is to cause the script to yield until there's more potential work for it to do.

If your main script has other tasks it has to handle, such as user input, then you may not want to have it specifically pause until redstone events occur (as you'd be interested in more event types than just that one). It may also be important for you to capture the data os.pullEvent() returns, instead of just "waiting for an event".

It's difficult to give a concrete answer without more information about what your script looks like and what you want it to do. Likewise, that's why my above example is so abstract; it demonstrates that you can check all colours with a simple "for" loop that calculates powers of two, but depending on the actual nature of your script, it may not be the best way to do things.

It may be that you could get more overall value out of this technique, which Dragon made use of in his code snippet to trim this sort of thing:

if colors.test(input,v) then
	colWires[a] = "on"
else
	colWires[a] = "off"
end

… down to just this:

colWires[a] = colors.test(input,v) and "on" or "off"
Dragon53535 #7
Posted 29 November 2015 - 06:13 PM
colours.test() will probably complain once you start passing it the functions in that table.
Riight, whoops, forgot that the colors table holds the functions.

local side = "right"
local input = rs.getBundledInput(side)
local colWires = {}
--#Variable initialization
--#Loop through all colors that are possible
for a,v in pairs(colors) do
  if type(v) == "number" then
    --# Fill our table to have the words on or off based on if the color is inside our input.
    colWires[a] = colors.test(input,v) and "on" or "off"
  end
end
--#Print out the states
for(a,v in pairs(colWires) do
  print(a.." : "..v)
end
That should probably work now :P/>