Component/redstone: Difference between revisions

From OpenComputers: Rebooted
Jump to navigation Jump to search
Clean up and rewrite non-component-API parts of the page
m Fix lack of line breaks for one of the API descriptions
 
(3 intermediate revisions by the same user not shown)
Line 25: Line 25:
One of the significant advantages of using these overloaded variants is that any api call cost is paid once per call, not once per each value set.
One of the significant advantages of using these overloaded variants is that any api call cost is paid once per call, not once per each value set.


== Component API ==
Component name: <code>redstone</code>. Callbacks:
Component name: <code>redstone</code>. Callbacks:


* <code>getInput(side: number): number</code>
= Tier 1 API =
 
{{APICallback|name=getInput|signature=([side: number]): number or table|description=
Returns current incoming (non-bundled) redstone values. getInput(side) returns the redstone level from the specified [[API/sides|side]]. getInput() returns a table of redstone levels from all sides.
 
Note that the table returned is zero based. That is because the keys of the table are the ordinal values of the sides, and sides.bottom is 0.
 
Note also that the side is relative to the computer's orientation, i.e. sides.south is ''in front of the computer'', not south in the world. Likewise, sides.left is to the left of the computer, so when you look at the computer's front, it'll be to your right.
 
If you use mods such as RedLogic the input may exceed the vanilla values of [0, 15].


<pre>getInput(): table
</pre>
<pre>Returns current incoming (non-bundled) redstone values. getInput(side) returns the redstone level from the specified [[API/sides|side]]. getInput() returns a table of redstone levels from all sides.
</pre>
<pre>Note that the table returned is zero based. That is because the keys of the table are the ordinal values of the sides, and sides.bottom is 0.
</pre>
<pre>Note also that the side is relative to the computer's orientation, i.e. sides.south is ''in front of the computer'', not south in the world. Likewise, sides.left is to the left of the computer, so when you look at the computer's front, it'll be to your right.
</pre>
<pre>If you use mods such as RedLogic the input may exceed the vanilla values of [0, 15].
</pre>
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
lua> component.redstone.getInput(sides.left) 15 lua> component.redstone.getInput(sides.right) 4 lua> component.redstone.getInput() {[0]=7, 0, 0, 0, 4, 15} lua> component.redstone.getInput()[sides.bottom] 7
lua> component.redstone.getInput(sides.left)
15
lua> component.redstone.getInput(sides.right)
4
lua> component.redstone.getInput()
{[0]=7, 0, 0, 0, 4, 15}
lua> component.redstone.getInput()[sides.bottom]
7
</syntaxhighlight>
</syntaxhighlight>
 
}}
* <code>getOutput(side: number): number</code>
{{APICallback|name=getOutput|signature=([side: number]): number or table|description=
 
<pre>getOutput(): table
Gets the currently set output on the specified side, or the set value of all sides if called with no arguments
Gets the currently set output on the specified side, or the set value of all sides if called with no arguments
</pre>
</pre>
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
lua> component.redstone.getOutput(sides.left) 7 lua> component.redstone.getOutput() [sides.left] 7
lua> component.redstone.getOutput(sides.left)
7
lua> component.redstone.getOutput()[sides.left]
7
</syntaxhighlight>
</syntaxhighlight>
}}
{{APICallback|name=setOutput|signature=([side: number,] value: number): number or table|description=
Sets the strength of the redstone signal to emit. Returns the **old** value. This can be an arbitrarily large number for mods that support this. The first variant of this method takes a [[API/sides|side]] and the redstone strength to apply to just that side. setOutput(values) allows you to set the redstone strength of all sides (or a subset of sides) in a single call.
}}
{{APICallback|name=getComparatorInput|signature=(side:number):number|description=
Get the comparator input on the specified side.
}}
{{APICallback|name=getWakeThreshold|signature=(): number|description=
Gets the current wake-up threshold.
}}
{{APICallback|name=setWakeThreshold|signature=(threshold: number): number|description=
Sets the wake-up threshold to the specified number.
}}


* <code>setOutput(side: number, value: number): number</code>
= Tier 2 API =


<pre>setOutput(values: table): table
{{APICallback|name=getBundledInput|signature=([side: number, color: number]): number or table|description=
</pre>
Like getInput, but for bundled input, reading the value for the channel with the specified [[API/colors|API/Colors]].
<pre>Sets the strength of the redstone signal to emit. Returns the **old** value. This can be an arbitrarily large number for mods that support this. The first variant of this method takes a [[API/sides|side]] and the redstone strength to apply to just that side. setOutput(values) allows you to set the redstone strength of all sides (or a subset of sides) in a single call.
</pre>
* <code>getBundledInput(side: number, color: number): number</code>


<pre>getBundledInput(side: number): table
<code>getBundledInput(side, color)</code> returns the strength of the incoming redstone value on the specified side on the specified color channel.
getBundledInput(): table
</pre>
<pre>Like getInput, but for bundled input, reading the value for the channel with the specified [[API/colors|API/Colors]].\\
//As of OC 1.3: only available on a tier two redstone card.//
</pre>
<pre>getBundledInput(side, color) returns the strength of the incoming redstone value on the specified side on the specified color channel.
getBundledInput(side) returns a table (Map[Int, Int] structure) of redstone values on the specified side in a bundled pack, indexed by color.
getBundledInput() returns all redstone values, of all sides and all colors. It is a Map[Int, Map[Int, Int]] structure. The top level keys are in [0, 5] range, values of [[API/sides|sides]] (keep in mind sides.bottom is zero). The child map of each side is the same data structure returned by getBundledInput(side).
</pre>
* <code>getBundledOutput(side: number, color: number): number</code>


<pre>getBundledOutput(side: number): table
<code>getBundledInput(side)</code> returns a table (Map[Int, Int] structure) of redstone values on the specified side in a bundled pack, indexed by color.
getBundledOutput(): table
Like getOutput, but for bundled output, getting the values previously set that the device is emitting.\\
//As of OC 1.3: only available on a tier two redstone card.//
</pre>
* <code>setBundledOutput(side: number, color: number, value: number): number</code>


<pre>Like setOutput, but for bundled output, setting the value for the channel with the specified [[API/colors|API/Colors]]. Returns the previous values set.
<code>getBundledInput()</code><nowiki> returns all redstone values, of all sides and all colors. It is a Map[Int, Map[Int, Int]] structure. The top level keys are in [0, 5] range, values of </nowiki>[[API/sides|sides]] (keep in mind sides.bottom is zero). The child map of each side is the same data structure returned by getBundledInput(side).
}}
{{APICallback|name=getBundledOutput|signature=([side: number, color: number]): number or table|description=
Like getOutput, but for bundled output, getting the values previously set that the device is emitting.
}}
{{APICallback|name=setBundledOutput|signature=([side: number, color: number, value: number] or values: table): number|description=
Like setOutput, but for bundled output, setting the value for the channel with the specified [[API/colors|API/Colors]]. Returns the previous values set.
setBundledOutput(side, values) sets a pack of color-indexed redstone values, [0, 15]. colors.white is zero. The values table doesn't have to be contiguous, and values omitted are left unchanged.
setBundledOutput(side, values) sets a pack of color-indexed redstone values, [0, 15]. colors.white is zero. The values table doesn't have to be contiguous, and values omitted are left unchanged.
setBundledOutput(values) allows you to set redstone levels for any side and any color in a single api call.\\
setBundledOutput(values) allows you to set redstone levels for any side and any color in a single api call.
//As of OC 1.3: only available on a tier two redstone card.//
</pre>
</pre>
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
lua> component.redstone.setBundledOutput(sides.left, { [colors.white] = 15 } ) -- only sets 1 output lua> component.redstone.setBundledOutput({ [sides.left] = { [colors.white] = 15 } } ) -- same as above
lua> component.redstone.setBundledOutput(sides.left, { [colors.white] = 15 } ) -- only sets 1 output
lua> component.redstone.setBundledOutput({ [sides.left] = { [colors.white] = 15 } } ) -- same as above
</syntaxhighlight>
</syntaxhighlight>
}}
{{APICallback|name=getWirelessInput|signature=(): number|description=
Get the wireless redstone input.
}}
{{APICallback|name=getWirelessOutput|signature=(): boolean|description=
Get the wireless redstone output.
}}
{{APICallback|name=setWirelessOutput|signature=(value: boolean): boolean|description=
Set the wireless redstone output.
}}
{{APICallback|name=getWirelessFrequency|signature=(): number|description=
Get the currently set wireless redstone frequency.
}}
{{APICallback|name=setWirelessFrequency|signature=(frequency: number): number|description=
Set the wireless redstone frequency to use.
}}


* <code>getComparatorInput(side:number):number</code>
Note that for mods such as Project: Red, low values (such as the vanilla maximum of 15) may not function as expected for simple on/off values (opening a door for example), because they have a larger value range. You may need to use a higher value, such as 255.


<pre>Get the comparator input on the specified side.
= Example =
</pre>
<syntaxhighlight lang="lua">
* <code>getWirelessInput():number</code>
local component = require("component")
 
local sides = require("sides")
<pre>Get the wireless redstone input.\\
local colors = require("colors")
//Added in OC 1.3. Only available on tier two redstone cards.//
local rs = component.redstone -- get primary redstone component
</pre>
print(rs.getInput(sides.back))
* <code>getWirelessOutput():boolean</code>
rs.setBundledOutput(sides.bottom, colors.green, rs.getBundledInput(sides.top, colors.red))
 
</syntaxhighlight>
<pre>Get the wireless redstone output.\\
//Added in OC 1.3. Only available on tier two redstone cards.//
</pre>
* <code>setWirelessOutput(value:boolean):boolean</code>
 
<pre>Set the wireless redstone output.\\
//Added in OC 1.3. Only available on tier two redstone cards.//
</pre>
* <code>getWirelessFrequency():number</code>
 
<pre>Get the currently set wireless redstone frequency.\\
//Added in OC 1.3. Only available on tier two redstone cards.//
</pre>
* <code>setWirelessFrequency(frequency:number):number</code>
 
<pre>Set the wireless redstone frequency to use.\\
//Added in OC 1.3. Only available on tier two redstone cards.//
</pre>
* <code>getWakeThreshold():number</code>
 
<pre>Gets the current wake-up threshold.
</pre>
* <code>setWakeThreshold(threshold:number):number</code>
 
<pre>Sets the wake-up threshold to the specified number.
</pre>
Note that for mods such as ProjectRed, low values (such as the vanilla maximum of 15) may not function as expected for simple on/off values (opening a door for example), because they have a larger value range. You may need to use a higher value, such as 255.
 
== Example use: <syntaxhighlight lang="lua">
local component = require("component") local sides = require("sides") local colors = require("colors") local rs = component.redstone -- get primary redstone component print(rs.getInput(sides.back)) rs.setBundledOutput(sides.bottom, colors.green, rs.getBundledInput(sides.top, colors.red))
</syntaxhighlight> ==


{{:Component/contents}}
{{:Component/contents}}

Latest revision as of 01:29, 15 October 2026

Overview

This component represents a Redstone card or Redstone I/O block.

The tier one Redstone Card only supports vanilla redstone functionality (single-line analog redstone).

The tier two Redstone Card and Redstone I/O block provide interoperation with other mods' redstone systems (bundled, wireless).

In 1.21, we currently support the following mods for bundled redstone:

  • Project: Red
  • Immersive Engineering (as of 1.9.5)

We currently do not have bespoke support for any wireless redstone systems; they can be interacted with as regular redstone devices, but there is no way to set 'channels' or adjust other settings those mods may provide.

Accessing Sides

Redstone Cards always access sides relative to the computer they are in. Redstone I/O blocks always access sides absolutely in the world. To enhance maintainability and reduce confusion, it is recommended to use the appropriate aliases when using the OpenOS sides API: relative aliases for cards and absolute aliases for I/O blocks.

If you need to distinguish whether your redstone component is a card or a block, check the slot attribute. -1 means you have a block, >= 0 means you have a card.

get* and set* call costs

The get methods are "direct" calls and are practically free. From testing I was able to call getInput 20,000 to 22,000 times per second.

The set methods are not "direct", and thus can only be called once per tick, at most. Thus, ~20 times per second. In addition to their indirect cost (consuming the rest of the tick) if the set call changes any output levels, there is an additional delay imposed on the machine. By default set calls can change outputs approximately 6 times per second.

One of the significant advantages of using these overloaded variants is that any api call cost is paid once per call, not once per each value set.

Component name: redstone. Callbacks:

Tier 1 API

getInput

getInput([side: number]): number or table

Returns current incoming (non-bundled) redstone values. getInput(side) returns the redstone level from the specified side. getInput() returns a table of redstone levels from all sides.

Note that the table returned is zero based. That is because the keys of the table are the ordinal values of the sides, and sides.bottom is 0.

Note also that the side is relative to the computer's orientation, i.e. sides.south is in front of the computer, not south in the world. Likewise, sides.left is to the left of the computer, so when you look at the computer's front, it'll be to your right.

If you use mods such as RedLogic the input may exceed the vanilla values of [0, 15].

lua> component.redstone.getInput(sides.left)
15
lua> component.redstone.getInput(sides.right)
4
lua> component.redstone.getInput()
{[0]=7, 0, 0, 0, 4, 15}
lua> component.redstone.getInput()[sides.bottom]
7

getOutput

getOutput([side: number]): number or table

Gets the currently set output on the specified side, or the set value of all sides if called with no arguments

lua> component.redstone.getOutput(sides.left)
7
lua> component.redstone.getOutput()[sides.left]
7

setOutput

setOutput([side: number,] value: number): number or table

Sets the strength of the redstone signal to emit. Returns the **old** value. This can be an arbitrarily large number for mods that support this. The first variant of this method takes a side and the redstone strength to apply to just that side. setOutput(values) allows you to set the redstone strength of all sides (or a subset of sides) in a single call.

getComparatorInput

getComparatorInput(side:number):number

Get the comparator input on the specified side.

getWakeThreshold

getWakeThreshold(): number

Gets the current wake-up threshold.

setWakeThreshold

setWakeThreshold(threshold: number): number

Sets the wake-up threshold to the specified number.

Tier 2 API

getBundledInput

getBundledInput([side: number, color: number]): number or table

Like getInput, but for bundled input, reading the value for the channel with the specified API/Colors.

getBundledInput(side, color) returns the strength of the incoming redstone value on the specified side on the specified color channel.

getBundledInput(side) returns a table (Map[Int, Int] structure) of redstone values on the specified side in a bundled pack, indexed by color.

getBundledInput() returns all redstone values, of all sides and all colors. It is a Map[Int, Map[Int, Int]] structure. The top level keys are in [0, 5] range, values of sides (keep in mind sides.bottom is zero). The child map of each side is the same data structure returned by getBundledInput(side).

getBundledOutput

getBundledOutput([side: number, color: number]): number or table

Like getOutput, but for bundled output, getting the values previously set that the device is emitting.

setBundledOutput

setBundledOutput([side: number, color: number, value: number] or values: table): number

Like setOutput, but for bundled output, setting the value for the channel with the specified API/Colors. Returns the previous values set. setBundledOutput(side, values) sets a pack of color-indexed redstone values, [0, 15]. colors.white is zero. The values table doesn't have to be contiguous, and values omitted are left unchanged. setBundledOutput(values) allows you to set redstone levels for any side and any color in a single api call.

lua> component.redstone.setBundledOutput(sides.left, { [colors.white] = 15 } ) -- only sets 1 output
lua> component.redstone.setBundledOutput({ [sides.left] = { [colors.white] = 15 } } ) -- same as above

getWirelessInput

getWirelessInput(): number

Get the wireless redstone input.

getWirelessOutput

getWirelessOutput(): boolean

Get the wireless redstone output.

setWirelessOutput

setWirelessOutput(value: boolean): boolean

Set the wireless redstone output.

getWirelessFrequency

getWirelessFrequency(): number

Get the currently set wireless redstone frequency.

setWirelessFrequency

setWirelessFrequency(frequency: number): number

Set the wireless redstone frequency to use.

Note that for mods such as Project: Red, low values (such as the vanilla maximum of 15) may not function as expected for simple on/off values (opening a door for example), because they have a larger value range. You may need to use a higher value, such as 255.

Example

local component = require("component")
local sides = require("sides")
local colors = require("colors")
local rs = component.redstone -- get primary redstone component
print(rs.getInput(sides.back))
rs.setBundledOutput(sides.bottom, colors.green, rs.getBundledInput(sides.top, colors.red))
Components
Core Components Computer - Drone - Drive - Filesystem - GPU - Microcontroller - Modem - Redstone - Robot
Others Component Access - Signals
Cross-Mod Integration Applied Energistics