I2C¶
The Inter-Integrated Circuit (I2C) Protocol is a protocol intended to allow multiple “peripheral” digital integrated circuits (“chips”) to communicate with the MetaWear board.
The I2C module allows you to directly communicate with a sensor via the I2C bus. If you want to add sensors to the MetaWear board that communicate with I2C, this is possible using the I2C module.
I2C functions are defined in the i2c.h header file.
Data Signal¶
I2C data signals are retrieved by calling mbl_mw_i2c_get_data_signal. You will need to pass two parameters:
Length variable that sets how many bytes the signal is expected to receive
An unique ID identifying the signal
If the id value has already been used, the length parameter will be ignored and the previously created signal will be returned.
var length = 1;
let signal = MetaWear.mbl_mw_i2c_get_data_signal(device.board, length, 0);
Read¶
To read I2C data, use the mbl_mw_datasignal_read_with_parameters function with the parameters set by the MblMwI2cReadParameters struct.
let deviceAddress = 0x1c;
let registerAddress = 0xd;
var length = 1;
let signal = MetaWear.mbl_mw_i2c_get_data_signal(device.board, length, 0);
var parameters = MblMwI2cReadParameters(device_addr: deviceAddress, register_addr: registerAddress);
MetaWear.mbl_mw_datasignal_read_with_parameters(signal, parameters);
MetaWear.mbl_mw_datasignal_subscribe(signal, ref.NULL, MetaWear.FnVoid_VoidP_DataP.toPointer((ctx, pointer) => {
var data = pointer.deref();
var value = data.parseValue();
console.log('bytes: ' + value);
}));
Write¶
Writing data through the I2C bus is handled with the mbl_mw_i2c_write function.
let deviceAddress = 0x77;
let registerAddress = 0xf4;
var writeData = 0x37;
var length = 1;
let array = Array(Data(bytes: writeData, count: Int(length)));
MetaWear.mbl_mw_i2c_write(device.board, deviceAddress, registerAddress, array, length);