302efec48de10ac2f697afd5a92c261b90de3c19
[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         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
78         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
79         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
80         /// <param name='index'>
81         /// Index. Default value for this is 0. Index refers to a particular uncalibrated gyroscope sensor in case of multiple sensors
82         /// </param>
83         public UncalibratedGyroscope(int index = 0) : base(index)
84         {
85             Log.Info(Globals.LogTag, "Creating UncalibratedGyroscope object");
86         }
87
88         internal override SensorType GetSensorType()
89         {
90             return SensorType.UncalibratedGyroscope;
91         }
92
93         /// <summary>
94         /// Event Handler for storing the callback functions for event corresponding to change in uncalibrated gyroscope sensor data.
95         /// </summary>
96
97         public event EventHandler<UncalibratedGyroscopeDataUpdatedEventArgs> DataUpdated;
98
99         private static int GetCount()
100         {
101             IntPtr list;
102             int count;
103             int error = Interop.SensorManager.GetSensorList(SensorType.UncalibratedGyroscope, out list, out count);
104             if (error != (int)SensorError.None)
105             {
106                 Log.Error(Globals.LogTag, "Error getting sensor list for uncalibrated gyroscope");
107                 count = 0;
108             }
109             else
110                 Interop.Libc.Free(list);
111             return count;
112         }
113
114         protected override void EventListenStart()
115         {
116             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
117             if (error != (int)SensorError.None)
118             {
119                 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated gyroscope sensor");
120                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated gyroscope");
121             }
122         }
123
124         protected override void EventListenStop()
125         {
126             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
127             if (error != (int)SensorError.None)
128             {
129                 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated gyroscope sensor");
130                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated gyroscope");
131             }
132         }
133
134         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
135         {
136             Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
137             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
138             X = sensorData.values[0];
139             Y = sensorData.values[1];
140             Z = sensorData.values[2];
141             BiasX = sensorData.values[3];
142             BiasY = sensorData.values[4];
143             BiasZ = sensorData.values[5];
144
145             DataUpdated?.Invoke(this, new UncalibratedGyroscopeDataUpdatedEventArgs(sensorData.values));
146         }
147     }
148 }