Class RegisterTarget

extends Object
An I2C target backed by a native register array.
Controller writes start with a one- or two-byte, big-endian register address. Any following bytes update consecutive registers. A write containing only the address selects the first register for a subsequent controller read. A write containing data leaves the selected register immediately after the last byte written. Controller reads are answered directly from native memory without waiting for a Toit task, and advance the selected register by the number of bytes actually read. Controller accesses wrap at the end of the register array.
The native array is initialized to zero. Toit code can access it with indexing, read, and write while the target is active. Individual byte accesses are atomic, but multi-byte accesses are not snapshots of a controller transaction. Coordinate the two sides if multi-byte values must be observed consistently.
Register targets require an I2C peripheral with address-match clock stretching and are not supported on the original ESP32.

Class summary


constructor --sda/int --scl/int --address/int --address-bit-size/int= --register-count/int= --register-address-byte-size/int= --receive-buffer-size/int= --pull-up/bool= --broadcast/bool=
Constructs a register-backed I2C target on the sda and scl GPIOs.

Constructors

constructor --sda/int --scl/int --address/int --address-bit-size/int=7 --register-count/int=256 --register-address-byte-size/int=1 --receive-buffer-size/int=DEFAULT-TARGET-BUFFER-SIZE --pull-up/bool=false --broadcast/bool=false
Constructs a register-backed I2C target on the sda and scl GPIOs.
register-count is the number of native register bytes. The register-address-byte-size is the number of address bytes in the controller protocol and must be 1 or 2. A one-byte address can select at most 256 registers; a two-byte address can select at most 65,536.
receive-buffer-size is the largest complete controller write transaction that can update the registers. Writes exceeding it are discarded.
The I2C address, pull-up, and broadcast options have the same meaning as for Target.

Methods

close -> none
Closes the target and releases its pins and native register array.

Number of controller write transactions discarded due to overflow.

Returns the byte stored at index.

operator []= index/int value/int -> int
Stores value at index and returns it.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

read index/int count/int -> ByteArray
Returns count consecutive register bytes starting at index.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

write index/int bytes/ByteArray -> none
Writes bytes to consecutive registers starting at index.

Fields