Class Bus

extends Object
Bus for communicating using I2C.
Controller operations from multiple tasks are serialized on this bus.

Class summary


constructor --sda/any --scl/any --frequency/int= --pull-up/bool=
Constructs an I2C bus on the sda (data) and the scl (clock) pins.
constructor --sda/any --scl/any --frequency/int= --sda-pullup/bool
Deprecated. Use constructor --sda --scl --pull-up instead.
constructor --sda/any --scl/any --frequency/int= --sda-pullup/bool= --scl-pullup/bool
Deprecated. Use constructor --sda --scl --pull-up instead.

Constructors

constructor --sda/any --scl/any --frequency/int=DEFAULT-FREQUENCY --pull-up/bool=false
Constructs an I2C bus on the sda (data) and the scl (clock) pins.
The sda and scl are GPIO numbers. The bus reserves the pins and releases them again when the bus is closed.
The frequency specifies the default frequency for devices that are created with device. Individual devices can have their frequency overwritten.
If pull-up is true, then the SDA and SCL pins are pulled up.
The EPS32s pullups are *not* strong enough for high-speed I2C communication. Use external pull-up resistors if you need to communicate at high speeds. Many i2c modules have integrated built-in pull-up resistors, so this is typically not necessary.
Passing a gpio.Pin as sda or scl is deprecated; provide the integer GPIO number instead. The gpio.Pin form will be removed in a future release.

constructor --sda/any --scl/any --frequency/int=DEFAULT_FREQUENCY --sda-pullup/bool
Deprecated. Use constructor --sda --scl --pull-up instead.
The sda-pullup is not fully supported anymore. If either is true, then both are pulled up.

constructor --sda/any --scl/any --frequency/int=DEFAULT_FREQUENCY --sda-pullup/bool=false --scl-pullup/bool
Deprecated. Use constructor --sda --scl --pull-up instead.
The sda-pullup and scl-pullup flags are not fully supported anymore. If either is true, then both are pulled up.

Methods

close -> none
Closes this I2C bus.
Releases the resources associated with this bus.

device i2c-address/int --frequency/int --address-bit-size/int=7 --timeout-us/int=null --disable-ack-check/bool=false -> Device
Creates the device connected on the i2c-address.
address-bit-size selects a 7-bit or 10-bit address.
timeout-us is the maximum SCL clock-stretching interval tolerated by the controller. If omitted or null, the platform default is used: 100 ms on ESP32, and no hardware timeout on EC618. EC618 does not support a custom clock-stretching timeout and rejects a non-null timeout-us with UNIMPLEMENTED. Use with-timeout to bound the whole operation on either platform.
If disable-ack-check is true, missing acknowledgements do not fail write transactions.
It is an error to connect a device on an address already in use. A device can be released with Device.close.

device i2c-address/int --address-bit-size/int=7 --timeout-us/int=null --disable-ack-check/bool=false -> Device
Variant of device i2c-address --frequency --address-bit-size that uses the default frequency given to the bus at construction.

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

scan --timeout-ms/int=100 -> Set
Scans all valid addresses.
Returns the set of addresses that responded.
Some addresses are reserved and are not scanned. See https://www.i2c-bus.org/addressing/.
Probes use the standard-mode 100kHz clock rate.
Waits at most timeout-ms for a response on each address. If the bus is very slow, increase the timeout.

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.

test address/any --timeout-ms/int=100 -> bool
Tests if the address responds.
The probe uses the standard-mode 100kHz clock rate.
Waits at most timeout-ms for a response. If the bus is very slow, increase the timeout.