package com.example.steeringhelper;

import java.io.IOException;

import android.util.Log;

public class BTCommThread implements Runnable{
	public static final long SLEEP_TIME = 100;
	
	private static final String TAG = "BTCommThread";
	private BT_Comm comm = null;
	private boolean mDoProcess = false;
	
	
	public BTCommThread(BT_Comm c)
	{
		comm = c;
	}
	
	@Override
	public void run() {
		mDoProcess = true;
		
		// TODO Auto-generated method stub
		if(comm == null){
			return;
		}
		
		while(comm.isNXTConnected() && mDoProcess){
			try {
				String msg = "";
				int offset = InfoCollector.GetTraceOffset();
				if(offset < 0){
					msg += "L";
				}else if(offset > 0){
					msg += "R";
				}else{
					msg += "M";
				}
				int direction = InfoCollector.GetDirection();
				if(direction < 0){
					msg += "N";
				}else if(direction > 0){
					msg += "P";
				}else{
					msg += "C";
				}
				Log.d("BBBB", "msg --> " + msg);
				comm.writeMessage(msg);
				Thread.sleep(SLEEP_TIME);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				Log.d(TAG, e.getLocalizedMessage());
			}
		}
		
		if(mDoProcess){
			mDoProcess = false;
		}
	}
	
	public void stopProcessing() {
		comm = null;
        mDoProcess = false;
    }

}
