Skip to content

Commit

Permalink
fix coding convention
Browse files Browse the repository at this point in the history
  • Loading branch information
KyuJeong committed Nov 29, 2017
1 parent 00a57c7 commit 0780471
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 108 deletions.
162 changes: 55 additions & 107 deletions app/src/main/java/com/example/kimyoungbin/stepcounter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener{
public class MainActivity extends AppCompatActivity implements SensorEventListener {

private long lastTime;
private float speed;
private float lastX;
private float lastY;
private float lastZ;
private float x,y,z;
private long lastTime;
private float speed;
private float lastX;
private float lastY;
private float lastZ;
private float x, y, z;

private static final int SHAKE_THRESHOLD = 300;
private static final int DATA_X = SensorManager.DATA_X;
private static final int DATA_Y = SensorManager.DATA_Y;
private static final int DATA_Z = SensorManager.DATA_Z;
private static final int SHAKE_THRESHOLD = 300;
private static final int DATA_X = SensorManager.DATA_X;
private static final int DATA_Y = SensorManager.DATA_Y;
private static final int DATA_Z = SensorManager.DATA_Z;

//Using the Accelometer & Gyroscoper
private SensorManager mSensorManager = null;

//Using the Accelometer
private SensorEventListener mAccLis;
private Sensor mAccelometerSensor = null;

//Using the Gyroscoper
Expand All @@ -40,26 +39,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen

TextView textView;

/** G sensor valuable **/
/************************************************************/
//Roll and Pitch
private double pitch;
private double roll;
private double yaw;

//timestamp and dt
private double timestamp;
private double dt;

// for radian -> dgree
private double RAD2DGR = 180 / Math.PI;
private static final float NS2S = 1.0f / 1000000000.0f;

static int isTouch = 0;
static int gTouch = 0;

/************************************************************/


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -71,7 +50,6 @@ protected void onCreate(Bundle savedInstanceState) {

//Using the Accelometer
mAccelometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccLis = new AccelometerListener();

//Using the Gyroscoper
mGgyroSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
Expand All @@ -80,82 +58,59 @@ protected void onCreate(Bundle savedInstanceState) {
mSensorManager.registerListener(mGyroLis, mGgyroSensor, SensorManager.SENSOR_DELAY_NORMAL);

textView = (TextView) findViewById(R.id.tv_step);
}

@Override
public void onStart() {
super.onStart();
if (mAccelometerSensor != null) {
mSensorManager.registerListener(this, mAccelometerSensor,
SensorManager.SENSOR_DELAY_GAME);
}
}

@Override
@Override
public void onPause() {
super.onPause();
Log.e("LOG", "onPause()");
mSensorManager.unregisterListener(mAccLis);
mSensorManager.unregisterListener(mGyroLis);
mSensorManager.unregisterListener(this);
}

@Override
public void onDestroy() {
super.onDestroy();
Log.e("LOG", "onDestroy()");
mSensorManager.unregisterListener(mAccLis);
mSensorManager.unregisterListener(mGyroLis);
mSensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
long currentTime = System.currentTimeMillis();
long gabOfTime = (currentTime - lastTime);
if(gabOfTime > 100){
lastTime = currentTime;
x = event.values[SensorManager.DATA_X];
y = event.values[SensorManager.DATA_Y];
z = event.values[SensorManager.DATA_Z];

speed = Math.abs(x+y+z-lastX-lastY-lastZ)/gabOfTime * 10000;

if(speed > SHAKE_THRESHOLD){
//이벤트발생!!
isStep = false;
Log.e("LOG", "너무 많이 흔듬");
}else{
isStep = true;
}
lastX = event.values[DATA_X];
lastY = event.values[DATA_Y];
lastZ = event.values[DATA_Z];
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
long currentTime = System.currentTimeMillis();
long gabOfTime = (currentTime - lastTime);
if (gabOfTime > 100) {
lastTime = currentTime;
x = event.values[SensorManager.DATA_X];
y = event.values[SensorManager.DATA_Y];
z = event.values[SensorManager.DATA_Z];

speed = Math.abs(x + y + z - lastX - lastY - lastZ) / gabOfTime * 10000;

if (speed > SHAKE_THRESHOLD) {
//이벤트발생!!
isStep = false;
} else {
isStep = true;
}
lastX = event.values[DATA_X];
lastY = event.values[DATA_Y];
lastZ = event.values[DATA_Z];
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}

private class AccelometerListener implements SensorEventListener {

@Override
public void onSensorChanged(SensorEvent event) {

double accX = event.values[0];
double accY = event.values[1];
double accZ = event.values[2];

double angleXZ = Math.atan2(accX, accZ) * 180 / Math.PI;
double angleYZ = Math.atan2(accY, accZ) * 180 / Math.PI;

Log.e("LOG", "ACCELOMETER [X]:" + String.format("%.4f", event.values[0])
+ " [Y]:" + String.format("%.4f", event.values[1])
+ " [Z]:" + String.format("%.4f", event.values[2])
+ " [angleXZ]: " + String.format("%.4f", angleXZ)
+ " [angleYZ]: " + String.format("%.4f", angleYZ));

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}

Expand All @@ -164,39 +119,32 @@ private class GyroscopeListener implements SensorEventListener {
@Override
public void onSensorChanged(SensorEvent event) {

/* 각 축의 각속도 성분을 받는다. */
/* receives the angular velocity of each axis. */
double gyroX = event.values[0];
double gyroY = event.values[1];
double gyroZ = event.values[2];

final double NUM = 0.2;

/* 각속도를 적분하여 회전각을 추출하기 위해 적분 간격(dt)을 구한다.
* dt : 센서가 현재 상태를 감지하는 시간 간격
* NS2S : nano second -> second */
/* 맨 센서 인식을 활성화 하여 처음 timestamp가 0일때는 dt값이 올바르지 않으므로 넘어간다. */

/* 각속도 성분을 적분 -> 회전각(pitch, roll)으로 변환.
* 여기까지의 pitch, roll의 단위는 '라디안'이다.
* SO 아래 로그 출력부분에서 멤버변수 'RAD2DGR'를 곱해주어 degree로 변환해줌. */
/* standard value to compare to determining step */
final double CMP = 0.2;

/* Compares the X and Z axis values ​​to determine the step. */
if (!isStep) {
if (gyroX > NUM && gyroZ < -NUM ) {
if (gyroX > CMP && gyroZ < -CMP) {
mStep++;
isStep = true;
}
}

if (isStep) {
if (gyroX < -NUM && gyroZ > NUM ) {
if (gyroX < -CMP && gyroZ > CMP) {
mStep++;
isStep = false;
}
}

Log.e("LOG", "GYROSCOPE [X]:" + String.format("%.4f", event.values[0])
+ " [Y]:" + String.format("%.4f", event.values[1])
+ " [Z]:" + String.format("%.4f", event.values[2]));
+ " [Y]:" + String.format("%.4f", event.values[1])
+ " [Z]:" + String.format("%.4f", event.values[2]));

textView.setText(String.format("%d", (int) mStep));

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:2.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 0780471

Please sign in to comment.