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