1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
11 namespace Tizen.Sensor
14 /// UncalibratedMagnetometer Sensor Class. Used for registering callbacks for uncalibrated magnetometer and getting uncalibrated magnetometer data
16 public class UncalibratedMagnetometer : Sensor
18 private static string UncalibratedMagnetometerKey = "http://tizen.org/feature/sensor.magnetometer.uncalibrated";
20 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
22 /// Gets the X component of the acceleration.
24 public float X { get; private set; }
27 /// Gets the Y component of the acceleration.
29 public float Y { get; private set; }
32 /// Gets the Z component of the acceleration.
34 public float Z { get; private set; }
37 /// Gets the BiasX component of the uncalibrated magnetometer data.
39 public float BiasX { get; private set; }
42 /// Gets the BiasY component of the uncalibrated magnetometer data.
44 public float BiasY { get; private set; }
47 /// Gets the BiasZ component of the uncalibrated magnetometer data.
49 public float BiasZ { get; private set; }
52 /// Returns true or false based on whether uncalibrated magnetometer sensor is supported by device.
54 public static bool IsSupported
58 Log.Info(Globals.LogTag, "Checking if the UncalibratedMagnetometer sensor is supported");
59 return CheckIfSupported(SensorType.UncalibratedMagnetometer, UncalibratedMagnetometerKey);
64 /// Returns the number of uncalibrated magnetometer sensors available on the device.
66 public static int Count
70 Log.Info(Globals.LogTag, "Getting the count of uncalibrated magnetometer sensors");
76 /// Initializes a new instance of the <see cref="Tizen.Sensor.UncalibratedMagnetometer"/> class.
78 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
79 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
80 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
81 /// <param name='index'>
82 /// Index. Default value for this is 0. Index refers to a particular uncalibrated magnetometer sensor in case of multiple sensors
84 public UncalibratedMagnetometer(uint index = 0) : base(index)
86 Log.Info(Globals.LogTag, "Creating UncalibratedMagnetometer object");
89 internal override SensorType GetSensorType()
91 return SensorType.UncalibratedMagnetometer;
95 /// Event Handler for storing the callback functions for event corresponding to change in uncalibrated magnetometer sensor data.
98 public event EventHandler<UncalibratedMagnetometerDataUpdatedEventArgs> DataUpdated;
101 /// Event handler for accuracy changed events.
103 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
107 if (_accuracyChanged == null)
109 AccuracyListenStart();
111 _accuracyChanged += value;
115 _accuracyChanged -= value;
116 if (_accuracyChanged == null)
118 AccuracyListenStop();
123 private static int GetCount()
127 int error = Interop.SensorManager.GetSensorList(SensorType.UncalibratedMagnetometer, out list, out count);
128 if (error != (int)SensorError.None)
130 Log.Error(Globals.LogTag, "Error getting sensor list for uncalibrated magnetometer");
134 Interop.Libc.Free(list);
138 protected override void EventListenStart()
140 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
141 if (error != (int)SensorError.None)
143 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated magnetometer sensor");
144 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated magnetometer");
148 protected override void EventListenStop()
150 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
151 if (error != (int)SensorError.None)
153 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated magnetometer sensor");
154 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated magnetometer");
158 private void AccuracyListenStart()
160 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, Interval, AccuracyEventCallback, IntPtr.Zero);
161 if (error != (int)SensorError.None)
163 Log.Error(Globals.LogTag, "Error setting accuracy event callback for uncalibrated magnetometer");
164 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for uncalibrated magnetometer");
168 private void AccuracyListenStop()
170 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
171 if (error != (int)SensorError.None)
173 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for uncalibrated magnetometer");
174 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for uncalibrated magnetometer");
178 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
180 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
181 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
182 X = sensorData.values[0];
183 Y = sensorData.values[1];
184 Z = sensorData.values[2];
185 BiasX = sensorData.values[3];
186 BiasY = sensorData.values[4];
187 BiasZ = sensorData.values[5];
189 DataUpdated?.Invoke(this, new UncalibratedMagnetometerDataUpdatedEventArgs(sensorData.values));
192 private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
194 TimeSpan = new TimeSpan((Int64)timestamp);
195 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));