Hi,

Here is the trimmed down file that should illustrate the problem. I think 
it mainly comes from my threads (timers) and the IOIO management...

Hope you guys can help me out :)

See attached file!

Le jeudi 12 mars 2015 05:28:13 UTC+1, Ytai a écrit :
>
> There's nothing special about the loop() method, it is mostly a 
> convenience feature. You may call IOIO APIs from any thread including the 
> UI thread (which includes all sorts of GUI listeners).
> The code you posted on stackoverflow doesn't have anything that's related 
> to IOIO on it, so I don't think you'd get much help there. I recommend you 
> to make a copy of your problematic app, trim it down to the bare minimum 
> that illustrates the problem and then post it here.
>
> On Wed, Mar 11, 2015 at 11:48 AM, Vincent Nadon <[email protected] 
> <javascript:>> wrote:
>
>> I am having the following problem : 
>> http://stackoverflow.com/questions/28989176/android-app-stops-i2c-bluetooth-communication-ioio
>>  
>> and I think it's because all my code to call the twi.WriteReadAsync is 
>> outside the loop() function.
>>
>> Le mercredi 11 mars 2015 18:55:55 UTC+1, Vincent Nadon a écrit :
>>
>>> How does the Android IOIO App loop() function work? Is there a way to 
>>> create functions in the Looper class which are called inside this loop() 
>>> according to a listener on a Button or a Spinner? This way it would 
>>> simplify my code since I need to send a lot of twi commands in the loop() 
>>> based on the user input in my Android UI Menu.
>>>
>>  -- 
>> You received this message because you are subscribed to the Google Groups 
>> "ioio-users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/ioio-users.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.
package net.mitchtech.ioio;

import java.nio.ByteBuffer;
import java.util.Calendar;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

//import com.example.graph.LineGraph;


import ioio.lib.api.AnalogInput;
import ioio.lib.api.TwiMaster;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import net.mitchtech.ioio.sensorgraph.R;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.SeekBar.OnSeekBarChangeListener;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.GraphicalView;

import android.view.View.OnClickListener;
import android.widget.Toast;

public class SensorGraphActivity extends IOIOActivity {

	private Spinner spinner1, spinner2;
	private Button button7;

	private Button emergency_stop;

	private static final String TAG = "MEDIA";


	private static GraphicalView gView;
	private LineGraph2 line = new LineGraph2();
	private static Thread thread;

	
	TwiMaster twi;



	private MalibuCountDownTimer stimDownTimer;
	private dataRecCountDownTimer dataRecTimer;
	private dataRecCountDownTimer calibRecTimer;

	private final long startTime = 39600;
	private final long interval = 1800;

	TextView myOutputBox;

	

	// DSP I2C addressses
	// IOIO convention is to keep 7 MSB, AD is not. address/2 = 0x38 - original
	// for DSP1 0x70, DSP2 0x74, DSP3 0x72.
	int addressDSP1 = 0x38;
	int addressDSP2 = 0x3A;
	int addressDSP3 = 0x39; // 116 decimal
	//...
	//Variable definitions...

	

	// ==============================================
	// Called when the activity is first created, initialize GUI

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		
		myOutputBox = (TextView) findViewById(R.id.myOutputBox);

		
		addListenerOnButton();
		addListenerOnSpinnerItemSelection();
		addListenerOnButton2();

		stimDownTimer = new MalibuCountDownTimer(startTime, interval);

		dataRecTimer = new dataRecCountDownTimer(1000, 100);

		calibRecTimer = new dataRecCountDownTimer(300, 100);
		
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

	}

	

	public void addListenerOnSpinnerItemSelection() {
		spinner1 = (Spinner) findViewById(R.id.spinner1);
		spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
	}

	
	
	

	public void addListenerOnButton() {

		spinner1 = (Spinner) findViewById(R.id.spinner1);
		spinner2 = (Spinner) findViewById(R.id.spinner2);
		button7 = (Button) findViewById(R.id.button7);

		button7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Toast.makeText(
						SensorGraphActivity.this,
						"OnClickListener : " + "\nSpinner 1 : "
								+ String.valueOf(spinner1.getSelectedItem())
								+ "\nSpinner 2 : "
								+ String.valueOf(spinner2.getSelectedItem()),
						Toast.LENGTH_SHORT).show();

				

				
				// for (int i = 0; i < arrayOfString.length; i++) {

				if (spinner1.getSelectedItem().toString().equals(" measurement")) 
				{
					iWrite("* measurement running *\n");

					
					try {
						twi.writeReadAsync(addressDSP1, false, stim_off,
								stim_off.length, null, 0);
						
						twi.writeReadAsync(addressDSP2, false, adapt_off,
								adapt_off.length, null, 0);

						twi.writeReadAsync(addressDSP1, false, stim_freq_0,
								stim_freq_0.length, null, 0);

						twi.writeReadAsync(addressDSP1, false, stim_on,
								stim_on.length, null, 0);
						
					} catch (ConnectionLostException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					LinearLayout layout = (LinearLayout) findViewById(R.id.chart);

					layout.removeView(gView);

					stimDownTimer.start();

					lineGraphHandler();

					

					dataRecTimer.start();

				}
					
				
			}

		});

	}

	
	public void lineGraphHandler() {

		//LineGraph Dynamic Graph

		LinearLayout layout = (LinearLayout) findViewById(R.id.chart);


		gView = line.getView(this);


		if (meas_exec == 1)
			line.clearPoints();

		thread = new Thread() {
			public void run() 
			{
				for (int i = 0; i < 22; i++) {
					try {
						Thread.sleep(1800);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					Point p = MockData.getDataFromReceiver1(i); // We got new
																// data!
					Point n = MockData.getDataFromReceiver2(i); // We got new
																// data!
					line.addNewPoints(p, n); // Add it to our graph
					gView.repaint();

				}
				meas_exec = 1;
			}
		};

		thread.start();

		layout.addView(gView);

	}

	//

	public void addListenerOnButton2() {

		emergency_stop = (Button) findViewById(R.id.button8);

		emergency_stop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Toast.makeText(SensorGraphActivity.this,
						"OnClickListener : " + "\nSTOP!!!! : ",
						Toast.LENGTH_SHORT).show();

				try {
					twi.writeReadAsync(addressDSP1, false, stim_off,
							stim_off.length, null, 0);
					twi.writeReadAsync(addressDSP1, false, stim_freq_0,
							stim_freq_0.length, null, 0);

					twi.writeReadAsync(addressDSP2, false, adapt_on,
							adapt_on.length, null, 0);

					// twi.writeReadAsync(addressDSP2, false,
					// request_4_on,request_4_on.length,null,0);
				} catch (ConnectionLostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				stimDownTimer.cancel();
				iWrite("measurement STOPPED!");

			}
		});
	}

	public class MalibuCountDownTimer extends CountDownTimer {

		public MalibuCountDownTimer(long startTime, long interval) {
			super(startTime, interval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			iWrite("freq #: " + millisUntilFinished / 1800);
		}

		@Override
		public void onFinish() {
			try {
				twi.writeReadAsync(addressDSP1, false, stim_off,
						stim_off.length, null, 0);

				twi.writeReadAsync(addressDSP2, false, adapt_on,
						adapt_on.length, null, 0);

				// twi.writeReadAsync(addressDSP2, false,
				// request_4_on,request_4_on.length,null,0);
			} catch (ConnectionLostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			iWrite("DPOAE measurement DONE!");
		}
	}

	public class dataRecCountDownTimer extends CountDownTimer {

		public dataRecCountDownTimer(long startTime, long interval) {
			super(startTime, interval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			double temp = 0;
			temp = 1000 - millisUntilFinished / 100;

			iWrite("Samples Recorded: " + temp);

			if (calib == 0) {

				try {
					twi.writeReadAsync(addressDSP3, false, RB,
							RB.length, response,
							response.length);
					

				} catch (ConnectionLostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				ByteBuffer buffer_DPOAE = ByteBuffer.wrap(response);
			
				int inInt = buffer_DPOAE.getInt();
				

				// now for working with dose and LEQ float would be nice:
				float asFloat = (float) inInt / 8388608;
			


				iWrite("sample Recorded");

			}

		}

		@Override
		public void onFinish() {

			if (calib == 0) {

				try {
					twi.writeReadAsync(addressDSP3, false, RB,
							RB.length, response,
							response.length);					

				} catch (ConnectionLostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				ByteBuffer buffer = ByteBuffer.wrap(response);

				
				int inInt = buffer.getInt();

				
				float asFloat = (float) inInt / 8388608;


			}

			try {
				twi.writeReadAsync(addressDSP1, false, stim_off,
						stim_off.length, null, 0);
				twi.writeReadAsync(addressDSP1, false, stim_freq_0,
						stim_freq_0.length, null, 0);

				twi.writeReadAsync(addressDSP2, false, adapt_on,
						adapt_on.length, null, 0);

			} catch (ConnectionLostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	

	class Looper extends BaseIOIOLooper {

		

		public void setup() throws ConnectionLostException {
			try {
				twi = ioio_.openTwiMaster(0, TwiMaster.Rate.RATE_1MHz, false);
				iWrite("\n I2C CONNECTION ESTABLISHED \n");
			} catch (ConnectionLostException e) {
				throw e;
			}
		}


		public void loop() throws ConnectionLostException {

			// ===============================================================================================================================================
			// END loop()

		} // END loop()
	} // END IOIOThread

	// ===============================================================================================================================================

	@Override
	protected IOIOLooper createIOIOLooper() {
		return new Looper();
	}


	// function to display string on Android screen
	private void iWrite(String myval) {

		final String crossValue = myval;
		runOnUiThread(new Runnable() {
			public void run() {
				myOutputBox.setText(crossValue);
			}
		});
	} // END iWrite

	

	public static byte[] toIntByteArray(int value) {
		// Int only needs 4 bytes, extra 2 are for address
		byte[] bytes = new byte[6];
		// Leave 0,1 for address start at 2 and write for 4
		ByteBuffer.wrap(bytes, 2, 4).putInt(value);
		return bytes;
	}

	public static byte[] toDoubleByteArray(double value) {
		byte[] bytes = new byte[8];
		ByteBuffer.wrap(bytes).putDouble(value);
		return bytes;
	}

	public static double toDouble(byte[] bytes) {
		return ByteBuffer.wrap(bytes).getDouble();
	}


} // LAST BRACKET

Reply via email to