Skip to content

Commit

Permalink
Improve Controller to have OpenGpio methods returning a GpioPin (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Oct 15, 2020
1 parent 98a3c8e commit e2e652e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
46 changes: 33 additions & 13 deletions System.Device.Gpio/GpioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,54 @@ public extern int PinCount
/// Opens a pin in order for it to be ready to use.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <returns>The opened GPIO pin.</returns>
/// <exception cref="InvalidOperationException">This exception will be thrown if the pin is already open.</exception>
public void OpenPin(int pinNumber)
public GpioPin OpenPin(int pinNumber)
{
var pin = new Gpio​Pin(pinNumber);
var gpioPin = InternalOpenPin(pinNumber);

if (pin.Init())
{
// add to array
s_GpioPins.Add(new GpioPinBundle() { PinNumber = pinNumber, GpioPin = pin });
// add to array
s_GpioPins.Add(new GpioPinBundle() { PinNumber = pinNumber, GpioPin = gpioPin });

// done here
return;
}

throw new InvalidOperationException();
return gpioPin;
}

/// <summary>
/// Opens a pin and sets it to a specific mode.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="mode">The mode to be set.</param>
public void OpenPin(
/// <returns>The opened GPIO pin.</returns>
public GpioPin OpenPin(
int pinNumber,
PinMode mode)
{
OpenPin(pinNumber);
var gpioPin = InternalOpenPin(pinNumber);

SetPinMode(pinNumber, mode);

// add to array
s_GpioPins.Add(new GpioPinBundle() { PinNumber = pinNumber, GpioPin = gpioPin });

return gpioPin;
}

/// <summary>
/// Opens a pin in order for it to be ready to use.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <exception cref="InvalidOperationException">This exception will be thrown if the pin is already open.</exception>
private GpioPin InternalOpenPin(int pinNumber)
{
var pin = new Gpio​Pin(pinNumber);

if (pin.Init())
{
// done here
return pin;
}

throw new InvalidOperationException();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion System.Device.Gpio/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

////////////////////////////////////////////////////////////////
// update this whenever the native assembly signature changes //
[assembly: AssemblyNativeVersion("100.1.0.0")]
[assembly: AssemblyNativeVersion("100.1.0.1")]
////////////////////////////////////////////////////////////////

// Setting ComVisible to false makes the types in this assembly not visible
Expand Down
4 changes: 2 additions & 2 deletions System.Device.Gpio/System.Device.Gpio.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>80b5c789-f9fc-43bf-9595-e8855af83219</ProjectGuid>
<ProjectGuid>{80b5c789-f9fc-43bf-9595-e8855af83219}</ProjectGuid>
<OutputType>Library</OutputType>
<FileAlignment>512</FileAlignment>
<RootNamespace>
Expand Down Expand Up @@ -128,4 +128,4 @@
</PropertyGroup>
<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'))" />
</Target>
</Project>
</Project>

0 comments on commit e2e652e

Please sign in to comment.