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 public float X { get; private set; } = float.MinValue;
35 /// Gets the Y component of the acceleration.
37 public float Y { get; private set; } = float.MinValue;
40 /// Gets the Z component of the acceleration.
42 public float Z { get; private set; } = float.MinValue;
45 /// Gets the BiasX component of the uncalibrated magnetometer data.
47 public float BiasX { get; private set; } = 0;
50 /// Gets the BiasY component of the uncalibrated magnetometer data.
52 public float BiasY { get; private set; } = 0;
55 /// Gets the BiasZ component of the uncalibrated magnetometer data.
57 public float BiasZ { get; private set; } = 0;
60 /// Returns true or false based on whether uncalibrated magnetometer sensor is supported by device.
62 public static bool IsSupported
66 Log.Info(Globals.LogTag, "Checking if the UncalibratedMagnetometer sensor is supported");
67 return CheckIfSupported(SensorType.UncalibratedMagnetometer, UncalibratedMagnetometerKey);
72 /// Returns the number of uncalibrated magnetometer sensors available on the device.
74 public static int Count
78 Log.Info(Globals.LogTag, "Getting the count of uncalibrated magnetometer sensors");
84 /// Initializes a new instance of the <see cref="Tizen.Sensor.UncalibratedMagnetometer"/> class.
86 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
87 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
88 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
89 /// <param name='index'>
90 /// Index. Default value for this is 0. Index refers to a particular uncalibrated magnetometer sensor in case of multiple sensors
92 public UncalibratedMagnetometer(uint index = 0) : base(index)
94 Log.Info(Globals.LogTag, "Creating UncalibratedMagnetometer object");
97 internal override SensorType GetSensorType()
99 return SensorType.UncalibratedMagnetometer;
103 /// Event Handler for storing the callback functions for event corresponding to change in uncalibrated magnetometer sensor data.
106 public event EventHandler<UncalibratedMagnetometerDataUpdatedEventArgs> DataUpdated;
109 /// Event handler for accuracy changed events.
111 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
115 if (_accuracyChanged == null)
117 AccuracyListenStart();
119 _accuracyChanged += value;
123 _accuracyChanged -= value;
124 if (_accuracyChanged == null)
126 AccuracyListenStop();
131 private static int GetCount()
135 int error = Interop.SensorManager.GetSensorList(SensorType.UncalibratedMagnetometer, out list, out count);
136 if (error != (int)SensorError.None)
138 Log.Error(Globals.LogTag, "Error getting sensor list for uncalibrated magnetometer");
142 Interop.Libc.Free(list);
146 protected internal override void EventListenStart()
148 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
149 if (error != (int)SensorError.None)
151 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated magnetometer sensor");
152 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated magnetometer");
156 protected internal override void EventListenStop()
158 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
159 if (error != (int)SensorError.None)
161 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated magnetometer sensor");
162 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated magnetometer");
166 private void AccuracyListenStart()
168 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, Interval, AccuracyEventCallback, IntPtr.Zero);
169 if (error != (int)SensorError.None)
171 Log.Error(Globals.LogTag, "Error setting accuracy event callback for uncalibrated magnetometer");
172 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for uncalibrated magnetometer");
176 private void AccuracyListenStop()
178 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
179 if (error != (int)SensorError.None)
181 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for uncalibrated magnetometer");
182 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for uncalibrated magnetometer");
186 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
188 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
189 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
190 X = sensorData.values[0];
191 Y = sensorData.values[1];
192 Z = sensorData.values[2];
193 BiasX = sensorData.values[3];
194 BiasY = sensorData.values[4];
195 BiasZ = sensorData.values[5];
197 DataUpdated?.Invoke(this, new UncalibratedMagnetometerDataUpdatedEventArgs(sensorData.values));
200 private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
202 TimeSpan = new TimeSpan((Int64)timestamp);
203 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));