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

Redstone Tutorial!

Started by Engineer, 26 October 2013 - 12:40 PM
Engineer #1
Posted 26 October 2013 - 02:40 PM
Redstone tutorial!
You ever wanted to interact with redstone? Hopefully after this tutorial you can!

So, you want to learn about redstone? Then you are at the right place, because I want to discuss all redstone functions and things you need to know about them.
It is not that hard to do, but for beginners it can be a bit hard.

Prerequisites
To understand anything here, you should know all these things, like in my previous tutorial:Also, take this page from the ComputerCraft Wiki: Redstone

First I want to make clear what an API is. An API is a file full of functions, that we can call externally. With that I mean, we load a file, and we call the functions from that file. We can do that by doing: <fileName>.<functionCall>. Luckily for us, the redstone API is by standard already loaded for you. So you can call functions just like they are on the wiki.

Also, by default you can run a redstone.* function by doing rs.*
So a shorthand for redstone when calling a function is rs!

Now, here is an overview of all the functions and things you will need from the Redstone API.
redstone.getSides()This function returns in a table those strings:

top
bottom
left
right
front
back

So actually, this gets literally returned:

{
	[1] = "top",
	[2] = "bottom",
	[3] = "left",
	[4] = "right",
	[5] = "front",
	[6] = "back"
}
This hasn't anything to due with redstone really, so I leave it as it is.

redstone.getInput( String side )This function is very useful to us. We can check if on a side of the computer ( See redstone.getSides() for all the sides ) redstone is turned on.
So for example a little code:

for _, side in pairs( redstone.getSides() ) do
	if redstone.getInput( side ) then
		print( "Redstone is active on the " .. side .. " of this computer." )
	end
end

If we run that on this computer:

We get this output:


Makes sense, right? The back is turned on, the left is turned on but the fron is not turned on! And that wraps up this function.
Note that this only works if there is some redstone dust on the given side of the computer.

redstone.getOutput( String side )This is very different then the redstone.getInput( String side ).

This function does not check if the redstone dust is turned on or not, but it checks if the computer is outputting a redstone signal. How to do that, is by calling the next function in the overview.
So, if we set the output from our computer on the right, you can check with redstone.getOutput( String side ), where side is one of the sides of redstone.getSides(), if the computer is outputting a redstone signal.

I can asssure you this will make sense when you read the next function in the overview.

redstone.setOutput( String side, Boolean state )With this function we can set redstone signals on and off. It actually is not that hard when you think about it.
As you can see, the first argument of the function is the side. So if you go back to redstone.getNames(), you see what sides this are. You have to fill in one of those.
The second argument is to turn it on or off. We do this with a boolean, so true or false.

So, an example:

local function checkOutput( side )
	if redstone.getOutput( side ) then
		print( "The computer is outputting power on the " .. side .. " of the computer." )
	else
		print( "The computer is not outputting power on the " .. side .. " of the computer." )
	end

	if redstone.getInput( side ) then
		print( "The computer is receiving power from the " .. side )
	else
		print( "The computer is not receiving power from the " .. side )
	end
	print( "" )
end

redstone.setOutput( "front", true )
checkOutput( "front" )
checkOutput( "right" )

redstone.setOutput( "front", false)
checkOutput( "front" )
checkOutput( "right" )

If we run this code with the following setup:

We get as output:


Doesnt it make more sense now? As assignment I would give you this: play around with those functions you have learned :)/>

Bundled Cable functionsThose functions only get added when Redpower is recognised as mod.
For this you need to know a bit of binary logic.

We got 16 colors in minecraft, 0 is off and 1 is on. The colors go from left to right:

Black : red : green : brown : blue : purple : cyan : lightGray : gray : pink : lime : yellow : lightBlue : magenta : orange : white
   0  :  0  :   0   :   0   :   0  :   0	:   0  :	  0	:   0  :   0  :   0  :	0   :	 0	 :	0	:	0   :   0
If we concat that number, we get: 0000000000000000 ( 16x0 )
Which is equal to 0 in binary. Now lets turn on black:

Black : red : green : brown : blue : purple : cyan : lightGray : gray : pink : lime : yellow : lightBlue : magenta : orange : white
   1  :  0  :   0   :   0   :   0  :   0	:   0  :	  0	:   0  :   0  :   0  :	0   :	 0	 :	0	:	0   :   0
If we concat that, we get: 1000000000000000.
Now we want to get the value of black. But before that we must count from right to left. Thats against yourself in, but you should get used to it when you are talking in binary. Also you start counting with 0, not 1!
The number of that 1, counting right to left, is 15. Now in order to get that value we have a formula:

2^n
Where n is the number when counting right to left
So black is: 2^15 = 32768

This is your foundation for the following functions.

redstone.getBundledInput( String side )This function returns all colors in binary value of the bundled cable. So lets imagine that black and yellow are turned on:

Black : red : green : brown : blue : purple : cyan : lightGray : gray : pink : lime : yellow : lightBlue : magenta : orange : white
   1  :  0  :   0   :   0   :   0  :   0	:   0  :	  0	:   0  :   0  :   0  :	1   :	 0	 :	0	:	0   :   0

First we are going to count (Right to left and start with 0) where the number one is turned on.
Black is 15 and yellow is 4.

We take the basic formula, you should know this, and we do this:
Black = 2 ^ 15 = 32768
Yellow = 2 ^ 4 = 16

So the output of redstone.getBundledInput( String side ) = 32768 + 16 = 32784

Now, how do you figure out how that number contains a certain color? For that we use the colors API.
And in particular colors.test( Int allcolors, Int testForcolor )

So if we do: colors.test( 327684, 4 ), then it should return true.
On another not, there is another neat trick in the colors API so we can do this:

colors.test( 327684, colors.yellow )

Now, I wrote a little function for you guys so you can get all the colors. This function returns all colors in a table, so for example: { "red", "green" }
test function

local function getColors( color )
	if type( color ) ~= "number" then
		error( "Number expected, got " .. type( colors ), 2 )
	end

	if color > 65535 then
		error( "Color is out of range!", 2 )
	end

	local positiveColors = {}
	for key, value in pairs( colors ) do
		if type( value ) == "number" then
			if colors.test( color, value )
				table.insert( positiveColors, key )
			end
		end
	end

	return positiveColors
end
This is everything that you know already. I will write an explanation for this if people really want it.
This input/output thing is the same as the redstone.getOutput and redstone.getInput thing!

redstone.getBundledOutput( String side )Well this is basically the same as redstone.getBundledInput, only you should note the difference between input and output.

redstone.setBundledOutput( String side, Int color )With this function we can activate certain colors from the bundled cable.

Lets imagine that we want to turn on black and yellow. (Yes, this is copy&amp;paste :P/>)

Black : red : green : brown : blue : purple : cyan : lightGray : gray : pink : lime : yellow : lightBlue : magenta : orange : white
   1  :  0  :   0   :   0   :   0  :   0	:   0  :	  0	:   0  :   0  :   0  :	1   :	 0	 :	0	:	0   :   0
First we put a 1 by the colors we want. Then we start counting (Right to left and start with 0) where the number one is turned on.
Black is 15 and yellow is 4.

We take the basic formula, you should know this, and we do this:
Black = 2 ^ 15 = 32768
Yellow = 2 ^ 4 = 16

So add those together, and those get turned on by the function where this is all about.

Now you know how this is done, you can simply call the function from the colors API:
colors.combine( colors.yellow, colors.black ) == 32768 + 16

redstone.testBundledInput( String side, Int color)With this function you can test if a bundled cable contains color(s). This is just a wrapper for color.test and redstone.getBundledInput in one function.

I have yet to include 3 functions, but I will add those in a future version. Leave some feedback!
Edited on 22 January 2014 - 10:00 AM
TekkitwithRiley #2
Posted 01 November 2013 - 03:39 AM
thanks this was really helpful
Vicirga #3
Posted 03 November 2013 - 11:48 PM
I have seriously been trying to fix my program all day and this is what fixed it. Thanks so much!!! =)
Sora Firestorm #4
Posted 04 November 2013 - 12:09 PM
Nice. One thing I'd change, though, rather than 'RedPower functions', I'd go with 'Bundled Cable functions', so that people know exactly what it's for (just to eliminate confusion) and also that MFRs Rednet cabling also works with these functions. Everything else is fine, though. Good job.
Engineer #5
Posted 22 January 2014 - 11:01 AM
thanks this was really helpful
Im very glad that it is helpfull!

I have seriously been trying to fix my program all day and this is what fixed it. Thanks so much!!! =)
:)/>

Nice. One thing I'd change, though, rather than 'RedPower functions', I'd go with 'Bundled Cable functions', so that people know exactly what it's for (just to eliminate confusion) and also that MFRs Rednet cabling also works with these functions. Everything else is fine, though. Good job.
Done, and thanks :D/>

I forgot to follow this topic, so I didnt see the replies.. Though I see them now!