Skip to content

Commit d6e49f2

Browse files
committed
A first draft. Looks neater than with ChatGPT. Obviously.
1 parent f45456e commit d6e49f2

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

src/devices/Board/RaspberryPiBoard.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.IO;
1414
using System.Linq;
1515
using System.Text;
16+
using Iot.Device.Gpio;
1617
using UnitsNet;
1718

1819
namespace Iot.Device.Board
@@ -925,5 +926,11 @@ public override ComponentInformation QueryComponentInformation()
925926
ret.Properties["PinCount"] = PinCount.ToString(CultureInfo.InvariantCulture);
926927
return ret;
927928
}
929+
930+
public static VirtualGpioController CreatePhysicalPinMapping(GpioController defaultController)
931+
{
932+
var map = 1;
933+
var pinMapping = new VirtualGpioController();
934+
}
928935
}
929936
}

src/devices/Board/VirtualGpioController.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ protected override void Dispose(bool disposing)
9393
_pins.Clear();
9494
}
9595

96-
/// <inheritdoc />
96+
/// <summary>
97+
/// This operation is not supported, as the virtual controller doesn't know what to do here.
98+
/// Use <see cref="VirtualGpioControllerWithDefault"/> for an alternative approach if you need to dynamically assign pins.
99+
/// </summary>
100+
/// <param name="pinNumber">The number of the pin</param>
101+
/// <exception cref="InvalidOperationException">Always. This operation is not supported</exception>
97102
protected override void OpenPinCore(int pinNumber)
98103
{
99104
// Not clear what this should do
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Device.Gpio;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
12+
13+
namespace Iot.Device.Gpio
14+
{
15+
/// <summary>
16+
/// A virtual GPIO controller that serves primarily for a pin mapping on an existing controller.
17+
/// Unlike <see cref="VirtualGpioController"/> it has a default controller rather than individual pins
18+
/// and thus also supports <see cref="GpioController.OpenPin(int)"/>.
19+
/// </summary>
20+
public class VirtualGpioControllerWithDefault : VirtualGpioController
21+
{
22+
private readonly GpioController _defaultController;
23+
private readonly Dictionary<int, int> _fromVirtualToRealMapping;
24+
25+
public VirtualGpioControllerWithDefault(GpioController defaultController, Dictionary<int, int> fromVirtualToRealMapping)
26+
{
27+
_defaultController = defaultController ?? throw new ArgumentNullException(nameof(defaultController));
28+
_fromVirtualToRealMapping = fromVirtualToRealMapping ?? throw new ArgumentNullException(nameof(fromVirtualToRealMapping));
29+
}
30+
31+
/// <summary>
32+
/// Opens a pin from the default controller
33+
/// </summary>
34+
/// <param name="pinNumber">The pin number</param>
35+
protected override void OpenPinCore(int pinNumber)
36+
{
37+
int realPinNumber = MapToRealPin(pinNumber);
38+
if (realPinNumber == -1)
39+
{
40+
throw new InvalidOperationException($"Virtual Pin Number {pinNumber} is unknown");
41+
}
42+
43+
var pin = _defaultController.OpenPin(realPinNumber);
44+
if (!Add(pinNumber, pin))
45+
{
46+
_defaultController.ClosePin(realPinNumber);
47+
throw new InvalidOperationException($"Virtual Pin Number {pinNumber} is in use already");
48+
}
49+
}
50+
51+
public int MapToRealPin(int virtualPin)
52+
{
53+
if (_fromVirtualToRealMapping.TryGetValue(virtualPin, out int realPin))
54+
{
55+
return realPin;
56+
}
57+
58+
return -1;
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)