Skip to content

Commit

Permalink
-rotation and bearing is now working
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaydashrath committed Jul 12, 2012
1 parent 9ffeb3e commit 8954610
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions CompassExample/res/layout/activity_compass.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@android:id/text1"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".CompassActivity" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public abstract class BasicSensorActivity extends SherlockFragmentActivity imple
private SensorManager sensorManager;
private Sensor acclSensor;
private Sensor magFieldSensor;

private double bearing = 0;
private int rotationDegrees = 0;


public BroadcastReceiver freshLocationReceiver = new BroadcastReceiver() {
Expand Down Expand Up @@ -152,7 +155,24 @@ private void updateSensorValues(SensorEvent event) {
floatBearing += magField.getDeclination();
if (floatBearing < 0)
floatBearing += 360;
//GlobalData.setBearing((int) floatBearing);
setBearing(floatBearing);
setRotationInDegrees(floatBearing);
}

private void setRotationInDegrees(double floatBearing) {
this.bearing = floatBearing;
}

private void setBearing(double floatBearing) {
this.rotationDegrees = (int)(360 - floatBearing);
}

public int getRotation(){
return rotationDegrees;
}

public double getBearing(){
return bearing;
}

private void setmagValues(SensorEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,53 @@

import com.novoda.example.compass.R;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.os.Bundle;
import android.widget.TextView;

public class CompassActivity extends BasicSensorActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compass);

}

@Override
public void onSensorChanged(SensorEvent evt) {
super.onSensorChanged(evt);
updateText();
}

private void updateText() {
TextView text = (TextView)findViewById(android.R.id.text1);
String dirTxt = getDirectionFromBearing(getBearing());
text.setText(" Direction = " + dirTxt + " Rotation in degress " + getRotation());

}

private String getDirectionFromBearing(double bearing) {
int range = (int) (bearing / (360f / 16f));
String dirTxt = "";
if (range == 15 || range == 0)
dirTxt = "N";
else if (range == 1 || range == 2)
dirTxt = "NE";
else if (range == 3 || range == 4)
dirTxt = "E";
else if (range == 5 || range == 6)
dirTxt = "SE";
else if (range == 7 || range == 8)
dirTxt = "S";
else if (range == 9 || range == 10)
dirTxt = "SW";
else if (range == 11 || range == 12)
dirTxt = "W";
else if (range == 13 || range == 14)
dirTxt = "NW";
return dirTxt;
}


Expand Down

0 comments on commit 8954610

Please sign in to comment.