Reading from I2C question.

Hi All,

I am new in here and I received yesterday my metawear board. I have been reading the documentation to program in Android but I have a few questions. I want to connect an ADC through I2C communication which sends after the conversion 16bits of information. I have looked at how to handle the I2C communication and this the example I found I would like to modify:
import com.mbientlab.metawear.module.I2C;

I2C i2cModule= mwBoard.getModule(I2C.class);
i2cModule.readData(Device Address, Reg Address, 16).onComplete(new CompletionHandler<byte[]>() {
    @Override
    public void success(byte[] result) {
        Log.i("MainActivity", String.format("WHO_AM_I= %d", result[0]));
    }
});
I have a few questions from it. Instead of displaying a Log window what I would like to do is save the 16 bits of conversion into a variable which I would I will to use it later on my program? Just instead a log put perhaps something like this:

public string var = result[15] + result[14] + ... + result[0];

Is this the right way?

Also I have another question regarding I2C communication. In the ADC it requires before start sending data a bit of Start. What is confusing me is that I can't find any way in the I2C library to send this bit of start. Can anyone clarify me this doubt?

Thanks anyway.

Comments

  • That depends on what you want to do with the array.  It also looks like you are new to the Java language so I would suggest reading up on arrays and classes from the Oracle website to familiarize yourself with Java before proceeding any further.

    You mention bits in your post.  The length parameter and the result array are in terms of bytes so if you really are looking at a 16 bit value, then you should be use length = 2.

    Writing data through the I2C bus is done with the writeData function.
  • Thanks for you answer. I forgot to say it before but what I will have to read from the register is 20 bits of information but I would only need the bits from 16-0 the other 4 aren't relevant. So I will need to read 3 bytes of info. From the other question that I have related. From the reading routine I put before, is it possible to read the variable byte[] result later on the program?
  • Yes, you need to store the value into variable.  You should be at least familiar with Java syntax before you proceed with your app.  Please look at the documentation linked in my previous post because it appears that you are not familiar with using Java.
This discussion has been closed.