2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.Sensor
22 /// The UncalibratedMagnetometer sensor class is used for registering callbacks for the uncalibrated magnetometer and getting the uncalibrated magnetometer data.
24 /// <since_tizen> 3 </since_tizen>
25 public sealed class UncalibratedMagnetometer : Sensor
27 private static string UncalibratedMagnetometerKey = "http://tizen.org/feature/sensor.magnetometer.uncalibrated";
29 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
31 /// Gets the X component of the acceleration.
33 /// <since_tizen> 3 </since_tizen>
34 /// <value> X </value>
35 public float X { get; private set; } = float.MinValue;
38 /// Gets the Y component of the acceleration.
40 /// <since_tizen> 3 </since_tizen>
41 /// <value> Y </value>
42 public float Y { get; private set; } = float.MinValue;
45 /// Gets the Z component of the acceleration.
47 /// <since_tizen> 3 </since_tizen>
48 /// <value> Z </value>
49 public float Z { get; private set; } = float.MinValue;
52 /// Gets the BiasX component of the uncalibrated magnetometer data.
54 /// <since_tizen> 3 </since_tizen>
55 /// <value> The X bias. </value>
56 public float BiasX { get; private set; } = 0;
59 /// Gets the BiasY component of the uncalibrated magnetometer data.
61 /// <since_tizen> 3 </since_tizen>
62 /// <value> The Y bias. </value>
63 public float BiasY { get; private set; } = 0;
66 /// Gets the BiasZ component of the uncalibrated magnetometer data.
68 /// <since_tizen> 3 </since_tizen>
69 /// <value> The Z bias. </value>
70 public float BiasZ { get; private set; } = 0;
73 /// Returns true or false based on whether the uncalibrated magnetometer sensor is supported by the device.
75 /// <since_tizen> 3 </since_tizen>
76 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
77 public static bool IsSupported
81 Log.Info(Globals.LogTag, "Checking if the UncalibratedMagnetometer sensor is supported");
82 return CheckIfSupported(SensorType.UncalibratedMagnetometer, UncalibratedMagnetometerKey);
87 /// Returns the number of uncalibrated magnetometer sensors available on the device.
89 /// <since_tizen> 3 </since_tizen>
90 /// <value> The count of uncalibrated magnetometer sensors. </value>
91 public static int Count
95 Log.Info(Globals.LogTag, "Getting the count of uncalibrated magnetometer sensors");
101 /// Initializes a new instance of the <see cref="Tizen.Sensor.UncalibratedMagnetometer"/> class.
103 /// <since_tizen> 3 </since_tizen>
104 /// <feature>http://tizen.org/feature/sensor.magnetometer.uncalibrated</feature>
105 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
106 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
107 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
108 /// <param name='index'>
109 /// Index. Default value for this is 0. Index refers to a particular uncalibrated magnetometer sensor in case of multiple sensors.
111 public UncalibratedMagnetometer(uint index = 0) : base(index)
113 Log.Info(Globals.LogTag, "Creating UncalibratedMagnetometer object");
116 internal override SensorType GetSensorType()
118 return SensorType.UncalibratedMagnetometer;
122 /// An event handler for storing the callback functions for the event corresponding to the change in the uncalibrated magnetometer sensor data.
124 /// <since_tizen> 3 </since_tizen>
126 public event EventHandler<UncalibratedMagnetometerDataUpdatedEventArgs> DataUpdated;
129 /// An event handler for accuracy changed events.
131 /// <since_tizen> 3 </since_tizen>
132 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
136 if (_accuracyChanged == null)
138 AccuracyListenStart();
140 _accuracyChanged += value;
144 _accuracyChanged -= value;
145 if (_accuracyChanged == null)
147 AccuracyListenStop();
152 private static int GetCount()
156 int error = Interop.SensorManager.GetSensorList(SensorType.UncalibratedMagnetometer, out list, out count);
157 if (error != (int)SensorError.None)
159 Log.Error(Globals.LogTag, "Error getting sensor list for uncalibrated magnetometer");
163 Interop.Libc.Free(list);
167 private static Interop.SensorListener.SensorEventCallback _callback;
169 internal override void EventListenStart()
171 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
172 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
174 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
175 X = sensorData.values[0];
176 Y = sensorData.values[1];
177 Z = sensorData.values[2];
178 BiasX = sensorData.values[3];
179 BiasY = sensorData.values[4];
180 BiasZ = sensorData.values[5];
182 DataUpdated?.Invoke(this, new UncalibratedMagnetometerDataUpdatedEventArgs(sensorData.values));
185 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
186 if (error != (int)SensorError.None)
188 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated magnetometer sensor");
189 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated magnetometer");
193 internal override void EventListenStop()
195 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
196 if (error != (int)SensorError.None)
198 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated magnetometer sensor");
199 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated magnetometer");
203 private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
205 private void AccuracyListenStart()
207 _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
208 TimeSpan = new TimeSpan((Int64)timestamp);
209 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
212 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
213 if (error != (int)SensorError.None)
215 Log.Error(Globals.LogTag, "Error setting accuracy event callback for uncalibrated magnetometer");
216 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for uncalibrated magnetometer");
220 private void AccuracyListenStop()
222 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
223 if (error != (int)SensorError.None)
225 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for uncalibrated magnetometer");
226 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for uncalibrated magnetometer");