The microcontroller component is the control interface exposed by a placed microcontroller block. A microcontroller is a compact computer with limited hardware capacity, no external component bus access, and a small set of callbacks for power state and side-based network forwarding.
- Repository:
opencomputers - Lua component name:
microcontroller - Typical host: placed microcontroller block
- Starts and stops the embedded machine.
- Reports whether the embedded machine is running and why it last crashed.
- Controls which physical sides are allowed to forward
network.messagetraffic between the microcontroller's internal network and the outside world.
- Microcontrollers are assembled from a microcontroller case plus internal parts such as CPU, memory, cards, and an EEPROM.
- They are smaller and cheaper than full computers, but they cannot directly access arbitrary external components the way a normal computer case can.
- The side-facing API only applies to the five sides that are not the block's front-facing side. Passing the facing side is invalid.
- All side outputs are enabled by default.
- Syntax:
microcontroller.start() - Returns:
boolean - Purpose: Starts the embedded machine.
Return value:
trueif the state changed and the machine actually started.falseif it was already running or could not start from the current state.
- Syntax:
microcontroller.stop() - Returns:
boolean - Purpose: Stops the embedded machine.
- Syntax:
microcontroller.isRunning() - Returns:
boolean - Purpose: Checks whether the embedded machine is currently running.
- Syntax:
microcontroller.lastError() - Returns:
stringornil - Purpose: Returns the most recent crash reason, if the machine failed.
- Syntax:
microcontroller.isSideOpen(side) - Parameters:
side:number: OpenComputers side constant for a non-facing side.
- Returns:
boolean - Purpose: Checks whether messages are currently allowed to leave through that side.
- Syntax:
microcontroller.setSideOpen(side, open) - Parameters:
side:number: OpenComputers side constant for a non-facing side.open:boolean:trueto allow forwarding,falseto block it.
- Returns:
boolean - Purpose: Changes side forwarding state and returns the previous value.
- Messages received from external networks are copied into the microcontroller's internal network.
- Messages generated by the internal network are forwarded out through every side whose output is open.
- Closing a side only blocks forwarding on that side; it does not shut down the machine.
local component = require("component")
local sides = require("sides")
local mcu = component.microcontroller
if not mcu.isRunning() then
assert(mcu.start(), "microcontroller did not start")
end
local previous = mcu.setSideOpen(sides.north, false)
print("North side was previously open:", previous)
print("North side open now:", mcu.isSideOpen(sides.north))
local crash = mcu.lastError()
if crash then
print("Last crash:", crash)
endcomputerroboteepromredstone