forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PinVector32.cs
75 lines (67 loc) · 2.32 KB
/
PinVector32.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
// 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.Pcx857x
{
/// <summary>
/// 32-bit vector of pins and values.
/// </summary>
internal struct PinVector32
{
/// <summary>
/// Bit vector of pin numbers from 0 (bit 0) to 31 (bit 31).
/// </summary>
public uint Pins { get; set; }
/// <summary>
/// Bit vector of values for each pin number from 0 (bit 0) to 31 (bit 31).
/// 1 is high, 0 is low.
/// </summary>
public uint Values { get; set; }
/// <summary>
/// Construct from a vector of pins and values.
/// </summary>
/// <param name="pins">Bit vector of pin numbers from 0 (bit 0) to 31 (bit 31).</param>
/// <param name="values">Bit vector of values for each pin number from 0 (bit 0) to 31 (bit 31).</param>
public PinVector32(uint pins, uint values)
{
Pins = pins;
Values = values;
}
/// <summary>
/// Construct from an array of <see cref="PinValuePair"/>s.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if <paramref name="pinValues"/> contains negative pin numbers or pin numbers higher than 31.
/// </exception>
public PinVector32(PinValuePair[] pinValues)
{
Pins = 0;
Values = 0;
for (var i = 0; i < pinValues.Length; i++)
{
var p = pinValues[i];
var pin = p.PinNumber;
var value = p.PinValue;
if (pin < 0 || pin >= sizeof(uint) * 8)
{
throw new ArgumentOutOfRangeException(nameof(pinValues));
}
uint bit = (uint)(1 << pin);
Pins |= bit;
if (value == PinValue.High)
{
Values |= bit;
}
}
}
/// <summary>
/// Convenience deconstructor. Allows using as a "return tuple".
/// </summary>
public void Deconstruct(out uint pins, out uint values)
{
pins = Pins;
values = Values;
}
}
}