be446496d5c8b3749881d8132ab835133982d9f9
[platform/core/csapi/sensor.git] / Tizen.Sensor / Tizen.Sensor / Plugins / RotationVectorSensor.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
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.
8
9 using System;
10
11 namespace Tizen.Sensor
12 {
13     /// <summary>
14     /// RotationVectorSensor Class. Used for registering callbacks for rotation vector sensor and getting rotation vector data
15     /// /// </summary>
16     public class RotationVectorSensor : Sensor
17     {
18         private static string RotationVectorKey = "http://tizen.org/feature/sensor.rotation_vector";
19
20         private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
21         /// <summary>
22         /// Gets the X component of the rotation vector.
23         /// </summary>
24         public float X { get; private set; }
25
26         /// <summary>
27         /// Gets the Y component of the rotation vector.
28         /// </summary>
29         public float Y { get; private set; }
30
31         /// <summary>
32         /// Gets the Z component of the rotation vector.
33         /// </summary>
34         public float Z { get; private set; }
35
36         /// <summary>
37         /// Gets the W component of the rotation vector.
38         /// </summary>
39         public float W { get; private set; }
40
41         /// <summary>
42         /// Gets the Accuracy of the rotation vector data.
43         /// </summary>
44         public SensorDataAccuracy Accuracy { get; private set; }
45
46         /// <summary>
47         /// Returns true or false based on whether rotation vector sensor is supported by device.
48         /// </summary>
49         public static bool IsSupported
50         {
51             get
52             {
53                 Log.Info(Globals.LogTag, "Checking if the RotationVectorSensor is supported");
54                 return CheckIfSupported(SensorType.RotationVectorSensor, RotationVectorKey);
55             }
56         }
57
58         /// <summary>
59         /// Returns the number of rotation vector sensors available on the device.
60         /// </summary>
61         public static int Count
62         {
63             get
64             {
65                 Log.Info(Globals.LogTag, "Getting the count of rotation vector sensors");
66                 return GetCount();
67             }
68         }
69
70         /// <summary>
71         /// Initializes a new instance of the <see cref="Tizen.Sensor.RotationVectorSensor"/> class.
72         /// </summary>
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
78         /// </param>
79         public RotationVectorSensor(int index = 0) : base(index)
80         {
81             Log.Info(Globals.LogTag, "Creating RotationVectorSensor object");
82         }
83
84         internal override SensorType GetSensorType()
85         {
86             return SensorType.RotationVectorSensor;
87         }
88
89         /// <summary>
90         /// Event Handler for storing the callback functions for event corresponding to change in rotation vector sensor data.
91         /// </summary>
92
93         public event EventHandler<RotationVectorSensorDataUpdatedEventArgs> DataUpdated;
94
95         /// <summary>
96         /// Event handler for accuracy changed events.
97         /// </summary>
98         public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
99         {
100             add
101             {
102                 if (_accuracyChanged == null)
103                 {
104                     AccuracyListenStart();
105                 }
106                 _accuracyChanged += value;
107             }
108             remove
109             {
110                 _accuracyChanged -= value;
111                 if (_accuracyChanged == null)
112                 {
113                     AccuracyListenStop();
114                 }
115             }
116         }
117
118         private static int GetCount()
119         {
120             IntPtr list;
121             int count;
122             int error = Interop.SensorManager.GetSensorList(SensorType.RotationVectorSensor, out list, out count);
123             if (error != (int)SensorError.None)
124             {
125                 Log.Error(Globals.LogTag, "Error getting sensor list for rotation vector");
126                 count = 0;
127             }
128             else
129                 Interop.Libc.Free(list);
130             return count;
131         }
132
133         protected override void EventListenStart()
134         {
135             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
136             if (error != (int)SensorError.None)
137             {
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");
140             }
141         }
142
143         protected override void EventListenStop()
144         {
145             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
146             if (error != (int)SensorError.None)
147             {
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");
150             }
151         }
152
153         private void AccuracyListenStart()
154         {
155             int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, Interval, AccuracyEventCallback, IntPtr.Zero);
156             if (error != (int)SensorError.None)
157             {
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");
160             }
161         }
162
163         private void AccuracyListenStop()
164         {
165             int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
166             if (error != (int)SensorError.None)
167             {
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");
170             }
171         }
172
173         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
174         {
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;
181
182             DataUpdated?.Invoke(this, new RotationVectorSensorDataUpdatedEventArgs(sensorData.values, sensorData.accuracy));
183         }
184
185         private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
186         {
187             TimeSpan = new TimeSpan((Int64)timestamp);
188             Accuracy = accuracy;
189             _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
190         }
191     }
192 }