[Sensor] Add new batched type sensor (#1522)
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / GravitySensor.cs
index c2a38c0..f3e4f23 100755 (executable)
@@ -19,8 +19,9 @@ using System;
 namespace Tizen.Sensor
 {
     /// <summary>
-    /// GravitySensor Class. Used for registering callbacks for gravity sensor and getting gravity data
+    /// The GravitySensor class is used for registering callbacks for the gravity sensor and getting the gravity data.
     /// </summary>
+    /// <since_tizen> 3 </since_tizen>
     public sealed class GravitySensor : Sensor
     {
         private const string GravitySensorKey = "http://tizen.org/feature/sensor.gravity";
@@ -48,10 +49,10 @@ namespace Tizen.Sensor
         public float Z { get; private set; } = float.MinValue;
 
         /// <summary>
-        /// Returns true or false based on whether gravity sensor is supported by device.
+        /// Returns true or false based on whether the gravity sensor is supported by the device.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
-        /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
+        /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
         public static bool IsSupported
         {
             get
@@ -65,7 +66,7 @@ namespace Tizen.Sensor
         /// Returns the number of gravity sensors available on the device.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
-        /// <value> The count of gravity sensors </value>
+        /// <value> The count of gravity sensors. </value>
         public static int Count
         {
             get
@@ -80,11 +81,11 @@ namespace Tizen.Sensor
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
         /// <feature>http://tizen.org/feature/sensor.gravity</feature>
-        /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
-        /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
-        /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
+        /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
+        /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
+        /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
         /// <param name='index'>
-        /// Index. Default value for this is 0. Index refers to a particular gravity sensor in case of multiple sensors
+        /// Index. Default value for this is 0. Index refers to a particular gravity sensor in case of multiple sensors.
         /// </param>
         public GravitySensor (uint index = 0) : base(index)
         {
@@ -97,14 +98,14 @@ namespace Tizen.Sensor
         }
 
         /// <summary>
-        /// Event Handler for storing the callback functions for event corresponding to change in gravity sensor data.
+        /// An event handler for storing the callback functions for the event corresponding to the change in the gravity sensor data.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
 
         public event EventHandler<GravitySensorDataUpdatedEventArgs> DataUpdated;
 
         /// <summary>
-        /// Event handler for accuracy changed events.
+        /// An event handler for accuracy changed events.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
         public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
@@ -142,14 +143,34 @@ namespace Tizen.Sensor
             return count;
         }
 
-        private static Interop.SensorListener.SensorEventCallback _callback;
+        /// <summary>
+        /// Read gravity sensor data synchronously.
+        /// </summary>
+        internal override void ReadData()
+        {
+            Interop.SensorEventStruct sensorData;
+            int error = Interop.SensorListener.ReadData(ListenerHandle, out sensorData);
+            if (error != (int)SensorError.None)
+            {
+                Log.Error(Globals.LogTag, "Error reading gravity sensor data");
+                throw SensorErrorFactory.CheckAndThrowException(error, "Reading gravity sensor data failed");
+            }
+
+            Timestamp = sensorData.timestamp;
+            X = sensorData.values[0];
+            Y = sensorData.values[1];
+            Z = sensorData.values[2];
+        }
+
+        private static Interop.SensorListener.SensorEventsCallback _callback;
 
         internal override void EventListenStart()
         {
-            _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
-                Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
+            _callback = (IntPtr sensorHandle, IntPtr eventPtr, uint events_count, IntPtr data) => {
+                updateBatchEvents(eventPtr, events_count);
+                Interop.SensorEventStruct sensorData = latestEvent();
 
-                TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
+                Timestamp = sensorData.timestamp;
                 X = sensorData.values[0];
                 Y = sensorData.values[1];
                 Z = sensorData.values[2];
@@ -157,7 +178,7 @@ namespace Tizen.Sensor
                 DataUpdated?.Invoke(this, new GravitySensorDataUpdatedEventArgs(sensorData.values));
             };
 
-            int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
+            int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
             if (error != (int)SensorError.None)
             {
                 Log.Error(Globals.LogTag, "Error setting event callback for gravity sensor");
@@ -167,7 +188,7 @@ namespace Tizen.Sensor
 
         internal override void EventListenStop()
         {
-            int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
+            int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
             if (error != (int)SensorError.None)
             {
                 Log.Error(Globals.LogTag, "Error unsetting event callback for gravity sensor");
@@ -180,8 +201,8 @@ namespace Tizen.Sensor
         private void AccuracyListenStart()
         {
             _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
-                TimeSpan = new TimeSpan((Int64)timestamp);
-                _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
+                Timestamp = timestamp;
+                _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(timestamp, accuracy));
             };
 
             int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);