daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.jozef.gyrouhol;
  2.  
  3. import android.hardware.Sensor;
  4. import android.hardware.SensorEvent;
  5. import android.hardware.SensorEventListener;
  6. import android.hardware.SensorManager;
  7. import android.os.Bundle;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. import android.app.Activity;
  12. import java.lang.Math;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Locale;
  16. import java.util.concurrent.TimeUnit;
  17.  
  18. public class Gyro extends AppCompatActivity implements SensorEventListener {
  19.  
  20.     private SensorManager mSensorManager;
  21.     private Sensor mRotationSensor, mStepSensor;
  22.  
  23.     private static final int SENSOR_DELAY = 1000;
  24.     private static final int FROM_RADS_TO_DEGS = -57;
  25.     private double norming;
  26.  
  27.     private ObjectHandler mData;
  28.  
  29.     private int count = 0;
  30.     private int pmin = 0, pmax=0;
  31.     private long actualTime = 0;
  32.  
  33.     private float mStartingAngle;
  34.     private HouseBackground myView;
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.  
  38.         super.onCreate(savedInstanceState);
  39.         myView = new HouseBackground(this);
  40.         setContentView(myView);
  41.  
  42.         try {
  43.  
  44.             mSensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
  45.             mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
  46.             mStepSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  47.             mSensorManager.registerListener(this, mRotationSensor, SENSOR_DELAY);
  48.             mSensorManager.registerListener(this,mStepSensor,SENSOR_DELAY);
  49.         }
  50.         catch (Exception e) {
  51.             Toast.makeText(this, "Hardware compatibility issue", Toast.LENGTH_LONG).show();
  52.         }
  53.  
  54.         mData = new ObjectHandler();
  55.     }
  56.  
  57.     @Override
  58.     public void onAccuracyChanged(Sensor sensor, int accuracy) {
  59.         // TODO Auto-generated method stub
  60.     }
  61.  
  62.     @Override
  63.     public void onSensorChanged(SensorEvent event) {
  64.  
  65.  
  66.         if (event.sensor == mRotationSensor) {
  67.  
  68.             update(event.values);
  69.         }
  70.  
  71.         if(event.sensor == mStepSensor) {
  72.  
  73.             norming = Math.sqrt((event.values[0]*event.values[0])+(event.values[1]*event.values[1])+(event.values[2]*event.values[2]));
  74.             stepCount(norming);
  75.         }
  76.     }
  77.  
  78.     private void update(float[] vectors) {
  79.  
  80.         float[] rotationMatrix = new float[9];
  81.         SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
  82.  
  83.         float[] orientation = new float[3];
  84.         SensorManager.getOrientation(rotationMatrix, orientation);
  85.  
  86.         float xdeg =  orientation[0]* FROM_RADS_TO_DEGS;
  87.         mData.ObjectHandlersetAngle(xdeg);
  88.     }
  89.  
  90.     protected void onPause() {
  91.  
  92.         mSensorManager.unregisterListener((SensorEventListener) this);
  93.         super.onPause();
  94.     }
  95.  
  96.     public void stepCount (double mNorming){
  97.         if (norming > 10.403 )
  98.  
  99.             pmax = 1;
  100.  
  101.         if (norming < 8.45)
  102.  
  103.             pmin = 1;
  104.  
  105.         if (pmax == 1 && pmin == 1) {
  106.  
  107.             if (count == 0){
  108.                 count++;
  109.                 actualTime = System.currentTimeMillis();
  110.                 if(mStartingAngle == 0)
  111.                 {
  112.                     mStartingAngle = mData.ObjectHandlergetAngle();
  113.                 }
  114.                 myView.newPointAdd((int) (myView.getLastX()-Math.round(93*Math.cos(Math.toRadians(mData.ObjectHandlergetAngle()-mStartingAngle))) ), (int) (myView.getLastY()-Math.round(93*Math.sin(Math.toRadians(mData.ObjectHandlergetAngle()-mStartingAngle)))));
  115.  
  116.             }
  117.  
  118.             else {
  119.                 if (System.currentTimeMillis() - actualTime > 400) {
  120.                     count++;
  121.                     actualTime = System.currentTimeMillis();
  122.                     int xnew = (int) (myView.getLastX()-Math.round(93*Math.cos(Math.toRadians(mData.ObjectHandlergetAngle()-mStartingAngle))));
  123.                     int ynew = (int) (myView.getLastY()-Math.round(93*Math.sin(Math.toRadians(mData.ObjectHandlergetAngle()-mStartingAngle))));
  124.                     myView.newPointAdd(xnew,ynew);
  125.                 }
  126.             }
  127.  
  128.             pmin = 0;
  129.             pmax = 0;
  130.         }
  131.     }
  132. }
RAW Paste Data
Top