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