Skip to content

Commit e2e652e

Browse files
authored
Improve Controller to have OpenGpio methods returning a GpioPin (#3)
1 parent 98a3c8e commit e2e652e

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

System.Device.Gpio/GpioController.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,54 @@ public extern int PinCount
5757
/// Opens a pin in order for it to be ready to use.
5858
/// </summary>
5959
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
60+
/// <returns>The opened GPIO pin.</returns>
6061
/// <exception cref="InvalidOperationException">This exception will be thrown if the pin is already open.</exception>
61-
public void OpenPin(int pinNumber)
62+
public GpioPin OpenPin(int pinNumber)
6263
{
63-
var pin = new Gpio​Pin(pinNumber);
64+
var gpioPin = InternalOpenPin(pinNumber);
6465

65-
if (pin.Init())
66-
{
67-
// add to array
68-
s_GpioPins.Add(new GpioPinBundle() { PinNumber = pinNumber, GpioPin = pin });
66+
// add to array
67+
s_GpioPins.Add(new GpioPinBundle() { PinNumber = pinNumber, GpioPin = gpioPin });
6968

70-
// done here
71-
return;
72-
}
73-
74-
throw new InvalidOperationException();
69+
return gpioPin;
7570
}
7671

7772
/// <summary>
7873
/// Opens a pin and sets it to a specific mode.
7974
/// </summary>
8075
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
8176
/// <param name="mode">The mode to be set.</param>
82-
public void OpenPin(
77+
/// <returns>The opened GPIO pin.</returns>
78+
public GpioPin OpenPin(
8379
int pinNumber,
8480
PinMode mode)
8581
{
86-
OpenPin(pinNumber);
82+
var gpioPin = InternalOpenPin(pinNumber);
83+
8784
SetPinMode(pinNumber, mode);
85+
86+
// add to array
87+
s_GpioPins.Add(new GpioPinBundle() { PinNumber = pinNumber, GpioPin = gpioPin });
88+
89+
return gpioPin;
90+
}
91+
92+
/// <summary>
93+
/// Opens a pin in order for it to be ready to use.
94+
/// </summary>
95+
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
96+
/// <exception cref="InvalidOperationException">This exception will be thrown if the pin is already open.</exception>
97+
private GpioPin InternalOpenPin(int pinNumber)
98+
{
99+
var pin = new Gpio​Pin(pinNumber);
100+
101+
if (pin.Init())
102+
{
103+
// done here
104+
return pin;
105+
}
106+
107+
throw new InvalidOperationException();
88108
}
89109

90110
/// <summary>

System.Device.Gpio/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
////////////////////////////////////////////////////////////////
1414
// update this whenever the native assembly signature changes //
15-
[assembly: AssemblyNativeVersion("100.1.0.0")]
15+
[assembly: AssemblyNativeVersion("100.1.0.1")]
1616
////////////////////////////////////////////////////////////////
1717

1818
// Setting ComVisible to false makes the types in this assembly not visible

System.Device.Gpio/System.Device.Gpio.nfproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
99
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
1010
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11-
<ProjectGuid>80b5c789-f9fc-43bf-9595-e8855af83219</ProjectGuid>
11+
<ProjectGuid>{80b5c789-f9fc-43bf-9595-e8855af83219}</ProjectGuid>
1212
<OutputType>Library</OutputType>
1313
<FileAlignment>512</FileAlignment>
1414
<RootNamespace>
@@ -128,4 +128,4 @@
128128
</PropertyGroup>
129129
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets'))" />
130130
</Target>
131-
</Project>
131+
</Project>

0 commit comments

Comments
 (0)