2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 namespace Tizen.Sensor
22 /// Gyroscope Sensor Class. Used for registering callbacks for gyroscope and getting gyroscope data
24 public sealed class Gyroscope : Sensor
26 private const string GyroscopeKey = "http://tizen.org/feature/sensor.gyroscope";
29 /// Gets the X component of the acceleration.
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> X </value>
33 public float X { get; private set; } = float.MinValue;
36 /// Gets the Y component of the acceleration.
38 /// <since_tizen> 3 </since_tizen>
39 /// <value> Y </value>
40 public float Y { get; private set; } = float.MinValue;
43 /// Gets the Z component of the acceleration.
45 /// <since_tizen> 3 </since_tizen>
46 /// <value> Z </value>
47 public float Z { get; private set; } = float.MinValue;
50 /// Returns true or false based on whether gyroscope sensor is supported by device.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
54 public static bool IsSupported
58 Log.Info(Globals.LogTag, "Checking if the Gyroscope sensor is supported");
59 return CheckIfSupported(SensorType.Gyroscope, GyroscopeKey);
64 /// Returns the number of gyroscope sensors available on the device.
66 /// <since_tizen> 3 </since_tizen>
67 /// <value> The count of gyroscope sensors </value>
68 public static int Count
72 Log.Info(Globals.LogTag, "Getting the count of gyroscope sensors");
78 /// Initializes a new instance of the <see cref="Tizen.Sensor.Gyroscope"/> class.
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
88 public Gyroscope(uint index = 0) : base(index)
90 Log.Info(Globals.LogTag, "Creating Gyroscope object");
93 internal override SensorType GetSensorType()
95 return SensorType.Gyroscope;
99 /// Event Handler for storing the callback functions for event corresponding to change in gyroscope sensor data.
101 /// <since_tizen> 3 </since_tizen>
103 public event EventHandler<GyroscopeDataUpdatedEventArgs> DataUpdated;
105 private static int GetCount()
109 int error = Interop.SensorManager.GetSensorList(SensorType.Gyroscope, out list, out count);
110 if (error != (int)SensorError.None)
112 Log.Error(Globals.LogTag, "Error getting sensor list for gyroscope");
116 Interop.Libc.Free(list);
120 private static Interop.SensorListener.SensorEventCallback _callback;
122 internal override void EventListenStart()
124 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
125 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
127 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
128 X = sensorData.values[0];
129 Y = sensorData.values[1];
130 Z = sensorData.values[2];
132 DataUpdated?.Invoke(this, new GyroscopeDataUpdatedEventArgs(sensorData.values));
135 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
136 if (error != (int)SensorError.None)
138 Log.Error(Globals.LogTag, "Error setting event callback for gyroscope sensor");
139 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gyroscope");
143 internal override void EventListenStop()
145 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
146 if (error != (int)SensorError.None)
148 Log.Error(Globals.LogTag, "Error unsetting event callback for gyroscope sensor");
149 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gyroscope");