I2C Connection with Python
Hi,
I'm trying to connect I2C-sensors to MetaMotionC Board, using MetaWear Python SDK. I'm a bit confused, how I can do it.
I use mbl_mw_i2c_write() API (https://mbientlab.com/docs/metawear/cpp/latest/i2c_8h.html#a484a0f6338a2d90eb9167283c6859165) in order to write a byte to the specific register of my sensor via the i2c bus. But how can I read a byte array from the specific register?
I've found that mbl_mw_datasignal_read_with_parameters(const MblMwDataSignal * signal, const void * parameters) can help me. With MblMwI2cRead Parameters structure (https://mbientlab.com/docs/metawear/cpp/latest/structMblMwI2cReadParameters.html). But it's a C++ way. How can I configure these parameters in Python?
A whole example of the correct i2c interaction would be the best:)
Thenks!
I'm trying to connect I2C-sensors to MetaMotionC Board, using MetaWear Python SDK. I'm a bit confused, how I can do it.
I use mbl_mw_i2c_write() API (https://mbientlab.com/docs/metawear/cpp/latest/i2c_8h.html#a484a0f6338a2d90eb9167283c6859165) in order to write a byte to the specific register of my sensor via the i2c bus. But how can I read a byte array from the specific register?
I've found that mbl_mw_datasignal_read_with_parameters(const MblMwDataSignal * signal, const void * parameters) can help me. With MblMwI2cRead Parameters structure (https://mbientlab.com/docs/metawear/cpp/latest/structMblMwI2cReadParameters.html). But it's a C++ way. How can I configure these parameters in Python?
A whole example of the correct i2c interaction would be the best:)
Thenks!
This discussion has been closed.
Comments
I use the "standard" data handler from the class State (https://github.com/mbientlab/MetaWear-SDK-Python/blob/master/examples/multi_device.py#L19). How can I understand which libmetawear.mbl_mw_datasignal_read_with_parameters called it?
In the handler I see only device and data payload, that's a byte list in my way.
Based on the multi_device.py example, I do so:
class State:
def __init__(self, device):
self.device = device
self.samples = 0
self.callback = FnVoid_DataP(self.data_handler)
def data_handler(self, data):
print("%s -> %s" % (self.device.address, parse_value(data)))
self.samples+= 1
states = []
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
states.append(State(d))
for s in states:
print("configuring device")
libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 6000)
libmetawear.mbl_mw_i2c_write(s.device.board, MPU6050_ADDR, PWR_MGMT_1, ctypes.c_ubyte(0), 1)
libmetawear.mbl_mw_i2c_write(s.device.board, MPU6050_ADDR_ADO, PWR_MGMT_1, ctypes.c_ubyte(0), 1)
signal_mpu6050 = libmetawear.mbl_mw_i2c_get_data_signal(s.device.board, 2, 0xa)
signal_mpu6050_ado = libmetawear.mbl_mw_i2c_get_data_signal(s.device.board, 2, 0xb)
libmetawear.mbl_mw_datasignal_subscribe(signal_mpu6050, s.callback)
libmetawear.mbl_mw_datasignal_subscribe(signal_mpu6050_ado, s.callback)
# get temperature
param_mpu6050_temp = I2cReadParameters(device_addr = MPU6050_ADDR, register_addr = TEMP_H)
param_mpu6050_ado_temp = I2cReadParameters(device_addr = MPU6050_ADDR_ADO, register_addr = TEMP_H)
# get acceleration
param_mpu6050_accl_x = I2cReadParameters(device_addr = MPU6050_ADDR, register_addr = ACCL_X_H)
param_mpu6050_ado_accl_x = I2cReadParameters(device_addr = MPU6050_ADDR_ADO, register_addr = ACCL_X_H)
libmetawear.mbl_mw_datasignal_read_with_parameters(signal_mpu6050, byref(param_mpu6050_temp))
libmetawear.mbl_mw_datasignal_read_with_parameters(signal_mpu6050_ado, byref(param_mpu6050_ado_temp))
sleep(0.5)
libmetawear.mbl_mw_datasignal_read_with_parameters(signal_mpu6050, byref(param_mpu6050_accl_x))
libmetawear.mbl_mw_datasignal_read_with_parameters(signal_mpu6050_ado, byref(param_mpu6050_ado_accl_x))
mbl_mw_datasignal_subscribe
with different callback functions