Skip to content

Device I/O

Pin- and id-based logic access uses ic.read / ic.write (and related helpers on ic). The same functions are also available as logic_read / logic_write / logic_batch_read / … on the global environment for IC10-style naming.

Devices are connected to the IC housing via device pins (d0 through d5). The housing itself is accessible as db, which in Lua is ic.const.BASE_UNIT_INDEX (typically index 6).

Reading Logic Values

lua
local LT = ic.enums.LogicType

-- Read temperature from device on pin d0
local temp = ic.read(0, LT.Temperature)

-- Returns nil if the device is missing or disconnected
if temp == nil then
    print("No device on d0!")
end

By Device Index

lua
-- ic.read(deviceIndex, logicType [, networkIndex])
local temp = ic.read(0, LT.Temperature)
local temp = ic.read(0, LT.Temperature, 0)  -- with network index

By Reference ID

For stable addressing that doesn't depend on pin wiring. Get a device's ReferenceId via device_list() or ic.find():

lua
-- Find a device by its label (set with the Labeller tool)
local id = ic.find("My Sensor")
if id then
    local temp = ic.read_id(id, LT.Temperature)
    ic.write_id(id, LT.On, 1)
end

-- Or get all matching devices
local ids = ic.find_all("Solar Panel")
for _, id in ipairs(ids) do
    ic.write_id(id, LT.On, 1)
end

Writing Logic Values

lua
-- ic.write(deviceIndex, logicType, value [, networkIndex])
ic.write(0, LT.On, 1)
ic.write(1, LT.Setting, 42)

-- By reference ID
-- ic.write_id(deviceId, logicType, value [, networkIndex])
ic.write_id(id, LT.On, 1)

WARNING

ic.write() can throw if the device is missing. Use pcall() for safe writes:

lua
local ok, err = pcall(ic.write, 0, LT.On, 1)

Device Labels & Names

lua
-- Set the display name of a device
device_label(0, "Main Sensor")
ic.device_label(0, "Main Sensor")  -- same; nested form is ic.device.label(...)

-- Get the live display name
local name = device_name(0)

-- Get a prefab name from a hash
local prefab = prefab_name(-2045627372)  -- "SolarPanel"

-- Resolve a nameHash by scanning visible devices
local resolved = namehash_name(deviceHash, nameHash)

Device List

Enumerate all devices visible on the chip's data cable network:

lua
local devices = device_list()

for i, dev in ipairs(devices) do
    print(string.format("%d: %s (id=%d, prefab=%d)",
        i, dev.display_name, dev.ref_id, dev.prefab_hash))
end

Each entry contains:

FieldTypeDescription
ref_idnumberDevice ReferenceId (for ic.read_id / ic.write_id)
prefab_hashnumberPrefab hash (for ic.batch_read / ic.batch_write)
name_hashnumberName hash
display_namestringCurrent display name

Accepts an optional networkIndex: device_list(networkIndex).

Network Index

Most read/write functions accept an optional networkIndex parameter for multi-network setups:

lua
local value = ic.read(0, LT.Temperature, 0)
local ch0 = ic.read(ic.const.BASE_UNIT_INDEX, LT.Channel0, 0)

Function Reference

FunctionReturnsDescription
ic.read(dev, logicType [, net])number | nilRead logic value
ic.write(dev, logicType, value [, net])Write logic value
ic.read_id(id, logicType [, net])number | nilRead by ReferenceId
ic.write_id(id, logicType, value [, net])Write by ReferenceId
ic.find(name [, net])number | nilFind device by label, returns ReferenceId
ic.find_all(name [, net])number[]Find all devices by label, returns ReferenceId array
device_name(dev [, net])string | nilGet device display name
device_label(dev, name)Set device label (also ic.device_label)
device_list([net])table[]List all network devices
prefab_name(hash)string | nilHash → prefab name
namehash_name(devHash, nameHash [, net])string | nilResolve nameHash
raise_error(state)Set the IC housing error state (1=error, 0=clear)
clear_error()Clear the IC housing error state
hcf()Halt and catch fire (stops the chip)

⚠️ This documentation was AI-generated and may contain inaccuracies. Please submit pull requests with corrections as needed.