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 /// The UncalibratedGyroscope sensor class is used for registering callbacks for the uncalibrated gyroscope and getting the uncalibrated gyroscope data.
24 /// <since_tizen> 3 </since_tizen>
25 public sealed class UncalibratedGyroscope : Sensor
27 private static string UncalibratedGyroscopeKey = "http://tizen.org/feature/sensor.gyroscope.uncalibrated";
30 /// Gets the X component of the acceleration.
32 /// <since_tizen> 3 </since_tizen>
33 /// <value> X </value>
34 public float X { get; private set; } = float.MinValue;
37 /// Gets the Y component of the acceleration.
39 /// <since_tizen> 3 </since_tizen>
40 /// <value> Y </value>
41 public float Y { get; private set; } = float.MinValue;
44 /// Gets the Z component of the acceleration.
46 /// <since_tizen> 3 </since_tizen>
47 /// <value> Z </value>
48 public float Z { get; private set; } = float.MinValue;
51 /// Gets the BiasX component of the uncalibrated gyroscope data.
53 /// <since_tizen> 3 </since_tizen>
54 /// <value> The X bias. </value>
55 public float BiasX { get; private set; } = 0;
58 /// Gets the BiasY component of the uncalibrated gyroscope data.
60 /// <since_tizen> 3 </since_tizen>
61 /// <value> The Y bias. </value>
62 public float BiasY { get; private set; } = 0;
65 /// Gets the BiasZ component of the uncalibrated gyroscope data.
67 /// <since_tizen> 3 </since_tizen>
68 /// <value> The Z bias. </value>
69 public float BiasZ { get; private set; } = 0;
72 /// Returns true or false based on whether the uncalibrated gyroscope sensor is supported by the device.
74 /// <since_tizen> 3 </since_tizen>
75 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
76 public static bool IsSupported
80 Log.Info(Globals.LogTag, "Checking if the UncalibratedGyroscope sensor is supported");
81 return CheckIfSupported(SensorType.UncalibratedGyroscope, UncalibratedGyroscopeKey);
86 /// Returns the number of the uncalibrated gyroscope sensors available on the device.
88 /// <since_tizen> 3 </since_tizen>
89 /// <value> The count of the uncalibrated gyroscope sensors. </value>
90 public static int Count
94 Log.Info(Globals.LogTag, "Getting the count of uncalibrated gyroscope sensors");
100 /// Initializes a new instance of the <see cref="Tizen.Sensor.UncalibratedGyroscope"/> class.
102 /// <since_tizen> 3 </since_tizen>
103 /// <feature>http://tizen.org/feature/sensor.gyroscope.uncalibrated</feature>
104 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
105 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
106 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
107 /// <param name='index'>
108 /// Index. Default value for this is 0. Index refers to a particular uncalibrated gyroscope sensor in case of multiple sensors.
110 public UncalibratedGyroscope(uint index = 0) : base(index)
112 Log.Info(Globals.LogTag, "Creating UncalibratedGyroscope object");
115 internal override SensorType GetSensorType()
117 return SensorType.UncalibratedGyroscope;
121 /// An event handler for storing the callback functions for the event corresponding to the change in the uncalibrated gyroscope sensor data.
123 /// <since_tizen> 3 </since_tizen>
125 public event EventHandler<UncalibratedGyroscopeDataUpdatedEventArgs> DataUpdated;
127 private static int GetCount()
131 int error = Interop.SensorManager.GetSensorList(SensorType.UncalibratedGyroscope, out list, out count);
132 if (error != (int)SensorError.None)
134 Log.Error(Globals.LogTag, "Error getting sensor list for uncalibrated gyroscope");
138 Interop.Libc.Free(list);
143 /// Read uncalibrated gyroscope data synchronously.
145 internal override void ReadData()
147 Interop.SensorEventStruct sensorData;
148 int error = Interop.SensorListener.ReadData(ListenerHandle, out sensorData);
149 if (error != (int)SensorError.None)
151 Log.Error(Globals.LogTag, "Error reading uncalibrated gyroscope data");
152 throw SensorErrorFactory.CheckAndThrowException(error, "Reading uncalibrated gyroscope data failed");
155 Timestamp = sensorData.timestamp;
156 X = sensorData.values[0];
157 Y = sensorData.values[1];
158 Z = sensorData.values[2];
159 BiasX = sensorData.values[3];
160 BiasY = sensorData.values[4];
161 BiasZ = sensorData.values[5];
164 private static Interop.SensorListener.SensorEventsCallback _callback;
166 internal override void EventListenStart()
168 _callback = (IntPtr sensorHandle, IntPtr eventPtr, uint events_count, IntPtr data) => {
169 updateBatchEvents(eventPtr, events_count);
170 Interop.SensorEventStruct sensorData = latestEvent();
172 Timestamp = sensorData.timestamp;
173 X = sensorData.values[0];
174 Y = sensorData.values[1];
175 Z = sensorData.values[2];
176 BiasX = sensorData.values[3];
177 BiasY = sensorData.values[4];
178 BiasZ = sensorData.values[5];
180 DataUpdated?.Invoke(this, new UncalibratedGyroscopeDataUpdatedEventArgs(sensorData.values));
183 int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
184 if (error != (int)SensorError.None)
186 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated gyroscope sensor");
187 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated gyroscope");
191 internal override void EventListenStop()
193 int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
194 if (error != (int)SensorError.None)
196 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated gyroscope sensor");
197 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated gyroscope");