Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I2C Multiple Readings #49

Closed
ghost opened this issue Feb 25, 2013 · 1 comment
Closed

I2C Multiple Readings #49

ghost opened this issue Feb 25, 2013 · 1 comment

Comments

@ghost
Copy link

ghost commented Feb 25, 2013

Hi all

I've been trying to use the IOIO board in a project of mine which utilizes sensors that communicate by I2C. I'm having the oddest bug: I can connect to the board fine, blink the status light fine, and take one reading from the sensors fine. When I attempt to take a second reading, the app force-closes. The addresses read for either the first or second attempt don't really matter (the first attempt will work correctly with any valid register, and the second attempt will force close with any register). I'm guessing that the port isn't getting closed correctly, but I can't be sure.

Anyways, here's the thread of interest. I can post the rest of the code, but there's not much of interest in it. The only things to note are that _ioio is a valid instance of a connection to the IOIO board. I have verified (through publishProgress()) that the app runs until the line that has the comment that says "CRASH."

Anyways, here's the thread:

protected class GetSensorCalibration extends AsyncTask<IOIO, Integer, String>
{

        /*Modifies ConstantArray[][], uses XCLR_Array[]*/
        protected String doInBackground(IOIO...params)
        {
            //TextView Console_IO = (TextView)findViewById(R.id.Con_IO);
            IOIO _ioio = params[0]; //Unnecessary to do this..
            //Console_IO.append("\nSTARTING CALIBRATION\n*****************************************\n");

            try
            {
                //Console_IO.append("Connected!\n");
                TwiMaster i2c = _ioio.openTwiMaster(0, TwiMaster.Rate.RATE_1MHz, false);
                publishProgress(7);
                DigitalOutput LED = _ioio.openDigitalOutput(0,false);
                //DigitalOutput X_CLR = _ioio.openDigitalOutput(6);
                //DigitalInput EOC = _ioio.openDigitalInput(7);

                byte Start[] = {(byte)0xAA}; //pointer to first register to read for calibration
                publishProgress((int)Start[0]);
                int Addr[] = {(byte)0x77};
                X_CLR.write(true);
                for (int j = 0; j < 11; j++)
                {
                    publishProgress(1);
                    byte MSBResponse[] = new byte[1];
                    byte LSBResponse[] = new byte[1];

                    i2c.writeRead(Addr[0], false, Start, 1, MSBResponse, 1); //first argument is decimal equivilant of 0xEE...Eclipse wouldn't compile with 0xEE, even with casts!
                    publishProgress((int)MSBResponse[0]);
                    Start[0] += 1; //increment to read next bit
                    publishProgress((int)Start[0]);
                    Thread.sleep(5);

                    publishProgress((int)(byte)0xAB);

                    i2c.writeRead(Addr[0], false, Start, 1, LSBResponse, 1); //CRASH
                    publishProgress(4);
                    Start[0] += 1; //point to next address

                    /* Read the data into the array */
                    CalibrationData[j] = ((int)MSBResponse[0] << 8) + (int)LSBResponse[0];
                    publishProgress((int)CalibrationData[j]);
                    /* Write the data to the console */
                    //String s = String.format("\tJust read: %ld\n", CalibrationData[j]);

                    //Console_IO.append(s);

                    MSBResponse[0] = (byte)0;
                    LSBResponse[0] = (byte)0;
                    Thread.sleep(5);
                    publishProgress(6);
                }
                //Console_IO.append("*******************************************\n");
                //X_CLR.write(false);
                i2c.close();
                LED.close();
                X_CLR.close();
                publishProgress(6);
                //Console_IO.append("Disconnected\n");
                return "Done!";
            }
            catch (Exception e)
            {
                //TextView Con_IO = (TextView)findViewById(R.id.Con_IO);
                String s = e.toString();
                //Con_IO.append(s);
                //Con_IO.append("BLURGH\n");
                return null;
            }

        }

        protected void onPostExecute(String Result)
        {
            TextView Con_IO = (TextView)findViewById(R.id.Con_IO);
            Con_IO.append("yay!\n");
            /*Catch bad results*/
            if (!(Result.equals("Done!")))
            {
                Con_IO.append("Calibration Sequence Returned Unsuccessfully.\n");
                return;
            }

            Con_IO.append("\n\nBEGIN_PLAYBACK\n");

            for (int i = 0; i < CalibrationData.length; i++)
            {
                String s = String.format("\tThe next Calibration Constant is %ld\n", CalibrationData[i]);
                Con_IO.append(s);
            }

            return;
        }

        protected void onPreExecute ()
        {
            TextView Con_IO = (TextView)findViewById(R.id.Con_IO);
            Con_IO.append("Starting Thread!\n");
        }

        protected void onProgressUpdate(Integer...values)
        {
            String S = values[0].toString();
            TextView Con_IO = (TextView)findViewById(R.id.Con_IO);
            Con_IO.append(S);
            Con_IO.append(" ");
        }
    }

And, if it's of interest, here's the datasheet for the sensor I'm using. Seems like standard I2C: https://www.sparkfun.com/products/11282

Thanks for any help you can give me!

@ytai
Copy link
Owner

ytai commented Feb 25, 2013

A few comments:

  1. Since this is most likely a usage error, I'm closing the bug, and the best way to pursue this issue and any further ones is on the ioio-users mailing list.
  2. When an Android app crashes it dumps the exception to logcat, so you can figure out the reason.
  3. In this particular case, my guess would be that your publishProgress is accessing the UI, which is illegal in Android anywhere but the UI thread. See IOIOSimpleApp as an example of how to overcome this using runOnUiThread().
  4. I have no idea why the I2C address is an array in your code. It would make much more sense to just have it as a scalar constant.
  5. You should be able to read more than one byte per-transaction on this device.

@ytai ytai closed this as completed Feb 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant