-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathLights.cs
133 lines (113 loc) · 3.68 KB
/
Lights.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Device.Gpio;
namespace Iot.Device.ExplorerHat
{
/// <summary>
/// Represents the Explorer HAT led array
/// </summary>
public class Lights : IDisposable, IEnumerable<Led>
{
private const int LED1_PIN = 4;
private const int LED2_PIN = 17;
private const int LED3_PIN = 27;
private const int LED4_PIN = 5;
private GpioController _controller;
private List<Led> LedArray { get; set; }
/// <summary>
/// Blue led (#1)
/// </summary>
public Led One => LedArray[0];
/// <summary>
/// Yellow led (#2)
/// </summary>
public Led Two => LedArray[1];
/// <summary>
/// Red led (#3)
/// </summary>
public Led Three => LedArray[2];
/// <summary>
/// Green led (#4)
/// </summary>
public Led Four => LedArray[3];
/// <summary>
/// Blue led (#1)
/// </summary>
public Led Blue => LedArray[0];
/// <summary>
/// Yellow led (#2)
/// </summary>
public Led Yellow => LedArray[1];
/// <summary>
/// Red led (#3)
/// </summary>
public Led Red => LedArray[2];
/// <summary>
/// Green led (#4)
/// </summary>
public Led Green => LedArray[3];
/// <summary>
/// Initializes a <see cref="Lights"/> instance
/// </summary>
/// <param name="controller"><see cref="GpioController"/> used by <see cref="Lights"/> to manage GPIO resources</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
internal Lights(GpioController? controller = null, bool shouldDispose = true)
{
_controller = controller ?? new();
_shouldDispose = shouldDispose || controller is null;
LedArray = new List<Led>()
{
new(LED1_PIN, _controller),
new(LED2_PIN, _controller),
new(LED3_PIN, _controller),
new(LED4_PIN, _controller)
};
}
/// <summary>
/// Switch on all led lights
/// </summary>
public void On()
{
foreach (Led led in LedArray)
{
led.On();
}
}
/// <summary>
/// Switch off all led lights
/// </summary>
public void Off()
{
foreach (Led led in LedArray)
{
led.Off();
}
}
private bool _shouldDispose;
/// <summary>
/// Disposes the <see cref="Lights"/> instance
/// </summary>
public void Dispose()
{
Off();
if (_shouldDispose)
{
_controller?.Dispose();
_controller = null!;
}
}
/// <summary>
/// Returns an enumerator that iterates through the collection of leds
/// </summary>
/// <returns>An enumerator that can be used to iterate through the collection of leds</returns>
public IEnumerator<Led> GetEnumerator() => LedArray.GetEnumerator();
/// <summary>
/// Returns an enumerator that iterates through the collection of leds
/// </summary>
/// <returns>An enumerator that can be used to iterate through the collection of leds</returns>
IEnumerator IEnumerable.GetEnumerator() => LedArray.GetEnumerator();
}
}