Skip to content

[Proposal] Add ReadBool and WriteBool to GpioController #121

@shaggygi

Description

@shaggygi

This proposal is an addition to the GpioController class that include helper methods for working with the different pin values and their respective boolean value.

Rationale and Usage

There are many times you need to determine the bit value and must perform conversions of setting/clearing the bit. Pins sometimes represent bits within a value. The proposal would help from having to write redundant code in apps and device bindings. For example, reading pin values while clocking data to/from a device.

Example

The following writes the different bits to a pin.

BitArray bitArray = new BitArray(new byte[] { 0x1A });

foreach (bool bitValue in bitArray)
{
  if (bitValue)
  {
    _controller.Write(1, PinValue.High);
  }
  else
  {
    _controller.Write(1, PinValue.Low);
  }
  
  // Other logic...
}

The proposed changes would allow the code to be written as:

BitArray bitArray = new BitArray(new byte[] { 0x1A });

foreach (bool bitValue in bitArray)
{
  _controller.WriteBool(1, bitValue);

  // Other logic...
}

Proposed Changes

The proposed changes are to add the following helper methods within GpioController class.

/// <summary>
/// Reads the current boolean value of a pin.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <returns>The boolean value of the pin.</returns>
public bool ReadBool(int pinNumber)
{
    return PinValueToBool(_controller.Read(pinNumber));
}

/// <summary>
/// Writes a boolean value to a pin.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="value">The boolean value to be written.</param>
public void WriteBool(int pinNumber, bool value)
{
    _controller.Write(pinNumber, BoolToPinValue(value));
}

/// <summary>
/// Converts a pin value to a boolean value.
/// </summary>
/// <returns>The boolean value of the pin value.</returns>
public static bool PinValueToBool(PinValue value)
{
    return value == PinValue.High;
}

/// <summary>
/// Converts a boolean value to a pin value.
/// </summary>
/// <returns>The pin value of the boolean value.</returns>
public static PinValue BoolToPinValue(bool value)
{
    return value ? PinValue.High : PinValue.Low;
}

Open Questions

  1. I'm not sure the proposed method names are the best. I used WriteBool/ReadBool as you can't overload Read to return PinValue or bool. What would be the best names?

  2. Should there be an option as a bool? where null represents closed pin or high-impedance state? I don't think so as that makes things a little more complex. Low (false) and High (true) should be good enough.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Device.GpioContains types for using general-purpose I/O (GPIO) pins

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions