

	public class MainActivity extends IOIOActivity {
		  // Debugging
		private static final String TAG = "ALIFE";
		private static final boolean D = true;
		double latitude,longitude;
		// declare for save and email
	    static String saveAdd,mSetAddressText;
	    public static long newTime1,newTime2,newTime3,newTime4,newTime5;
	    public static long timeDiff1,timeDiff2,timeDiff3,timeDiff4,timeDiff5;
	    public static long avgTime,mECGCount;
	   public static long actualTime;
	   // private int i;
		private ToggleButton mEcgOn,mTempOn;
		public static TextView mHRateDisplay,mTempDisplay,mGetAddress;
		private Button mEmail;
		public static String voltsText,voltsText1;
		public static float v,vout;
		private static float mGetMax1=0; 
		private static float mGetMax2=0;
		private static final int HISTORY_SIZE = 20;            // asdfnumber of points to plot in history
		private XYPlot mySimpleXYPlot;
		private SimpleXYSeries sensorHistorySeries = null;
		private Context context=null;
		private ProgressDialog dialog = null;
		private LinkedList<Number> sensorHistory;
		{
        sensorHistory = new LinkedList<Number>();
        sensorHistorySeries = new SimpleXYSeries("HR");
		}
		
		//-------------------------Temperature Graph detials--------//
		private static final int HISTORY_SIZE1 = 20;            // asdfnumber of points to plot in history
		private XYPlot mySimpleXYPlot1;
		private SimpleXYSeries sensorHistorySeries1 = null;
		private LinkedList<Number> sensorHistory1;
		{
        sensorHistory1 = new LinkedList<Number>();
        sensorHistorySeries1 = new SimpleXYSeries("TEMP");
		}
		
		// GPSTracker class
	    GPSTracker gps;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.main);
		
		//Set buttons
		
		mEcgOn = (ToggleButton) findViewById(R.id.toggleButton1);
		mTempOn = (ToggleButton) findViewById(R.id.toggleButton2);
  		mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
  		mHRateDisplay = (TextView) findViewById(R.id.textView3);
  		mTempDisplay = (TextView) findViewById(R.id.textView5);
  		mGetAddress = (TextView) findViewById(R.id.getAdress);
  		
		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		//-----------------------------------------Graph ECG---------------------------------//
		 mySimpleXYPlot.getGraphWidget().getDomainLabelPaint().setColor(Color.TRANSPARENT);
         mySimpleXYPlot.getGraphWidget().getBackgroundPaint().setColor(Color.BLACK);
 		mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.BLACK);
 		mySimpleXYPlot.getGraphWidget().getRangeGridLinePaint().setColor(Color.GRAY);
 		mySimpleXYPlot.getGraphWidget().getDomainGridLinePaint().setColor(Color.TRANSPARENT);
 		  		
  	//---------------------------- Screen Shot -------------------//
 		

        
        mEmail = (Button) findViewById(R.id.button1);
        mEmail.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
            	 Bitmap bitmap = takeScreenshot();
                 saveBitmap(bitmap);
            }
        });
        //----------------------------------End-------------------------//
        

 
	}
 	class Looper extends BaseIOIOLooper{
 		private DigitalOutput led_;
         private AnalogInput inPin;
         private AnalogInput inPin1;
         
 		/* (non-Javadoc)
 		 * @see ioio.lib.util.BaseIOIOLooper#setup()
 		 */
 		@Override
 		protected void setup() throws ConnectionLostException,
 				InterruptedException {
 			 try {
 				led_ = ioio_.openDigitalOutput(IOIO.LED_PIN, true);
//            led_.write(true);
 				  inPin = ioio_.openAnalogInput(40);
 				 inPin1 = ioio_.openAnalogInput(41);
 				  enableUi(true);
 				// TODO Auto-generated method stub
 			} catch (ConnectionLostException e) {
 				enableUi(false);
 				throw e;
 			}
 			super.setup();
 			
 		}

 		/* (non-Javadoc)
 		 * @see ioio.lib.util.BaseIOIOLooper#loop()
 		 */
 		@Override
 		public void loop() throws ConnectionLostException, InterruptedException {
 			// TODO Auto-generated method stub
 			super.loop();

             //led_.write(true);
             v = 0;
             float v1 = 0;
             try {
            	 actualTime = System.currentTimeMillis();
            	 newTime1 = actualTime;
                 led_.write(!mEcgOn.isChecked());
                     v = inPin.getVoltage();
                     v1 = inPin1.getVoltage(); 
     				Thread.sleep(100);                           // controls the delay to get analog data
     				vout = v;		 
             } catch (InterruptedException e) {
                     inPin.close();
                  
                     inPin1.close();
//                   // TODO Auto-generated catch block
                     e.printStackTrace();
             }
             
             final float vout1 = v1;
           
//           
             
             if (sensorHistory.size() > HISTORY_SIZE) {
                   
                    //sensorHistory.removeAll(sensorHistory);
                    sensorHistory.removeFirst();
                    sensorHistory.removeFirst();
             }
             sensorHistory.addLast(vout);

             
             sensorHistorySeries.setModel(sensorHistory, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
//           VoltsField.setText(voltsText);

             
             runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                             
         	        
                            mySimpleXYPlot.redraw();
                              
                     }
             });
             runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                         
                	
                	 if(v>2.5)
      				{
      					newTime2=newTime1;
      					timeDiff1 = actualTime-newTime2;
      				}
                	 
                	 mECGCount = 60000/timeDiff1;
      				voltsText = Long.toString(timeDiff1);
      				voltsText1 = Long.toString(mECGCount);  
                    mHRateDisplay.setText(voltsText);
    	        	mTempDisplay.setText(voltsText1);
                 }
         });
             
             
 		}
 		private void enableUi(final boolean enable) {
 			runOnUiThread(new Runnable() {
 				@Override
 				public void run() {
 					  //mySimpleXYPlot.redraw();	
 					  mEcgOn.setEnabled(enable);
 			
 				}
 			});
 		}
       
 	}
 	

	/* (non-Javadoc)
	 * @see ioio.lib.util.android.IOIOActivity#createIOIOLooper()
	 */
	@Override
	protected IOIOLooper createIOIOLooper() {
		// TODO Auto-generated method stub
		return new Looper();
	}
	
	
	 @Override
	    public void onStart() {
	        super.onStart();
	        if(D) Log.e(TAG, "++ ON START ++");
		     
	           

	            
	    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
}
