Skip to content

IO Controller

CD4017BE edited this page Jan 12, 2020 · 1 revision

The Signal IO Controller provides special integration for interacting with OpenComputers. Although you could use the Redstone functionality of Computers or Redstone I/O blocks connected to Redstone Sockets for communication, the Signal IO Controller is compact all-in-one solution and it also offers special features for timing control.

The block contains:

  • a Network Node so it can connect to Open Computers networks via Cable. The component ID is rsio.
  • 8 Signal input ports that can be read by software and configured to trigger events when input values change.
  • 8 Signal output ports that can be written by software asynchronously while the signal is updated during post-tick.
  • 4 Energy buffer ports that provide access to the energy stored in the Open Computers network.

Direct IO

value = rsio.getInput(portId)
reads the signal received at an input port: portId is the number 0...7 of the port and value the signal read from it as integer. This operation is performed asynchronously from a lua thread.

rsio.setOutput(portId, value)
sets the signal to emit at an output port: portId is the number 0...7 of the port and value the signal to write as integer (gets rounded if needed). The signal change is scheduled from the lua thread and then later executed by the device during the next post-tick phase updating all output ports simultaneously.

Interrupt Inputs

rsio.registerEvent(ports)
will make the computer receive events when inputs change their signal value. ports is a bit-mask defining which of the ports should trigger events, so for example the number 5 = 0b101 will enable events for ports 0 and 2.

Then, whenever the value received at a registered input port changes, the IO Controller will send a "rs_change", device_address, portId, value event to the registered computer that could be handled like this:

event, src, pin, value = event.pull()
if event == "rs_change" then
  -- do something
end

Only one computer can be registered for events at a time. If another computer calls the register function on the component, the previous computer gets unregistered.

Buffered IO

Since the lua programs run in their own thread (that might be busy with other computers as well) it generally can't be guaranteed that a particular function is called at a particular point in time. This is problematic if your application requires tick precise timing for communication with Redstone Control devices.

The solution is to let the IO Controller buffer the input and output signals for the computer so it can process them at its own pace.

rsio.bufferInput(ports, ticks)
rsio.bufferOutput(ports, ticks)
will configure buffering for inputs and outputs respectively. ports is the number of ports to buffer, when ports = 5 for example it will buffer the ports 0...4 (you have to wire the ports accordingly). And ticks is the number of ticks to buffer for (which is rounded up to the next power of two), it basically determines how much timing wriggle-room the program has. The total buffer size ports * ticks must not exceed 64 entries.

The IO Controller controls its buffering with an internal timer that can be read via time = rsio.time() and reset via rsio.time(0). Buffered signals are then read and written using:
value = rsio.getInput(port, time)
rsio.setOutput(port, time, value)

Here the time parameter controls when exactly the operation should happen, you always have to read the past time < rsio.time() and write the future time > rsio.time() (while also staying within the I/O buffer boundaries).

Example code:

rs = component.rsio

--configure I/O buffering over 4 ticks
rs.bufferInput(1, 4)
rs.bufferOutput(1, 4)

--reset timer
t = rs.time(0)
while true do
  --wait until new input is available
  if rs.time() <= t then
    os.sleep(0.1) --2 ticks to save power
  end
  
  --read input
  valIn = rs.getInput(0, t)
  
  --do some computation
  valOut = valIn * 5 - 3
  
  --schedule output for 4 ticks later
  rs.setOutput(0, t + 4, valOut)
  
  t = t + 1
end

Clone this wiki locally