Remove Tizen.UI dependency
[platform/core/csapi/tizenfx.git] / src / Tizen.System / Device / Led.cs
1 using System;
2
3 using Tizen.Common;
4
5 namespace Tizen.System
6 {
7     /// <summary>
8     /// The Led class provides properties and methods to control the attached led device.
9     /// </summary>
10     /// <remarks>
11     /// 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 to the front of a device.
12     /// Related Features
13     ///    http://tizen.org/feature/led
14     ///    http://tizen.org/feature/camera.back.flash
15     /// It is recommended to design feature related codes in your application for reliability.
16     /// You can check if a device supports the related features for this API by using System Information, thereby controlling the procedure of your application.
17     /// </remarks>
18     /// <privilege>
19     ///  http://tizen.org/privilege/led
20     /// </privilege>
21     /// <code>
22     ///     Console.WriteLine("Led MaxBrightness is: {0}", Tizen.System.Led.MaxBrightness);
23     ///     Console.WriteLine("Led current Brightness is: {0}", Tizen.System.Led.Brightness);
24     /// </code>
25     public static class Led
26     {
27         /// <summary>
28         /// Gets the max brightness value of a LED that is located next to the camera.
29         /// </summary>
30         public static int MaxBrightness
31         {
32             get
33             {
34                 int max = 0;
35                 DeviceError res = (DeviceError)Interop.Device.DeviceFlashGetMaxBrightness(out max);
36                 if (res != DeviceError.None)
37                 {
38                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get max brightness value.");
39                 }
40                 return max;
41             }
42         }
43
44         private static readonly object s_lock = new object();
45
46         /// <summary>
47         /// Gets the brightness value of a LED that is located next to the camera.
48         /// </summary>
49         /// <remarks>The brightness value range of LED is 0 to Tizen.System.Led.MaxBrightness value.
50         /// Changing the brightness value will invoke the registered EventHandler for led BrightnessChanged (If any).
51         /// </remarks>
52         /// <exception cref="ArgumentException"> When the invalid parameter value is set.</exception>
53         /// <exception cref = "UnauthorizedAccessException"> If the privilege is not set.</exception>
54         /// <code>
55         ///     Console.WriteLine("Led current Brightness is: {0}", Tizen.System.Led.Brightness);
56         ///     Tizen.System.Led.Brightness = 50;
57         ///     Console.WriteLine("Led current Brightness is: {0}", Tizen.System.Led.Brightness);
58         /// </code>
59
60         public static int Brightness
61         {
62             get
63             {
64                 int brightness = 0;
65                 DeviceError res = (DeviceError)Interop.Device.DeviceFlashGetBrightness(out brightness);
66                 if (res != DeviceError.None)
67                 {
68                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get brightness value.");
69                 }
70                 return brightness;
71             }
72             set
73             {
74                 DeviceError res = (DeviceError) Interop.Device.DeviceFlashSetBrightness(value);
75                 if (res != DeviceError.None)
76                 {
77                     throw DeviceExceptionFactory.CreateException(res, "unable to set brightness value");
78                 }
79             }
80         }
81
82         /// <summary>
83         /// Plays the LED that is located to the front of a device.
84         /// </summary>
85         /// <param name="on">Turn on time in milliseconds </param>
86         /// <param name="off">Turn off time in milliseconds </param>
87         /// <param name="color">
88         /// The Color value
89         /// The first byte means opaque and the other 3 bytes are RGB values.
90         /// </param>
91         /// <exception cref="ArgumentException"> When the invalid parameter value is set.</exception>
92         /// <exception cref = "UnauthorizedAccessException"> If the privilege is not set.</exception>
93         /// <exception cref = "InvalidOperationException"> In case of any system error.</exception>
94         /// <exception cref = "NotSupportedException"> In case of device does not support this behavior.</exception>
95         /// <code>
96         ///     try
97         ///     {
98         ///         Led.Play(500, 200, Color.FromRgba(255, 255, 255, 1));
99         ///     }
100         ///     Catch(Exception e)
101         ///     {
102         ///     }
103         /// </code>
104         public static void Play(int on, int off, Color color)
105         {
106             //looks like only blink option is supported. So hard coded to default blink option.
107             DeviceError res = (DeviceError)Interop.Device.DeviceLedPlayCustom(on, off, Convert.ToUInt32(color.GetArgb()), 1);
108             if (res != DeviceError.None)
109             {
110                 throw DeviceExceptionFactory.CreateException(res, "failed to play Led.");
111             }
112         }
113
114         /// <summary>
115         /// Stops the LED that is located to the front of a device.
116         /// </summary>
117         /// <exception cref = "UnauthorizedAccessException"> If the privilege is not set.</exception>
118         /// <exception cref = "InvalidOperationException"> In case of any system error.</exception>
119         /// <exception cref = "NotSupportedException"> In case of device does not support this behavior.</exception>
120         /// <code>
121         ///     try
122         ///     {
123         ///         Led.Play(500, 200, Color.FromRgba(255, 255, 255, 1));
124         ///         //wait for a while and stop...
125         ///         Led.Stop();
126         ///     }
127         ///     Catch(Exception e)
128         ///     {
129         ///     }
130         /// </code>
131
132         public static void Stop()
133         {
134             DeviceError res = (DeviceError)Interop.Device.DeviceLedStopCustom();
135             if (res != DeviceError.None)
136             {
137                 throw DeviceExceptionFactory.CreateException(res, "failed to stop Led.");
138             }
139         }
140
141
142         private static EventHandler<LedBrightnessChangedEventArgs> s_brightnessChanged;
143         /// <summary>
144         /// StateChanged is raised when the LED state is changed
145         /// </summary>
146         /// <param name="sender">The source of the event.</param>
147         /// <param name="e">An LedBrightnessChangedEventArgs object that contains the changed brightness.</param>
148         public static event EventHandler<LedBrightnessChangedEventArgs> BrightnessChanged
149         {
150             add
151             {
152                 lock (s_lock)
153                 {
154                     if (s_brightnessChanged == null)
155                     {
156                         EventListenerStart();
157                     }
158                     s_brightnessChanged += value;
159                 }
160             }
161             remove
162             {
163                 lock (s_lock)
164                 {
165                     s_brightnessChanged -= value;
166                     if (s_brightnessChanged == null)
167                     {
168                         EventListenerStop();
169                     }
170                 }
171             }
172         }
173
174         private static Interop.Device.deviceCallback s_handler;
175         private static void EventListenerStart()
176         {
177             s_handler = (int type, IntPtr value, IntPtr data) =>
178             {
179                 int val = value.ToInt32();
180                 LedBrightnessChangedEventArgs e = new LedBrightnessChangedEventArgs()
181                 {
182                     Brightness = val
183                 };
184                 s_brightnessChanged?.Invoke(null, e);
185                 return true;
186             };
187             Interop.Device.DeviceAddCallback(EventType.FlashBrightness, s_handler, IntPtr.Zero);
188         }
189
190         private static void EventListenerStop()
191         {
192             Interop.Device.DeviceRemoveCallback(EventType.FlashBrightness, s_handler);
193         }
194     }
195 }