-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.cs
121 lines (93 loc) · 4.2 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System.Collections.Generic;
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Hardware;
namespace AccelTest
{
[Activity(Label = "Acceleration Sensor Test", MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
public class MainActivity : Activity
{
enum Axis { AxisX, AxisY, AxisZ };
Axis ChosenAxis = Axis.AxisX;
Accelerometer accelerometer;
CurrentCoordinates currentCoords;
TextView posText, velText;
TextView accText, gravText;
TextView linText;
RadioButton RadioX, RadioY, RadioZ;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.button1);
velText = FindViewById<TextView>(Resource.Id.velocitytext);
posText = FindViewById<TextView>(Resource.Id.positiontext);
accText = FindViewById<TextView>(Resource.Id.accelerationtext);
gravText = FindViewById<TextView>(Resource.Id.gravitytext);
linText = FindViewById<TextView>(Resource.Id.linearaccelerationtext);
RadioX = FindViewById<RadioButton>(Resource.Id.radioAxisX);
RadioY = FindViewById<RadioButton>(Resource.Id.radioAxisY);
RadioZ = FindViewById<RadioButton>(Resource.Id.radioAxisZ);
SensorManager sensor = (SensorManager)GetSystemService(SensorService);
accelerometer = new Accelerometer(this, sensor);
if (accelerometer.isLinearAccelerationAvaliable)
linText.Text = "Linear acceleration is ON";
else
linText.Text = "Linear aceleration is OFF";
currentCoords = new CurrentCoordinates();
accelerometer.sensorChanged = new SensorChanged(currentCoords.IntegrateCoordinates);
accelerometer.sensorChanged += UpdateTexts;
currentCoords.CoordsChanged = new CoordinatesChanged(UpdateTexts);
button.Text = "Reset position";
button.Click += (object sender, EventArgs e) =>
{
currentCoords.Reset();
};
RadioX.Checked = true;
RadioX.Click += (object sender, EventArgs e) => { ChosenAxis = Axis.AxisX; };
RadioY.Click += (object sender, EventArgs e) => { ChosenAxis = Axis.AxisY; };
RadioZ.Click += (object sender, EventArgs e) => { ChosenAxis = Axis.AxisZ; };
}
void UpdateTexts(Coordinates args)
{
switch (ChosenAxis)
{
case (Axis.AxisX):
posText.Text = string.Format("X = {0}", args.X);
velText.Text = string.Format("V.x = {0}", args.Vx);
break;
case (Axis.AxisY):
posText.Text = string.Format("Y = {0}", args.Y);
velText.Text = string.Format("V.y = {0}", args.Vx);
break;
case (Axis.AxisZ):
posText.Text = string.Format("Z = {0}", args.Z);
velText.Text = string.Format("V.z = {0}", args.Vx);
break;
}
}
void UpdateTexts(OnSensorChangedArgs args)
{
switch (ChosenAxis)
{
case (Axis.AxisX):
accText.Text = string.Format("A.x = {0}", args.AccelerationX);
gravText.Text = string.Format("G.x = {0}", args.GravityX);
break;
case (Axis.AxisY):
accText.Text = string.Format("A.y = {0}", args.AccelerationY);
gravText.Text = string.Format("G.y = {0}", args.GravityY);
break;
case (Axis.AxisZ):
accText.Text = string.Format("A.z = {0}", args.AccelerationZ);
gravText.Text = string.Format("G.z = {0}", args.GravityZ);
break;
}
}
}
}