Skip to content

Commit

Permalink
Merge pull request #36 from KalebKE/v2.2
Browse files Browse the repository at this point in the history
v2.2
  • Loading branch information
KalebKE authored Jul 17, 2020
2 parents 79f326f + e89ffa2 commit 03ac30e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion fsensor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
minSdkVersion 14
targetSdkVersion 29
versionCode 9
versionName "2.0"
versionName "2.2"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class AccelerationSensor implements FSensor {

private final SensorSubject sensorSubject;

private int sensorType = Sensor.TYPE_ACCELEROMETER;

public AccelerationSensor(Context context) {
this.sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
this.listener = new SimpleSensorListener();
Expand Down Expand Up @@ -69,6 +71,18 @@ public void unregister(SensorSubject.SensorObserver sensorObserver) {
sensorSubject.unregister(sensorObserver);
}

/**
* Set the gyroscope sensor type.
* @param sensorType must be Sensor.TYPE_GYROSCOPE or Sensor.TYPE_GYROSCOPE_UNCALIBRATED
*/
public void setSensorType(int sensorType) {
if(sensorType != Sensor.TYPE_ACCELEROMETER && sensorType != Sensor.TYPE_ACCELEROMETER_UNCALIBRATED && sensorType != Sensor.TYPE_LINEAR_ACCELERATION) {
throw new IllegalStateException("Sensor Type must be Sensor.TYPE_ACCELEROMETER or Sensor.TYPE_ACCELEROMETER_UNCALIBRATED or sensorType != Sensor.TYPE_LINEAR_ACCELERATION");
}

this.sensorType = sensorType;
}

/**
* Set the sensor frequency.
* @param sensorDelay Must be SensorManager.SENSOR_DELAY_FASTEST, SensorManager.SENSOR_DELAY_GAME, SensorManager.SENSOR_DELAY_NORMAL or SensorManager.SENSOR_DELAY_UI
Expand Down Expand Up @@ -112,7 +126,7 @@ private void processAcceleration(float[] acceleration) {
private void registerSensors(int sensorDelay) {
// Register for sensor updates.
sensorManager.registerListener(listener, sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
.getDefaultSensor(sensorType),
sensorDelay);
}

Expand All @@ -130,7 +144,7 @@ private class SimpleSensorListener implements SensorEventListener {

@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
if (event.sensor.getType() == sensorType) {

processAcceleration(event.values);
setOutput(acceleration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,24 @@ public static float[] getGravityFromOrientation(float[] orientation) {

float[] components = new float[3];

float pitch = orientation[1];
float roll = orientation[2];

// Find the gravity component of the X-axis
// = g*-cos(pitch)*sin(roll);
components[0] = (float) -(gravity[0]
* -Math.cos(orientation[0]) * Math
.sin(orientation[1]));
* -Math.cos(pitch) * Math
.sin(roll));

// Find the gravity component of the Y-axis
// = g*-sin(pitch);
components[1] = (float) (gravity[1] * -Math
.sin(orientation[0]));
.sin(pitch));

// Find the gravity component of the Z-axis
// = g*cos(pitch)*cos(roll);
components[2] = (float) (gravity[2]
* Math.cos(orientation[0]) * Math.cos(orientation[1]));
* Math.cos(pitch) * Math.cos(roll));

return components;
}
Expand Down

0 comments on commit 03ac30e

Please sign in to comment.