1 // Copyright 2016 by Samsung Electronics, Inc.,
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.
11 namespace Tizen.Sensor
14 /// Gyroscope Sensor Class. Used for registering callbacks for gyroscope and getting gyroscope data
16 public class Gyroscope : Sensor
18 private const string GyroscopeKey = "http://tizen.org/feature/sensor.gyroscope";
21 /// Gets the X component of the acceleration.
23 public float X { get; private set; }
26 /// Gets the Y component of the acceleration.
28 public float Y { get; private set; }
31 /// Gets the Z component of the acceleration.
33 public float Z { get; private set; }
36 /// Returns true or false based on whether gyroscope sensor is supported by device.
38 public static bool IsSupported
42 Log.Info(Globals.LogTag, "Checking if the Gyroscope sensor is supported");
43 return CheckIfSupported(SensorType.Gyroscope, GyroscopeKey);
48 /// Returns the number of gyroscope sensors available on the device.
50 public static int Count
54 Log.Info(Globals.LogTag, "Getting the count of gyroscope sensors");
60 /// Initializes a new instance of the <see cref="Tizen.Sensor.Gyroscope"/> class.
62 /// <param name='index'>
63 /// Index. Default value for this is 0. Index refers to a particular gyroscope sensor in case of multiple sensors
65 public Gyroscope(int index = 0) : base(index)
67 Log.Info(Globals.LogTag, "Creating Gyroscope object");
70 internal override SensorType GetSensorType()
72 return SensorType.Gyroscope;
76 /// Event Handler for storing the callback functions for event corresponding to change in gyroscope sensor data.
79 public event EventHandler<GyroscopeDataUpdatedEventArgs> DataUpdated;
81 private static int GetCount()
85 int error = Interop.SensorManager.GetSensorList(SensorType.Gyroscope, out list, out count);
86 if (error != (int)SensorError.None)
88 Log.Error(Globals.LogTag, "Error getting sensor list for gyroscope");
92 Interop.Libc.Free(list);
96 protected override void EventListenStart()
98 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
99 if (error != (int)SensorError.None)
101 Log.Error(Globals.LogTag, "Error setting event callback for gyroscope sensor");
102 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gyroscope");
106 protected override void EventListenStop()
108 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
109 if (error != (int)SensorError.None)
111 Log.Error(Globals.LogTag, "Error unsetting event callback for gyroscope sensor");
112 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gyroscope");
116 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
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];
124 DataUpdated?.Invoke(this, new GyroscopeDataUpdatedEventArgs(sensorData.values));