1 //
2 // Copyright (c) 2010-2020 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using Antmicro.Renode.Core;
10 using Antmicro.Renode.Logging;
11 using Antmicro.Migrant;
12 
13 namespace Antmicro.Renode.Peripherals.Miscellaneous
14 {
15     public class LED : IGPIOReceiver, ILed
16     {
LED(bool invert = false)17         public LED(bool invert = false)
18         {
19             inverted = invert;
20             state = invert;
21             sync = new object();
22         }
23 
OnGPIO(int number, bool value)24         public void OnGPIO(int number, bool value)
25         {
26             if(number != 0)
27             {
28                 throw new ArgumentOutOfRangeException();
29             }
30 
31             State = inverted ? !value : value;
32         }
33 
Reset()34         public void Reset()
35         {
36             state = inverted;
37         }
38 
39         [field: Transient]
40         public event Action<ILed, bool> StateChanged;
41 
42         public bool State
43         {
44             get => state;
45 
46             private set
47             {
48                 lock(sync)
49                 {
50                     if(value == state)
51                     {
52                         return;
53                     }
54 
55                     state = value;
56                     StateChanged?.Invoke(this, state);
57                     this.Log(LogLevel.Noisy, "LED state changed to {0}", state);
58                 }
59             }
60         }
61 
62         private bool state;
63 
64         private readonly bool inverted;
65         private readonly object sync;
66     }
67 }
68 
69