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
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!")
endBy Device Index
-- ic.read(deviceIndex, logicType [, networkIndex])
local temp = ic.read(0, LT.Temperature)
local temp = ic.read(0, LT.Temperature, 0) -- with network indexBy Reference ID
For stable addressing that doesn't depend on pin wiring. Get a device's ReferenceId via device_list() or ic.find():
-- 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)
endWriting Logic Values
-- 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:
local ok, err = pcall(ic.write, 0, LT.On, 1)Device Labels & Names
-- 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:
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))
endEach entry contains:
| Field | Type | Description |
|---|---|---|
ref_id | number | Device ReferenceId (for ic.read_id / ic.write_id) |
prefab_hash | number | Prefab hash (for ic.batch_read / ic.batch_write) |
name_hash | number | Name hash |
display_name | string | Current display name |
Accepts an optional networkIndex: device_list(networkIndex).
Network Index
Most read/write functions accept an optional networkIndex parameter for multi-network setups:
local value = ic.read(0, LT.Temperature, 0)
local ch0 = ic.read(ic.const.BASE_UNIT_INDEX, LT.Channel0, 0)Function Reference
| Function | Returns | Description |
|---|---|---|
ic.read(dev, logicType [, net]) | number | nil | Read logic value |
ic.write(dev, logicType, value [, net]) | — | Write logic value |
ic.read_id(id, logicType [, net]) | number | nil | Read by ReferenceId |
ic.write_id(id, logicType, value [, net]) | — | Write by ReferenceId |
ic.find(name [, net]) | number | nil | Find device by label, returns ReferenceId |
ic.find_all(name [, net]) | number[] | Find all devices by label, returns ReferenceId array |
device_name(dev [, net]) | string | nil | Get 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 | nil | Hash → prefab name |
namehash_name(devHash, nameHash [, net]) | string | nil | Resolve 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) |
