readBytesReg method

List<int> readBytesReg(
  1. int address,
  2. int register,
  3. int len
)

Reads len bytes from register of the I2C device with the address.

Some I2C devices can directly be read without explicit register.

Implementation

List<int> readBytesReg(int address, int register, int len) {
  var data = <I2Cmsg>[];
  data.add(I2Cmsg.buffer(address, [], [register]));
  data.add(I2Cmsg(address, [I2CmsgFlags.i2c_m_rd], len));

  var result = transfer(data);
  var msg2 = result._messages[1];
  try {
    var read = msg2.len;

    var ptr = msg2.buf;
    var list = <int>[];
    for (var i = 0; i < read; ++i) {
      list.add(ptr[i]);
    }
    return list;
  } finally {
    result.dispose();
  }
}