forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GpioOutputSegment.cs
127 lines (114 loc) · 4.14 KB
/
GpioOutputSegment.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Device.Gpio;
using System.Threading;
using Iot.Device.Multiplexing.Utility;
namespace Iot.Device.Multiplexing
{
/// <summary>
/// IOutputSegment implementation that uses GpioController.
/// </summary>
public class GpioOutputSegment : IOutputSegment, IDisposable
{
private readonly int[] _pins;
private readonly bool _shouldDispose;
private readonly VirtualOutputSegment _segment;
private GpioController _controller;
/// <summary>
/// IOutputSegment implementation that uses GpioController.
/// </summary>
/// <param name="pins">The GPIO pins that should be used and are connected.</param>
/// <param name="gpioController">The GpioController to use. If one isn't provided, one will be created.</param>
/// <param name="shouldDispose">The policy to use (true, by default) for disposing the GPIO controller when disposing this instance.</param>
public GpioOutputSegment(int[] pins, GpioController? gpioController = null, bool shouldDispose = true)
{
_shouldDispose = shouldDispose || gpioController is null;
_controller = gpioController ?? new GpioController();
_pins = pins;
_segment = new VirtualOutputSegment(_pins.Length);
foreach (var pin in _pins)
{
_controller.OpenPin(pin, PinMode.Output);
}
}
/// <summary>
/// The length of the segment; the number of GPIO pins it exposes.
/// </summary>
public int Length => _segment.Length;
/// <summary>
/// Segment values.
/// </summary>
public PinValue this[int index]
{
get => _segment[index];
set => _segment[index] = value;
}
/// <summary>
/// Writes a PinValue to a virtual segment.
/// Does not display output.
/// </summary>
public void Write(int pin, PinValue value)
{
_segment.Write(pin, value);
}
/// <summary>
/// Writes discrete underlying bits to a virtual segment.
/// Writes each bit, left to right. Least significant bit will written to index 0.
/// Does not display output.
/// </summary>
public void Write(byte value)
{
_segment.Write(value);
}
/// <summary>
/// Writes discrete underlying bits to a virtual segment.
/// Writes each byte, left to right. Least significant bit will written to index 0.
/// Does not display output.
/// </summary>
public void Write(SpanByte value)
{
_segment.Write(value);
}
/// <summary>
/// Writes a byte to the underlying GpioController.
/// </summary>
public void TurnOffAll()
{
_segment.TurnOffAll();
Display();
}
/// <summary>
/// Displays current state of segment.
/// Segment is displayed at least until token receives a cancellation signal, possibly due to a specified duration expiring.
/// </summary>
public void Display(CancellationToken token)
{
Display();
_segment.Display(token);
}
/// <summary>
/// Displays current state of segment.
/// Segment is displayed at least until token receives a cancellation signal, possibly due to a specified duration expiring.
/// </summary>
private void Display()
{
for (int i = 0; i < _pins.Length; i++)
{
_controller.Write(_pins[i], _segment[i]);
}
}
/// <summary>
/// Disposes the underlying GpioController.
/// </summary>
public void Dispose()
{
if (_shouldDispose)
{
_controller?.Dispose();
_controller = null!;
}
}
}
}