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