Release 4.0.0-preview1-00194
[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         public static event EventHandler<LedBrightnessChangedEventArgs> BrightnessChanged
172         {
173             add
174             {
175                 lock (s_lock)
176                 {
177                     if (s_brightnessChanged == null)
178                     {
179                         EventListenerStart();
180                     }
181                     s_brightnessChanged += value;
182                 }
183             }
184             remove
185             {
186                 lock (s_lock)
187                 {
188                     s_brightnessChanged -= value;
189                     if (s_brightnessChanged == null)
190                     {
191                         EventListenerStop();
192                     }
193                 }
194             }
195         }
196
197         private static Interop.Device.deviceCallback s_handler;
198         private static void EventListenerStart()
199         {
200             s_handler = (int type, IntPtr value, IntPtr data) =>
201             {
202                 int val = value.ToInt32();
203                 LedBrightnessChangedEventArgs e = new LedBrightnessChangedEventArgs()
204                 {
205                     Brightness = val
206                 };
207                 s_brightnessChanged?.Invoke(null, e);
208                 return true;
209             };
210             Interop.Device.DeviceAddCallback(EventType.FlashBrightness, s_handler, IntPtr.Zero);
211         }
212
213         private static void EventListenerStop()
214         {
215             Interop.Device.DeviceRemoveCallback(EventType.FlashBrightness, s_handler);
216         }
217     }
218 }