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 /// UncalibratedMagnetometer Sensor Class. Used for registering callbacks for uncalibrated magnetometer and getting uncalibrated magnetometer data
24 public sealed class UncalibratedMagnetometer : Sensor
26 private static string UncalibratedMagnetometerKey = "http://tizen.org/feature/sensor.magnetometer.uncalibrated";
28 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
30 /// Gets the X component of the acceleration.
32 /// <since_tizen> 3 </since_tizen>
33 /// <value> X </value>
34 public float X { get; private set; } = float.MinValue;
37 /// Gets the Y component of the acceleration.
39 /// <since_tizen> 3 </since_tizen>
40 /// <value> Y </value>
41 public float Y { get; private set; } = float.MinValue;
44 /// Gets the Z component of the acceleration.
46 /// <since_tizen> 3 </since_tizen>
47 /// <value> Z </value>
48 public float Z { get; private set; } = float.MinValue;
51 /// Gets the BiasX component of the uncalibrated magnetometer data.
53 /// <since_tizen> 3 </since_tizen>
54 /// <value> X bias </value>
55 public float BiasX { get; private set; } = 0;
58 /// Gets the BiasY component of the uncalibrated magnetometer data.
60 /// <since_tizen> 3 </since_tizen>
61 /// <value> Y bias </value>
62 public float BiasY { get; private set; } = 0;
65 /// Gets the BiasZ component of the uncalibrated magnetometer data.
67 /// <since_tizen> 3 </since_tizen>
68 /// <value> Z bias </value>
69 public float BiasZ { get; private set; } = 0;
72 /// Returns true or false based on whether uncalibrated magnetometer sensor is supported by device.
74 /// <since_tizen> 3 </since_tizen>
75 /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
76 public static bool IsSupported
80 Log.Info(Globals.LogTag, "Checking if the UncalibratedMagnetometer sensor is supported");
81 return CheckIfSupported(SensorType.UncalibratedMagnetometer, UncalibratedMagnetometerKey);
86 /// Returns the number of uncalibrated magnetometer sensors available on the device.
88 /// <since_tizen> 3 </since_tizen>
89 /// <value> The count of uncalibrated magnetometer sensors </value>
90 public static int Count
94 Log.Info(Globals.LogTag, "Getting the count of uncalibrated magnetometer sensors");
100 /// Initializes a new instance of the <see cref="Tizen.Sensor.UncalibratedMagnetometer"/> class.
102 /// <since_tizen> 3 </since_tizen>
103 /// <feature>http://tizen.org/feature/sensor.magnetometer.uncalibrated</feature>
104 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
105 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
106 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
107 /// <param name='index'>
108 /// Index. Default value for this is 0. Index refers to a particular uncalibrated magnetometer sensor in case of multiple sensors
110 public UncalibratedMagnetometer(uint index = 0) : base(index)
112 Log.Info(Globals.LogTag, "Creating UncalibratedMagnetometer object");
115 internal override SensorType GetSensorType()
117 return SensorType.UncalibratedMagnetometer;
121 /// Event Handler for storing the callback functions for event corresponding to change in uncalibrated magnetometer sensor data.
123 /// <since_tizen> 3 </since_tizen>
125 public event EventHandler<UncalibratedMagnetometerDataUpdatedEventArgs> DataUpdated;
128 /// Event handler for accuracy changed events.
130 /// <since_tizen> 3 </since_tizen>
131 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
135 if (_accuracyChanged == null)
137 AccuracyListenStart();
139 _accuracyChanged += value;
143 _accuracyChanged -= value;
144 if (_accuracyChanged == null)
146 AccuracyListenStop();
151 private static int GetCount()
155 int error = Interop.SensorManager.GetSensorList(SensorType.UncalibratedMagnetometer, out list, out count);
156 if (error != (int)SensorError.None)
158 Log.Error(Globals.LogTag, "Error getting sensor list for uncalibrated magnetometer");
162 Interop.Libc.Free(list);
166 private static Interop.SensorListener.SensorEventCallback _callback;
168 internal override void EventListenStart()
170 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
171 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
173 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
174 X = sensorData.values[0];
175 Y = sensorData.values[1];
176 Z = sensorData.values[2];
177 BiasX = sensorData.values[3];
178 BiasY = sensorData.values[4];
179 BiasZ = sensorData.values[5];
181 DataUpdated?.Invoke(this, new UncalibratedMagnetometerDataUpdatedEventArgs(sensorData.values));
184 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
185 if (error != (int)SensorError.None)
187 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated magnetometer sensor");
188 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated magnetometer");
192 internal override void EventListenStop()
194 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
195 if (error != (int)SensorError.None)
197 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated magnetometer sensor");
198 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated magnetometer");
202 private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
204 private void AccuracyListenStart()
206 _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
207 TimeSpan = new TimeSpan((Int64)timestamp);
208 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
211 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
212 if (error != (int)SensorError.None)
214 Log.Error(Globals.LogTag, "Error setting accuracy event callback for uncalibrated magnetometer");
215 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for uncalibrated magnetometer");
219 private void AccuracyListenStop()
221 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
222 if (error != (int)SensorError.None)
224 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for uncalibrated magnetometer");
225 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for uncalibrated magnetometer");