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