Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.System / Device / Led.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18
19 using Tizen.Common;
20
21 namespace Tizen.System
22 {
23     /// <summary>
24     /// The LED class provides the properties and methods to control the attached LED device.
25     /// </summary>
26     /// <remarks>
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.
28     /// Related features:
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.
33     /// </remarks>
34     /// <privilege>
35     ///  http://tizen.org/privilege/led
36     /// </privilege>
37     /// <code>
38     ///     Console.WriteLine("Led MaxBrightness is: {0}", Tizen.System.Led.MaxBrightness);
39     ///     Console.WriteLine("Led current Brightness is: {0}", Tizen.System.Led.Brightness);
40     /// </code>
41     public static class Led
42     {
43         /// <summary>
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>
49         /// </summary>
50         public static int MaxBrightness
51         {
52             get
53             {
54                 int max = 0;
55                 DeviceError res = (DeviceError)Interop.Device.DeviceFlashGetMaxBrightness(out max);
56                 if (res != DeviceError.None)
57                 {
58                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get max brightness value.");
59                 }
60                 return max;
61             }
62         }
63
64         private static readonly object s_lock = new object();
65
66         /// <summary>
67         /// Gets the brightness value of the LED that is located next to the camera.
68         /// </summary>
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).
71         /// </remarks>
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>
76         /// <code>
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);
80         /// </code>
81
82         public static int Brightness
83         {
84             get
85             {
86                 int brightness = 0;
87                 DeviceError res = (DeviceError)Interop.Device.DeviceFlashGetBrightness(out brightness);
88                 if (res != DeviceError.None)
89                 {
90                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get brightness value.");
91                 }
92                 return brightness;
93             }
94             set
95             {
96                 DeviceError res = (DeviceError) Interop.Device.DeviceFlashSetBrightness(value);
97                 if (res != DeviceError.None)
98                 {
99                     throw DeviceExceptionFactory.CreateException(res, "unable to set brightness value");
100                 }
101             }
102         }
103
104         /// <summary>
105         /// Plays the LED that is located at the front of the device.
106         /// </summary>
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">
111         /// The Color value
112         /// The first byte means opaque and the other 3 bytes are the RGB values.
113         /// </param>
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>
118         /// <code>
119         ///     try
120         ///     {
121         ///         Led.Play(500, 200, Color.FromRgba(255, 255, 255, 1));
122         ///     }
123         ///     Catch(Exception e)
124         ///     {
125         ///     }
126         /// </code>
127         public static void Play(int on, int off, Color color)
128         {
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)
132             {
133                 throw DeviceExceptionFactory.CreateException(res, "failed to play Led.");
134             }
135         }
136
137         /// <summary>
138         /// Stops the LED that is located at the front of the device.
139         /// </summary>
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>
144         /// <code>
145         ///     try
146         ///     {
147         ///         Led.Play(500, 200, Color.FromRgba(255, 255, 255, 1));
148         ///         //wait for a while and stop...
149         ///         Led.Stop();
150         ///     }
151         ///     Catch(Exception e)
152         ///     {
153         ///     }
154         /// </code>
155
156         public static void Stop()
157         {
158             DeviceError res = (DeviceError)Interop.Device.DeviceLedStopCustom();
159             if (res != DeviceError.None)
160             {
161                 throw DeviceExceptionFactory.CreateException(res, "failed to stop Led.");
162             }
163         }
164
165
166         private static EventHandler<LedBrightnessChangedEventArgs> s_brightnessChanged;
167         /// <summary>
168         /// StateChanged is raised when the LED state is changed.
169         /// </summary>
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
174         {
175             add
176             {
177                 lock (s_lock)
178                 {
179                     if (s_brightnessChanged == null)
180                     {
181                         EventListenerStart();
182                     }
183                     s_brightnessChanged += value;
184                 }
185             }
186             remove
187             {
188                 lock (s_lock)
189                 {
190                     s_brightnessChanged -= value;
191                     if (s_brightnessChanged == null)
192                     {
193                         EventListenerStop();
194                     }
195                 }
196             }
197         }
198
199         private static Interop.Device.deviceCallback s_handler;
200         private static void EventListenerStart()
201         {
202             s_handler = (int type, IntPtr value, IntPtr data) =>
203             {
204                 int val = value.ToInt32();
205                 LedBrightnessChangedEventArgs e = new LedBrightnessChangedEventArgs()
206                 {
207                     Brightness = val
208                 };
209                 s_brightnessChanged?.Invoke(null, e);
210                 return true;
211             };
212             Interop.Device.DeviceAddCallback(EventType.FlashBrightness, s_handler, IntPtr.Zero);
213         }
214
215         private static void EventListenerStop()
216         {
217             Interop.Device.DeviceRemoveCallback(EventType.FlashBrightness, s_handler);
218         }
219     }
220 }