package com.test.friction;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class Sen implements SensorEventListener{
	public float dx = 0;
	public float dy = 0;
	public float dz = 0;
	public float[] values = new float[3];
	private SensorEvent lastevent;
	public final int type;
	private Friction friction;
	
	public static final int ACC = 0;
	public static final int GYRO = 1;
	
	private double tolerance;
	
	public Sen(int type, Friction f){
		this.friction = f;
		this.type = type;
		this.tolerance = -1;
	}
	public void onAccuracyChanged(Sensor sensor, int a){
	}
	public void onSensorChanged(SensorEvent event){
		this.lastevent = event;
		
		if (type==GYRO){
			this.values[1] = event.values[1]-dx;
			this.values[2] = event.values[2]-dy;
			this.values[0] = event.values[0]-dz;
		}
		if (type==ACC){
			this.values[0] = event.values[0]-dx;
			this.values[1] = event.values[1]-dy;
			this.values[2] = event.values[2]-dz;

			double total = Math.sqrt((this.values[0]) * (this.values[0]) + (this.values[1]) * (this.values[1]) + (this.values[2]) * (this.values[2])) - SensorManager.GRAVITY_EARTH;
			
			
			if (total <= tolerance)
				friction.slip();
		}
	}
	public void cal(){
		if (type==GYRO){
			this.dx = lastevent.values[1];
			this.dy = lastevent.values[2];
			this.dz = lastevent.values[0];
		}
		
		if (type==ACC){
			this.dx = lastevent.values[0];
			this.dy = lastevent.values[1];
			this.dz = lastevent.values[2];
		}
	}
	public void reset(){
		this.dx = 0;
		this.dy = 0;
		this.dz = 0;
	}
	public float[] get(){
		return values;
	}
	public void setT(double t){
		this.tolerance = t;
	}
}