-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathLed.cs
82 lines (72 loc) · 2.25 KB
/
Led.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
// 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.Device.Gpio;
namespace Iot.Device.ExplorerHat
{
/// <summary>
/// Represents a led light
/// </summary>
public class Led : IDisposable
{
private GpioController _controller;
/// <summary>
/// GPIO pin to which led is attached
/// </summary>
public int Pin { get; private set; }
/// <summary>
/// Gets if led is switched on or not
/// </summary>
/// <value></value>
public bool IsOn { get; private set; }
/// <summary>
/// Initializes a <see cref="Led"/> instance
/// </summary>
/// <param name="pin">Underlying rpi GPIO pin number</param>
/// <param name="controller"><see cref="GpioController"/> used by <see cref="Led"/> to manage GPIO resources</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
internal Led(int pin, GpioController? controller = null, bool shouldDispose = true)
{
_controller = controller ?? new();
_shouldDispose = shouldDispose || controller is null;
Pin = pin;
IsOn = false;
_controller.OpenPin(Pin, PinMode.Output);
}
/// <summary>
/// Switch on this led light
/// </summary>
public void On()
{
if (!IsOn && _controller is object)
{
_controller.Write(Pin, PinValue.High);
IsOn = true;
}
}
/// <summary>
/// Switch off this led light
/// </summary>
public void Off()
{
if (IsOn && _controller is object)
{
_controller.Write(Pin, PinValue.Low);
IsOn = false;
}
}
private bool _shouldDispose;
/// <summary>
/// Disposes the <see cref="Led"/> instance
/// </summary>
public void Dispose()
{
Off();
if (_shouldDispose)
{
_controller?.Dispose();
_controller = null!;
}
}
}
}