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 LightSensor class is used for registering callbacks for the light sensor and getting the light data.
24 public sealed class LightSensor : Sensor
26 private const string LightSensorKey = "http://tizen.org/feature/sensor.photometer";
29 /// Gets the level of the light.
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> The light level. </value>
33 public float Level { get; private set; } = float.MinValue;
36 /// Returns true or false based on whether the light sensor is supported by the device.
38 /// <since_tizen> 3 </since_tizen>
39 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
40 public static bool IsSupported
44 Log.Info(Globals.LogTag, "Checking if the LightSensor is supported");
45 return CheckIfSupported(SensorType.LightSensor, LightSensorKey);
50 /// Returns the number of light sensors available on the device.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value> The count of light sensors. </value>
54 public static int Count
58 Log.Info(Globals.LogTag, "Getting the count of light sensors");
64 /// Initializes a new instance of the <see cref="Tizen.Sensor.LightSensor"/> class.
66 /// <since_tizen> 3 </since_tizen>
67 /// <feature>http://tizen.org/feature/sensor.photometer</feature>
68 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
69 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
70 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
71 /// <param name='index'>
72 /// Index. Default value for this is 0. Index refers to a particular light sensor in case of multiple sensors.
74 public LightSensor(uint index = 0) : base(index)
76 Log.Info(Globals.LogTag, "Creating LightSensor object");
79 internal override SensorType GetSensorType()
81 return SensorType.LightSensor;
85 /// An event handler for storing the callback functions for the event corresponding to the change in the light sensor data.
87 /// <since_tizen> 3 </since_tizen>
89 public event EventHandler<LightSensorDataUpdatedEventArgs> DataUpdated;
91 private static int GetCount()
95 int error = Interop.SensorManager.GetSensorList(SensorType.LightSensor, out list, out count);
96 if (error != (int)SensorError.None)
98 Log.Error(Globals.LogTag, "Error getting sensor list for light");
102 Interop.Libc.Free(list);
106 private static Interop.SensorListener.SensorEventCallback _callback;
108 internal override void EventListenStart()
110 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
111 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
113 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
114 Level = sensorData.values[0];
116 DataUpdated?.Invoke(this, new LightSensorDataUpdatedEventArgs(sensorData.values[0]));
119 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
120 if (error != (int)SensorError.None)
122 Log.Error(Globals.LogTag, "Error setting event callback for light sensor");
123 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for light");
127 internal override void EventListenStop()
129 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
130 if (error != (int)SensorError.None)
132 Log.Error(Globals.LogTag, "Error unsetting event callback for light sensor");
133 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for light");