Class Device

extends Object implements Device
Device connected using the I2C bus.
A device is connected on a specific I2C address that can be found in the data sheet of the device.
Errors
Controller operations throw I2C_NACK when acknowledgement checking fails and I2C_TIMEOUT when the configured clock-stretching timeout is exceeded.

Class summary


registers --byte-size/int= --byte-order/ByteOrder= -> Registers
write bytes/ByteArray -> none
Writes the bytes to the device.
write bytes/ByteArray [failure] -> none
Variant of write bytes.
write-reg register/int bytes/ByteArray -> none
Writes the bytes to the device at the given register.
write-reg register/int bytes/ByteArray [failure] -> none
write-address address/ByteArray bytes/ByteArray -> none
Writes the bytes to the device at the given address.
write-address address/ByteArray bytes/ByteArray [failure] -> none
read size/int -> ByteArray
Reads size bytes from the device.
read-into buffer/ByteArray size/int= -> none
Variant of read size.
read size/int [failure] -> ByteArray
Variant of read size.
read-reg register/int size/int -> ByteArray
Reads size bytes from the given register.
read-reg register/int size/int [failure] -> ByteArray
read-address address/ByteArray size/int -> ByteArray
Reads size bytes from the given address.
read-address address/ByteArray size/int [failure] -> ByteArray
write-read tx-buffer/Data size/int -> ByteArray
Writes the tx-buffer to the device and reads size bytes.
write-read-into --tx-buffer/Data --rx-buffer/ByteArray size/int= -> none
close -> none
Closes this device and releases the I2C address.
operator == other/any -> bool
Whether this object is equal to the other.
stringify -> string
Stringifies this object.

Methods

close -> none
Closes this device and releases the I2C address.

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

Reads size bytes from the device.
The size must be positive.
Advanced
The read operation is done as follows:
  • send a 'start',
  • send the device's I2C address with the READ/WRITE bit set to READ. This is accomplished by
  • shifting the I2C address by one and setting the least-significant bit. The device must ack.
  • Read size bytes, acking each byte except for the last, where receipt is confirmed with a 'nack'.
  • Finally, send a 'stop'.

read size/int [failure] -> ByteArray
Variant of read size.
Calls the failure block if the read fails.
Deprecated. Use exception handling instead.

Reads size bytes from the given address.

read-address address/ByteArray size/int [failure] -> ByteArray
Calls the failure block if the operation fails.
Deprecated. Use exception handling instead.

read-into buffer/ByteArray size/int=buffer.size -> none
Variant of read size.
Reads size bytes into the given buffer.

read-reg register/int size/int -> ByteArray
Reads size bytes from the given register.
The register value must satisfy 0 <= register < 256.
Equivalent to calling read-address with a byte array containing the register value.

read-reg register/int size/int [failure] -> ByteArray
Calls the failure block if the read fails.
Deprecated. Use exception handling instead.

registers --byte-size/int=1 --byte-order/ByteOrder=io.BIG-ENDIAN -> Registers
The byte-size parameter specifies the size of the registers in bytes. For most I2C devices, this is 1, but 2 is common too. If the register size is greater than 1, then the byte-order parameter specifies the byte order of the register address.
Always returns the same object, unless the size of the registers changes or the register byte-order is not the same. The first allocation of the register cached; all subsequent *different* ones will create new objects.

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 bytes/ByteArray -> none
Writes the bytes to the device.
The bytes data must not be empty.
Advanced
The write operation is executed by sending:
  • a 'start',
  • the device's I2C address with the READ/WRITE bit set to WRITE. This is accomplished by
  • shifting the I2C address by one and clearing the least-significant bit. The device must ack
  • the bytes.
  • the device must ack.
  • a 'stop'.

write bytes/ByteArray [failure] -> none
Variant of write bytes.
Calls the failure block if the write fails.
Deprecated. Use exception handling instead.

write-address address/ByteArray bytes/ByteArray -> none
Writes the bytes to the device at the given address.
This is a convenience method and equivalent to prepending the address bytes to bytes and then calling write bytes.

write-address address/ByteArray bytes/ByteArray [failure] -> none
Calls the failure block if the write fails.
Deprecated. Use exception handling instead.

write-read tx-buffer/Data size/int -> ByteArray
Writes the tx-buffer to the device and reads size bytes.
The tx-buffer must be nonempty and size must be positive.
Advanced
This operation is done as follows:
  • send a 'start',
  • send the device's I2C address with the READ/WRITE bit set to WRITE. This is accomplished by
  • shifting the I2C address by one and clearing the least-significant bit. The device must ack.
  • send the tx-buffer, needing an 'ack' for each byte.
  • send another 'start'
  • send the device's I2C address with the READ/WRITE bit set to READ. This is accomplished by
  • shifting the I2C address by one and setting the least-significant bit. The device must ack.
  • read size bytes, sending an 'ack' for each byte except for the last, where
  • receipt is confirmed with a 'nack'.
  • finally send a 'stop'.

write-read-into --tx-buffer/Data --rx-buffer/ByteArray size/int=rx-buffer.size -> none
Reads size bytes into the given rx-buffer.

write-reg register/int bytes/ByteArray -> none
Writes the bytes to the device at the given register.
The register value must satisfy 0 <= register < 256.
This is a convenience method and equivalent to prepending the register byte to bytes and then calling write bytes.

write-reg register/int bytes/ByteArray [failure] -> none
Calls the failure block if the write fails.
Deprecated. Use exception handling instead.

Fields

I2C address of the device.

Number of address bits, either 7 or 10.