Serial Passthrough¶
The ISerialPassthrough interface encapsulates the I2C and SPI buses which can be used to communicate with more complex devices.
using MbientLab.MetaWear.Peripheral;
ISerialPassthrough serialPassthrough = metawear.GetModule<ISerialPassthrough>();
I2C¶
Reading and writing data over the I2C bus is done with the ReadI2CAsync and WriteI2C methods respectively. Note that the MeteaWear performs a register read not a raw read.
// On MetaWear R boards, this reads the WHO_AM_I register from the accelerometer
byte[] result = await serialPassthrough.ReadI2CAsync(0x1c, 0x0d, 1);
Console.WriteLine("WHO_AM_I = " + result[0]);
// Reads the ID register from the BMP280 barometer (rpro, cpro, motion)
result = await serialPassthrough.ReadI2CAsync(0x77, 0xd0, 1);
Console.WriteLine("bmp280 id = " + result[0]);
SPI¶
Reading and writing data over the SPI bus is done with the ReadSpiAsync and WriteSpi methods respectively.
// read registers from the BMI160 IMU (rg, rpro, c, cpro, motion, tracker)
var result = await serialPassthrough.ReadSPIAsync(5, 10, 0, 11, 7, 3, SpiFrequency._8_MHz,
lsbFirst: false, data: new byte[] { 0xda });
Console.WriteLine("bmi160 register read = " + Util.arrayToHexString(result));
Data Route¶
Typically I2C and SPI data is passed directly to the Android device. They can be used with a data route by creating a data producer using
I2C and
SPI respectively. When calling
these functions, a unique numerical value needs to be given to identify the object along with the number of bytes the object will read. If the id
parameter corresponds to an existing object, the length
parameter is ignored and the existing object is returned instead.