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.
21 namespace Tizen.System
24 /// The LED class provides the properties and methods to control the attached LED device.
27 /// The LED API provides the way to control the attached LED device, such as the camera flash and service LED. It supports to turn on the camera flash and set the pattern to the service LED which is located at the front of the device.
29 /// http://tizen.org/feature/led
30 /// http://tizen.org/feature/camera.back.flash
31 /// It is recommended to design the feature related codes in your application for reliability.
32 /// You can check if a device supports the related features for this API by using system information, thereby controlling the procedure of your application.
35 /// http://tizen.org/privilege/led
38 /// Console.WriteLine("Led MaxBrightness is: {0}", Tizen.System.Led.MaxBrightness);
39 /// Console.WriteLine("Led current Brightness is: {0}", Tizen.System.Led.Brightness);
41 public static class Led
44 /// Gets the maximum brightness value of the LED that is located next to the camera.
45 /// <since_tizen> 3 </since_tizen>
46 /// <exception cref="ArgumentException">When an invalid parameter value is set.</exception>
47 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
48 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
50 public static int MaxBrightness
55 DeviceError res = (DeviceError)Interop.Device.DeviceFlashGetMaxBrightness(out max);
56 if (res != DeviceError.None)
58 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get max brightness value.");
64 private static readonly object s_lock = new object();
67 /// Gets the brightness value of the LED that is located next to the camera.
69 /// <remarks>The brightness value range of the LED is 0 to Tizen.System.Led.MaxBrightness value.
70 /// Changing the brightness value will invoke the registered EventHandler for the LED BrightnessChanged (if any).
72 /// <since_tizen> 3 </since_tizen>
73 /// <exception cref="ArgumentException"> When an invalid parameter value is set.</exception>
74 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
75 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
77 /// Console.WriteLine("Led current Brightness is: {0}", Tizen.System.Led.Brightness);
78 /// Tizen.System.Led.Brightness = 50;
79 /// Console.WriteLine("Led current Brightness is: {0}", Tizen.System.Led.Brightness);
82 public static int Brightness
87 DeviceError res = (DeviceError)Interop.Device.DeviceFlashGetBrightness(out brightness);
88 if (res != DeviceError.None)
90 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get brightness value.");
96 DeviceError res = (DeviceError) Interop.Device.DeviceFlashSetBrightness(value);
97 if (res != DeviceError.None)
99 throw DeviceExceptionFactory.CreateException(res, "unable to set brightness value");
105 /// Plays the LED that is located at the front of the device.
107 /// <since_tizen> 3 </since_tizen>
108 /// <param name="on">Turn on time in milliseconds.</param>
109 /// <param name="off">Turn off time in milliseconds.</param>
110 /// <param name="color">
112 /// The first byte means opaque and the other 3 bytes are the RGB values.
114 /// <exception cref="ArgumentException">When an invalid parameter value is set.</exception>
115 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
116 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
117 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
121 /// Led.Play(500, 200, Color.FromRgba(255, 255, 255, 1));
123 /// Catch(Exception e)
127 public static void Play(int on, int off, Color color)
129 //looks like only blink option is supported. So hard coded to default blink option.
130 DeviceError res = (DeviceError)Interop.Device.DeviceLedPlayCustom(on, off, Convert.ToUInt32(color.GetArgb()), 1);
131 if (res != DeviceError.None)
133 throw DeviceExceptionFactory.CreateException(res, "failed to play Led.");
138 /// Stops the LED that is located at the front of the device.
140 /// <since_tizen> 3 </since_tizen>
141 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
142 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
143 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
147 /// Led.Play(500, 200, Color.FromRgba(255, 255, 255, 1));
148 /// //wait for a while and stop...
151 /// Catch(Exception e)
156 public static void Stop()
158 DeviceError res = (DeviceError)Interop.Device.DeviceLedStopCustom();
159 if (res != DeviceError.None)
161 throw DeviceExceptionFactory.CreateException(res, "failed to stop Led.");
166 private static EventHandler<LedBrightnessChangedEventArgs> s_brightnessChanged;
168 /// StateChanged is raised when the LED state is changed.
170 /// <since_tizen> 3 </since_tizen>
171 /// <param name="sender">The source of the event.</param>
172 /// <param name="e">LedBrightnessChangedEventArgs is an object that contains the changed brightness.</param>
173 public static event EventHandler<LedBrightnessChangedEventArgs> BrightnessChanged
179 if (s_brightnessChanged == null)
181 EventListenerStart();
183 s_brightnessChanged += value;
190 s_brightnessChanged -= value;
191 if (s_brightnessChanged == null)
199 private static Interop.Device.deviceCallback s_handler;
200 private static void EventListenerStart()
202 s_handler = (int type, IntPtr value, IntPtr data) =>
204 int val = value.ToInt32();
205 LedBrightnessChangedEventArgs e = new LedBrightnessChangedEventArgs()
209 s_brightnessChanged?.Invoke(null, e);
212 Interop.Device.DeviceAddCallback(EventType.FlashBrightness, s_handler, IntPtr.Zero);
215 private static void EventListenerStop()
217 Interop.Device.DeviceRemoveCallback(EventType.FlashBrightness, s_handler);