I2CInterface#
- class I2CInterface(
- name: str,
- driver: I2CDriverBase,
- system_definition: SystemDefinition,
- publishers: list[Publisher] | None = None,
- **kwargs,
Bases:
InstrumentI2C interface supporting register-based and command-based devices.
Commands are enum values OR’d together and sent as a single byte stream. The
SystemDefinitionis read-only —I2CInterfaceresolves names to transactions on every call; the vendor driver never sees it.Initialize an I2CInterface.
- Parameters:
name
Channel-name prefix for published data.
driver
Concrete I2C-adapter driver; owns its own transport:
i2c = I2CInterface( "main", driver=Aardvark(serial_number="2239-764425"), system_definition=system, )
system_definition
Bus description (devices, register maps, comms params).
publishers
Publishers that receive emitted Measurement/Command data.
**kwargs
Default tags applied to every emitted Measurement/Command. Pass
dataset_rid="<rid>"to auto-create a NominalCorePublisher (uses the on-disk ‘default’ Nominal credential).
Attributes
Seconds between background daemon iterations (0 = no wait).
Return an insertion-ordered snapshot of all published channel names.
Methods
Append
methodto the daemon's call list.Register a publisher to receive this instrument's Measurement/Command data.
Close the underlying driver and stop the daemon.
Replace all daemon functions with a single
method(called with the given args).Return the most recent
lengthsamples forchannel_namefrom the in-memory buffer.Return the most recent sample for
channel_namefrom the in-memory buffer.Open the underlying I2C-adapter driver.
Fan
dataout to every configured publisher;kwargspass through.Send a batch command (OR of its enum members) to
peripheraland read the response.Read a register (or one of its named fields) on
peripheral.Read
lengthbytes from the 7-bit I2Caddressand decode as an integer.Write a register's defined default value back to hardware (initialization/recovery).
Start the background daemon thread.
Signal the background daemon to stop and join it.
Write a register (or one of its named fields) on
peripheral.Write
datato the 7-bit I2Caddress.Write-then-read without an intermediate I2C STOP (uses repeated START); decode as integer.
Write-then-read with an I2C STOP between operations (required by devices that need STOP to process input).
- write_read_raw( ) int[source]#
Write-then-read without an intermediate I2C STOP (uses repeated START); decode as integer.
Typical use: write a register address, then read its contents.
- Parameters:
address
7-bit I2C device address.
payload
Bytes to write (typically the register address).
length
Number of bytes to read back.
endianness
Byte order for the integer decode.
- write_then_read_raw( ) int[source]#
Write-then-read with an I2C STOP between operations (required by devices that need STOP to process input).
- read( ) Measurement[source]#
Read a register (or one of its named fields) on
peripheral.With
field, only the named bit field is extracted (masked and shifted). RaisesValueErrorifperipheralis not a register-based device.
- write( ) Command[source]#
Write a register (or one of its named fields) on
peripheral.With
field, performs a thread-safe read-modify-write of just that field (mask, insert, write). RaisesValueErrorifperipheralis not a register-based device.
- reset_reg(peripheral: str, register_alias: str, **kwargs)[source]#
Write a register’s defined default value back to hardware (initialization/recovery).
- query( ) Measurement[source]#
Send a batch command (OR of its enum members) to
peripheraland read the response.Uses write-then-read with an I2C STOP between operations. Raises
ValueErrorifperipheralis not a command-based device, or ifbatch_commandreferences undefined command enums.
- add_background_daemon_function(
- method: Callable,
- *args,
- **kwargs,
Append
methodto the daemon’s call list. Usedefine_background_daemonto replace instead.
- add_publisher(
- publisher: Publisher,
Register a publisher to receive this instrument’s Measurement/Command data.
- property background_interval#
Seconds between background daemon iterations (0 = no wait).
- property channel_names: tuple[str, ...]#
Return an insertion-ordered snapshot of all published channel names.
Names are complete after one full daemon iteration: {name}.loop_time publishes every iteration, so call get_channel(“loop_time”, wait_for_new_samples=True) then read channel_names to ensure the complete set has arrived.
- Raises:
RuntimeError – No background buffer;
start()was not called.
- define_background_daemon(
- method: Callable,
- *args,
- **kwargs,
Replace all daemon functions with a single
method(called with the given args).
- get_channel( ) Measurement#
Return the most recent
lengthsamples forchannel_namefrom the in-memory buffer.If the channel does not exist yet, or no sample is available, the code will always block until
timeoutexpires.- Parameters:
channel_name
Name of the channel to retrieve.
length
Number of trailing samples to return.
wait_for_new_samples
Block until at least
lengthnew values arrive.timeout
Seconds to wait when insufficient data exists or
wait_for_new_samples=True.
- Raises:
RuntimeError – No background buffer;
start()was not called.ChannelNotFoundError – channel had no values and no data appeared before
timeout.wait_for_new_samples=Trueand channel did not appear withintimeout.ChannelValueTimeoutError –
wait_for_new_samples=Trueand values did not arrive withintimeout.
- get_single_channel_value(
- channel_name: str,
Return the most recent sample for
channel_namefrom the in-memory buffer.This will not wait, if data is not available then
Noneis returned.- Parameters:
channel_name
Name of the channel to retrieve.
- Raises:
RuntimeError – No background buffer;
start()was not called.
- publish(
- data: Measurement | Command,
- **kwargs,
Fan
dataout to every configured publisher;kwargspass through.
- start()#
Start the background daemon thread. No-op if already running.
- stop()#
Signal the background daemon to stop and join it. No-op if not running.