-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Magnetometer.shared.cs
257 lines (217 loc) · 7.74 KB
/
Magnetometer.shared.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#nullable enable
using System;
using System.Numerics;
using Microsoft.Maui.ApplicationModel;
namespace Microsoft.Maui.Devices.Sensors
{
/// <summary>
/// Detect device's orentation relative to Earth's magnetic field in microteslas (µ).
/// </summary>
public interface IMagnetometer
{
/// <summary>
/// Gets a value indicating whether reading the magnetometer is supported on this device.
/// </summary>
bool IsSupported { get; }
/// <summary>
/// Gets a value indicating whether the magnetometer is actively being monitored.
/// </summary>
bool IsMonitoring { get; }
/// <summary>
/// Start monitoring for changes to the magnetometer.
/// </summary>
/// <param name="sensorSpeed">The speed to listen for changes.</param>
void Start(SensorSpeed sensorSpeed);
/// <summary>
/// Stop monitoring for changes to the magnetometer.
/// </summary>
void Stop();
/// <summary>
/// Occurs when the magnetometer reading changes.
/// </summary>
event EventHandler<MagnetometerChangedEventArgs> ReadingChanged;
}
/// <summary>
/// Detect device's orentation relative to Earth's magnetic field in microteslas (µ).
/// </summary>
public static partial class Magnetometer
{
/// <summary>
/// Occurs when the magnetometer reading changes.
/// </summary>
public static event EventHandler<MagnetometerChangedEventArgs> ReadingChanged
{
add => Current.ReadingChanged += value;
remove => Current.ReadingChanged -= value;
}
/// <summary>
/// Gets a value indicating whether the magnetometer is actively being monitored.
/// </summary>
public static bool IsMonitoring
=> Current.IsMonitoring;
/// <summary>
/// Gets a value indicating whether reading the magnetometer is supported on this device.
/// </summary>
public static bool IsSupported
=> Current.IsSupported;
/// <summary>
/// Start monitoring for changes to the magnetometer.
/// </summary>
/// <param name="sensorSpeed">The speed to listen for changes.</param>
/// <remarks>
/// Will throw <see cref="FeatureNotSupportedException"/> if not supported on device.
/// Will throw <see cref="InvalidOperationException"/> if <see cref="IsMonitoring"/> is <see langword="true"/>.
/// </remarks>
public static void Start(SensorSpeed sensorSpeed)
=> Current.Start(sensorSpeed);
/// <summary>
/// Stop monitoring for changes to the magnetometer.
/// </summary>
public static void Stop()
=> Current.Stop();
static IMagnetometer Current => Devices.Sensors.Magnetometer.Default;
static IMagnetometer? defaultImplementation;
/// <summary>
/// Provides the default implementation for static usage of this API.
/// </summary>
public static IMagnetometer Default =>
defaultImplementation ??= new MagnetometerImplementation();
internal static void SetDefault(IMagnetometer? implementation) =>
defaultImplementation = implementation;
}
/// <summary>
/// Contains the current pressure information from the <see cref="IBarometer.ReadingChanged"/> event.
/// </summary>
public class MagnetometerChangedEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="MagnetometerChangedEventArgs"/> class.
/// </summary>
/// <param name="reading">The magnetometer data reading.</param>
public MagnetometerChangedEventArgs(MagnetometerData reading) =>
Reading = reading;
/// <summary>
/// The current values of the magnetometer.
/// </summary>
public MagnetometerData Reading { get; }
}
/// <summary>
/// Contains the pressure measured by the user's device magnetometer.
/// </summary>
public readonly struct MagnetometerData : IEquatable<MagnetometerData>
{
/// <summary>
/// Initializes a new instance of the <see cref="MagnetometerData"/> class.
/// </summary>
/// <param name="x">X axis data.</param>
/// <param name="y">Y axis data.</param>
/// <param name="z">Z asix data.</param>
public MagnetometerData(double x, double y, double z)
: this((float)x, (float)y, (float)z)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MagnetometerData"/> class.
/// </summary>
/// <param name="x">X axis data.</param>
/// <param name="y">Y axis data.</param>
/// <param name="z">Z asix data.</param>
public MagnetometerData(float x, float y, float z) =>
MagneticField = new Vector3(x, y, z);
/// <summary>
/// Gets the magnetic field vector in microteslas (µ).
/// </summary>
public Vector3 MagneticField { get; }
/// <summary>
/// Compares the underlying <see cref="MagnetometerData"/> instances.
/// </summary>
/// <param name="obj">Object to compare with.</param>
/// <returns><see langword="true"/> if they are equal, otherwise <see langword="false"/>.</returns>
public override bool Equals(object? obj) =>
(obj is MagnetometerData data) && Equals(data);
/// <summary>
/// Compares the underlying <see cref="MagneticField"/> instances.
/// </summary>
/// <param name="other"><see cref="MagnetometerData"/> object to compare with.</param>
/// <returns><see langword="true"/> if they are equal, otherwise <see langword="false"/>.</returns>
public bool Equals(MagnetometerData other) =>
MagneticField.Equals(other.MagneticField);
/// <summary>
/// Equality operator for equals.
/// </summary>
/// <param name="left">Left to compare.</param>
/// <param name="right">Right to compare.</param>
/// <returns><see langword="true"/> if objects are equal, otherwise <see langword="false"/>.</returns>
public static bool operator ==(MagnetometerData left, MagnetometerData right) =>
left.Equals(right);
/// <summary>
/// Inequality operator.
/// </summary>
/// <param name="left">Left to compare.</param>
/// <param name="right">Right to compare.</param>
/// <returns><see langword="true"/> if objects are not equal, otherwise <see langword="false"/>.</returns>
public static bool operator !=(MagnetometerData left, MagnetometerData right) =>
!left.Equals(right);
/// <inheritdoc cref="object.GetHashCode"/>
public override int GetHashCode() =>
MagneticField.GetHashCode();
/// <summary>
/// Returns a string representation of the current values of <see cref="MagneticField"/>.
/// </summary>
/// <returns>A string representation of this instance in the format of <c>MagneticField.X: {value}, MagneticField.Y: {value}, MagneticField.Z: {value}</c>.</returns>
public override string ToString() =>
$"{nameof(MagneticField.X)}: {MagneticField.X}, " +
$"{nameof(MagneticField.Y)}: {MagneticField.Y}, " +
$"{nameof(MagneticField.Z)}: {MagneticField.Z}";
}
partial class MagnetometerImplementation : IMagnetometer
{
bool UseSyncContext => SensorSpeed == SensorSpeed.Default || SensorSpeed == SensorSpeed.UI;
public event EventHandler<MagnetometerChangedEventArgs>? ReadingChanged;
public bool IsMonitoring { get; private set; }
public bool IsSupported => PlatformIsSupported;
SensorSpeed SensorSpeed { get; set; } = SensorSpeed.Default;
public void Start(SensorSpeed sensorSpeed)
{
if (!PlatformIsSupported)
throw new FeatureNotSupportedException();
if (IsMonitoring)
throw new InvalidOperationException("Magnetometer has already been started.");
IsMonitoring = true;
try
{
PlatformStart(sensorSpeed);
}
catch
{
IsMonitoring = false;
throw;
}
}
public void Stop()
{
if (!PlatformIsSupported)
throw new FeatureNotSupportedException();
if (!IsMonitoring)
return;
IsMonitoring = false;
try
{
PlatformStop();
}
catch
{
IsMonitoring = true;
throw;
}
}
void RaiseReadingChanged(MagnetometerData data)
{
var args = new MagnetometerChangedEventArgs(data);
if (UseSyncContext)
MainThread.BeginInvokeOnMainThread(() => ReadingChanged?.Invoke(this, args));
else
ReadingChanged?.Invoke(this, args);
}
}
}