Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / Gyroscope.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     /// Gyroscope Sensor Class. Used for registering callbacks for gyroscope and getting gyroscope data
23     /// </summary>
24     public sealed class Gyroscope : Sensor
25     {
26         private const string GyroscopeKey = "http://tizen.org/feature/sensor.gyroscope";
27
28         /// <summary>
29         /// Gets the X component of the acceleration.
30         /// </summary>
31         /// <since_tizen> 3 </since_tizen>
32         /// <value> X </value>
33         public float X { get; private set; } = float.MinValue;
34
35         /// <summary>
36         /// Gets the Y component of the acceleration.
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         /// <value> Y </value>
40         public float Y { get; private set; } = float.MinValue;
41
42         /// <summary>
43         /// Gets the Z component of the acceleration.
44         /// </summary>
45         /// <since_tizen> 3 </since_tizen>
46         /// <value> Z </value>
47         public float Z { get; private set; } = float.MinValue;
48
49         /// <summary>
50         /// Returns true or false based on whether gyroscope sensor is supported by device.
51         /// </summary>
52         /// <since_tizen> 3 </since_tizen>
53         /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
54         public static bool IsSupported
55         {
56             get
57             {
58                 Log.Info(Globals.LogTag, "Checking if the Gyroscope sensor is supported");
59                 return CheckIfSupported(SensorType.Gyroscope, GyroscopeKey);
60             }
61         }
62
63         /// <summary>
64         /// Returns the number of gyroscope sensors available on the device.
65         /// </summary>
66         /// <since_tizen> 3 </since_tizen>
67         /// <value> The count of gyroscope sensors </value>
68         public static int Count
69         {
70             get
71             {
72                 Log.Info(Globals.LogTag, "Getting the count of gyroscope sensors");
73                 return GetCount();
74             }
75         }
76
77         /// <summary>
78         /// Initializes a new instance of the <see cref="Tizen.Sensor.Gyroscope"/> class.
79         /// </summary>
80         /// <since_tizen> 3 </since_tizen>
81         /// <feature>http://tizen.org/feature/sensor.gyroscope</feature>
82         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
83         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
84         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
85         /// <param name='index'>
86         /// Index. Default value for this is 0. Index refers to a particular gyroscope sensor in case of multiple sensors
87         /// </param>
88         public Gyroscope(uint index = 0) : base(index)
89         {
90             Log.Info(Globals.LogTag, "Creating Gyroscope object");
91         }
92
93         internal override SensorType GetSensorType()
94         {
95             return SensorType.Gyroscope;
96         }
97
98         /// <summary>
99         /// Event Handler for storing the callback functions for event corresponding to change in gyroscope sensor data.
100         /// </summary>
101         /// <since_tizen> 3 </since_tizen>
102
103         public event EventHandler<GyroscopeDataUpdatedEventArgs> DataUpdated;
104
105         private static int GetCount()
106         {
107             IntPtr list;
108             int count;
109             int error = Interop.SensorManager.GetSensorList(SensorType.Gyroscope, out list, out count);
110             if (error != (int)SensorError.None)
111             {
112                 Log.Error(Globals.LogTag, "Error getting sensor list for gyroscope");
113                 count = 0;
114             }
115             else
116                 Interop.Libc.Free(list);
117             return count;
118         }
119
120         private static Interop.SensorListener.SensorEventCallback _callback;
121
122         internal override void EventListenStart()
123         {
124             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
125                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
126
127                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
128                 X = sensorData.values[0];
129                 Y = sensorData.values[1];
130                 Z = sensorData.values[2];
131
132                 DataUpdated?.Invoke(this, new GyroscopeDataUpdatedEventArgs(sensorData.values));
133             };
134
135             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
136             if (error != (int)SensorError.None)
137             {
138                 Log.Error(Globals.LogTag, "Error setting event callback for gyroscope sensor");
139                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gyroscope");
140             }
141         }
142
143         internal 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 gyroscope sensor");
149                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gyroscope");
150             }
151         }
152     }
153 }