Skip to content

Latest commit

 

History

History
106 lines (74 loc) · 3.22 KB

File metadata and controls

106 lines (74 loc) · 3.22 KB

microcontroller

Summary

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.

Availability

  • Repository: opencomputers
  • Lua component name: microcontroller
  • Typical host: placed microcontroller block

What It Does

  • 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.message traffic between the microcontroller's internal network and the outside world.

Hardware Notes

  • 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.

API

start

  • Syntax: microcontroller.start()
  • Returns: boolean
  • Purpose: Starts the embedded machine.

Return value:

  • true if the state changed and the machine actually started.
  • false if it was already running or could not start from the current state.

stop

  • Syntax: microcontroller.stop()
  • Returns: boolean
  • Purpose: Stops the embedded machine.

isRunning

  • Syntax: microcontroller.isRunning()
  • Returns: boolean
  • Purpose: Checks whether the embedded machine is currently running.

lastError

  • Syntax: microcontroller.lastError()
  • Returns: string or nil
  • Purpose: Returns the most recent crash reason, if the machine failed.

isSideOpen

  • 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.

setSideOpen

  • Syntax: microcontroller.setSideOpen(side, open)
  • Parameters:
    • side:number: OpenComputers side constant for a non-facing side.
    • open:boolean: true to allow forwarding, false to block it.
  • Returns: boolean
  • Purpose: Changes side forwarding state and returns the previous value.

Network Behavior

  • 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.

Example

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)
end

Related

  • computer
  • robot
  • eeprom
  • redstone