[Device] Implementation of system.device module including error handling.
[platform/core/csapi/system.git] / Tizen.System / Device / Led.cs
1 using System;
2
3 namespace Tizen.System
4 {
5     /// <summary>
6     /// The Led class provides properties and methods to control the attached led device.
7     /// </summary>
8     public static class Led
9     {
10         /// <summary>
11         /// Gets the max brightness value of a LED that is located next to the camera.
12         /// </summary>
13         public static int MaxBrightness
14         {
15             get
16             {
17                 int max = 0;
18                 DeviceError res = (DeviceError) Interop.Device.DeviceFlashGetMaxBrightness(out max);
19                 if (res != DeviceError.None)
20                 {
21                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get max brightness value.");
22                 }
23                 return max;
24             }
25         }
26
27         private static readonly object s_lock = new object();
28
29         /// <summary>
30         /// Gets the brightness value of a LED that is located next to the camera.
31         /// </summary>
32         public static int Brightness
33         {
34             get
35             {
36                 int brightness = 0;
37                 DeviceError res = (DeviceError) Interop.Device.DeviceFlashGetBrightness(out brightness);
38                 if (res != DeviceError.None)
39                 {
40                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get brightness value.");
41                 }
42                 return brightness;
43             }
44             set
45             {
46                 Interop.Device.DeviceFlashSetBrightness(value);
47             }
48         }
49
50         /// <summary>
51         /// Plays the LED that is located to the front of a device.
52         /// </summary>
53         /// <param name="on">Turn on time in milliseconds </param>
54         /// <param name="off">Turn off time in milliseconds </param>
55         /// <param name="color">
56         /// The Color value
57         /// The first byte means opaque and the other 3 bytes are RGB values.
58         /// </param>
59         public static void Play(int on, int off, uint color)
60         {
61             //looks like only blink option is supported. So hard coded to default blink option.
62             DeviceError res = (DeviceError)Interop.Device.DeviceLedPlayCustom(on, off, color, 1);
63             if (res != DeviceError.None)
64             {
65                 throw DeviceExceptionFactory.CreateException(DeviceError.InvalidParameter, "failed to play Led.");
66             }
67         }
68
69         /// <summary>
70         /// Stops the LED that is located to the front of a device.
71         /// </summary>
72         public static void Stop()
73         {
74             DeviceError res = (DeviceError) Interop.Device.DeviceLedStopCustom();
75             if(res != DeviceError.None)
76             {
77                 throw DeviceExceptionFactory.CreateException(DeviceError.InvalidParameter, "failed to stop Led.");
78             }
79         }
80
81
82         private static EventHandler<LedBrightnessChangedEventArgs> s_brightnessChanged;
83         /// <summary>
84         /// StateChanged is raised when the LED state is changed
85         /// </summary>
86         /// <param name="sender">The source of the event.</param>
87         /// <param name="e">An LedBrightnessChangedEventArgs object that contains the changed brightness.</param>
88         public static event EventHandler<LedBrightnessChangedEventArgs> BrightnessChanged
89         {
90             add
91             {
92                 lock (s_lock)
93                 {
94                     if (s_brightnessChanged == null)
95                     {
96                         EventListenerStart();
97                     }
98                     s_brightnessChanged += value;
99                 }
100             }
101             remove
102             {
103                 lock (s_lock)
104                 {
105                     s_brightnessChanged -= value;
106                     if (s_brightnessChanged == null)
107                     {
108                         EventListenerStop();
109                     }
110                 }
111             }
112         }
113
114         private static Interop.Device.deviceCallback s_handler;
115         private static void EventListenerStart()
116         {
117             s_handler = (int type, IntPtr value, IntPtr data) =>
118             {
119                 int val = value.ToInt32();
120                 LedBrightnessChangedEventArgs e = new LedBrightnessChangedEventArgs()
121                 {
122                     Brightness = val
123                 };
124                 s_brightnessChanged?.Invoke(null, e);
125                 return true;
126             };
127             Interop.Device.DeviceAddCallback(EventType.FlashBrightness, s_handler, IntPtr.Zero);
128         }
129
130         private static void EventListenerStop()
131         {
132             Interop.Device.DeviceRemoveCallback(EventType.FlashBrightness, s_handler);
133         }
134     }
135 }