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 /// RotationVectorSensor Class. Used for registering callbacks for rotation vector sensor and getting rotation vector data
16 public class RotationVectorSensor : Sensor
18 private static string RotationVectorKey = "http://tizen.org/feature/sensor.rotation_vector";
20 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
22 /// Gets the X component of the rotation vector.
24 public float X { get; private set; }
27 /// Gets the Y component of the rotation vector.
29 public float Y { get; private set; }
32 /// Gets the Z component of the rotation vector.
34 public float Z { get; private set; }
37 /// Gets the W component of the rotation vector.
39 public float W { get; private set; }
42 /// Gets the Accuracy of the rotation vector data.
44 public SensorDataAccuracy Accuracy { get; private set; }
47 /// Returns true or false based on whether rotation vector sensor is supported by device.
49 public static bool IsSupported
53 Log.Info(Globals.LogTag, "Checking if the RotationVectorSensor is supported");
54 return CheckIfSupported(SensorType.RotationVectorSensor, RotationVectorKey);
59 /// Returns the number of rotation vector sensors available on the device.
61 public static int Count
65 Log.Info(Globals.LogTag, "Getting the count of rotation vector sensors");
71 /// Initializes a new instance of the <see cref="Tizen.Sensor.RotationVectorSensor"/> class.
73 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
74 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
75 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
76 /// <param name='index'>
77 /// Index. Default value for this is 0. Index refers to a particular rotation vector sensor in case of multiple sensors
79 public RotationVectorSensor(int index = 0) : base(index)
81 Log.Info(Globals.LogTag, "Creating RotationVectorSensor object");
84 internal override SensorType GetSensorType()
86 return SensorType.RotationVectorSensor;
90 /// Event Handler for storing the callback functions for event corresponding to change in rotation vector sensor data.
93 public event EventHandler<RotationVectorSensorDataUpdatedEventArgs> DataUpdated;
96 /// Event handler for accuracy changed events.
98 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
102 if (_accuracyChanged == null)
104 AccuracyListenStart();
106 _accuracyChanged += value;
110 _accuracyChanged -= value;
111 if (_accuracyChanged == null)
113 AccuracyListenStop();
118 private static int GetCount()
122 int error = Interop.SensorManager.GetSensorList(SensorType.RotationVectorSensor, out list, out count);
123 if (error != (int)SensorError.None)
125 Log.Error(Globals.LogTag, "Error getting sensor list for rotation vector");
129 Interop.Libc.Free(list);
133 protected override void EventListenStart()
135 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
136 if (error != (int)SensorError.None)
138 Log.Error(Globals.LogTag, "Error setting event callback for rotation vector sensor");
139 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for rotation vector");
143 protected override void EventListenStop()
145 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
146 if (error != (int)SensorError.None)
148 Log.Error(Globals.LogTag, "Error unsetting event callback for rotation vector sensor");
149 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for rotation vector");
153 private void AccuracyListenStart()
155 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, Interval, AccuracyEventCallback, IntPtr.Zero);
156 if (error != (int)SensorError.None)
158 Log.Error(Globals.LogTag, "Error setting accuracy event callback for rotation vector sensor");
159 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for rotation vector");
163 private void AccuracyListenStop()
165 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
166 if (error != (int)SensorError.None)
168 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for rotation vector sensor");
169 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for rotation vector");
173 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
175 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
176 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
177 X = sensorData.values[0];
178 Y = sensorData.values[1];
179 Z = sensorData.values[2];
180 Accuracy = sensorData.accuracy;
182 DataUpdated?.Invoke(this, new RotationVectorSensorDataUpdatedEventArgs(sensorData.values, sensorData.accuracy));
185 private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
187 TimeSpan = new TimeSpan((Int64)timestamp);
189 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));