Class Target

extends Object
An addressable I2C target.
A target receives complete controller write transactions with read and queues bytes for controller read transactions with write.

Class summary


constructor --sda/int --scl/int --address/int --address-bit-size/int= --send-buffer-size/int= --receive-buffer-size/int= --default-response/ByteArray= --pull-up/bool= --broadcast/bool=
Constructs an I2C target on the sda and scl GPIOs.

Constructors

constructor --sda/int --scl/int --address/int --address-bit-size/int=7 --send-buffer-size/int=DEFAULT-TARGET-BUFFER-SIZE --receive-buffer-size/int=DEFAULT-TARGET-BUFFER-SIZE --default-response/ByteArray=null --pull-up/bool=false --broadcast/bool=false
Constructs an I2C target on the sda and scl GPIOs.
The address is either a 7-bit or 10-bit address, selected with address-bit-size. The pins are reserved until close is called.
send-buffer-size is the number of native bytes available for responses to controller reads. receive-buffer-size is both the largest controller write transaction that can be received and the approximate amount of native space used to queue unread transactions.
default-response is served when no response has been queued and no serve-read-requests handler is active. It must contain between 1 and MAX-DEFAULT-RESPONSE-SIZE bytes. If omitted, the default response is 32 bytes of 0xff. On targets with response-time clock stretching, the response repeats if a controller reads beyond it. The original ESP32 can only guarantee the first response (at most 32 bytes) of a transaction.
If pull-up is true, the weak internal pull-ups are enabled. External pull-ups are recommended for normal and fast bus speeds.
broadcast makes the target acknowledge the general-call address. See the ESP32 target support section above for availability. It cannot be combined with a 10-bit address.

Methods

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

Number of controller write transactions dropped due to buffer overflow.

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

Waits for and returns the next complete controller write transaction.

serve-read-requests --response-timeout-us/int=DEFAULT-TARGET-RESPONSE-TIMEOUT-US [block] -> none
Serves controller read requests with responses returned by block.
The block must return a ByteArray. It is called again when the controller consumes the response and continues reading. An empty response calls the block again while the controller remains waiting. If the controller ends the transaction before consuming the response, the unused tail is discarded and the block is called for the next read transaction.
response-timeout-us bounds each call to block. If the block does not produce a response in time, this method throws DEADLINE_EXCEEDED and restores the default response, releasing a controller that is waiting for data. The block must not suppress task deadlines for longer than this interval.
While this method is active, the configured default response is suppressed. If the block throws or performs a non-local return, the default response is restored. A controller already waiting for a response is released with the default response.
This method requires response-time clock stretching and is not supported on the original ESP32. Use write to queue responses there.

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.

Returns the next complete controller write transaction, or null if none is queued.
Transaction boundaries are preserved. An overflow is reported by throwing OVERFLOW; dropped-receive-count provides the cumulative count.

Queues as many bytes as currently fit for a controller read.
Returns the number of bytes queued. This method does not wait. Throws INVALID_STATE while serve-read-requests is active.

write bytes/ByteArray -> none
Queues all bytes for controller reads.
If the native send buffer fills, this method waits until a controller asks for more data.