[Sensor] Add new batched type sensor (#1522)
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / GravitySensor.cs
old mode 100644 (file)
new mode 100755 (executable)
index e1d8eb5..f3e4f23
@@ -1,19 +1,28 @@
-// Copyright 2016 by Samsung Electronics, Inc.,
-//
-// This software is the confidential and proprietary information
-// of Samsung Electronics, Inc. ("Confidential Information"). You
-// shall not disclose such Confidential Information and shall use
-// it only in accordance with the terms of the license agreement
-// you entered into with Samsung.
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
 using System;
 
 namespace Tizen.Sensor
 {
     /// <summary>
-    /// GravitySensor Class. Used for registering callbacks for gravity sensor and getting gravity data
-    /// /// </summary>
-    public class GravitySensor : Sensor
+    /// 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";
 
@@ -21,21 +30,29 @@ namespace Tizen.Sensor
         /// <summary>
         /// Gets the X component of the gravity.
         /// </summary>
-        public float X { get; private set; }
+        /// <since_tizen> 3 </since_tizen>
+        /// <value> X </value>
+        public float X { get; private set; } = float.MinValue;
 
         /// <summary>
         /// Gets the Y component of the gravity.
         /// </summary>
-        public float Y { get; private set; }
+        /// <since_tizen> 3 </since_tizen>
+        /// <value> Y </value>
+        public float Y { get; private set; } = float.MinValue;
 
         /// <summary>
         /// Gets the Z component of the gravity.
         /// </summary>
-        public float Z { get; private set; }
+        /// <since_tizen> 3 </since_tizen>
+        /// <value> Z </value>
+        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>
         public static bool IsSupported
         {
             get
@@ -48,6 +65,8 @@ namespace Tizen.Sensor
         /// <summary>
         /// Returns the number of gravity sensors available on the device.
         /// </summary>
+        /// <since_tizen> 3 </since_tizen>
+        /// <value> The count of gravity sensors. </value>
         public static int Count
         {
             get
@@ -60,10 +79,15 @@ namespace Tizen.Sensor
         /// <summary>
         /// Initializes a new instance of the <see cref="Tizen.Sensor.GravitySensor"/> class.
         /// </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>
         /// <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 (int index = 0) : base(index)
+        public GravitySensor (uint index = 0) : base(index)
         {
             Log.Info(Globals.LogTag, "Creating GravitySensor object");
         }
@@ -74,14 +98,16 @@ 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
         {
             add
@@ -117,9 +143,42 @@ namespace Tizen.Sensor
             return count;
         }
 
-        protected override void EventListenStart()
+        /// <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()
         {
-            int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
+            _callback = (IntPtr sensorHandle, IntPtr eventPtr, uint events_count, IntPtr data) => {
+                updateBatchEvents(eventPtr, events_count);
+                Interop.SensorEventStruct sensorData = latestEvent();
+
+                Timestamp = sensorData.timestamp;
+                X = sensorData.values[0];
+                Y = sensorData.values[1];
+                Z = sensorData.values[2];
+
+                DataUpdated?.Invoke(this, new GravitySensorDataUpdatedEventArgs(sensorData.values));
+            };
+
+            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");
@@ -127,9 +186,9 @@ namespace Tizen.Sensor
             }
         }
 
-        protected override void EventListenStop()
+        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");
@@ -137,9 +196,16 @@ namespace Tizen.Sensor
             }
         }
 
+        private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
+
         private void AccuracyListenStart()
         {
-            int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, Interval, AccuracyEventCallback, IntPtr.Zero);
+            _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
+                Timestamp = timestamp;
+                _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(timestamp, accuracy));
+            };
+
+            int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
             if (error != (int)SensorError.None)
             {
                 Log.Error(Globals.LogTag, "Error setting accuracy event callback for gravity sensor");
@@ -156,22 +222,5 @@ namespace Tizen.Sensor
                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for gravity");
             }
         }
-
-        private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
-        {
-            Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
-            TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
-            X = sensorData.values[0];
-            Y = sensorData.values[1];
-            Z = sensorData.values[2];
-
-            DataUpdated?.Invoke(this, new GravitySensorDataUpdatedEventArgs(sensorData.values));
-        }
-
-        private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
-        {
-            TimeSpan = new TimeSpan((Int64)timestamp);
-            _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
-        }
     }
 }