Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obsolete PinNumberingScheme #2358

Merged
merged 14 commits into from
Nov 14, 2024
4 changes: 2 additions & 2 deletions src/devices/Arduino/ArduinoAnalogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ internal class ArduinoAnalogController : AnalogController
private readonly IReadOnlyList<SupportedPinConfiguration> _supportedPinConfigurations;

public ArduinoAnalogController(ArduinoBoard board,
IReadOnlyList<SupportedPinConfiguration> supportedPinConfigurations, PinNumberingScheme scheme)
: base(scheme)
IReadOnlyList<SupportedPinConfiguration> supportedPinConfigurations)
krwq marked this conversation as resolved.
Show resolved Hide resolved
: base()
pgrawehr marked this conversation as resolved.
Show resolved Hide resolved
{
_board = board ?? throw new ArgumentNullException(nameof(board));
_supportedPinConfigurations = supportedPinConfigurations ?? throw new ArgumentNullException(nameof(supportedPinConfigurations));
Expand Down
4 changes: 2 additions & 2 deletions src/devices/Arduino/ArduinoBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ public override GpioController CreateGpioController()
throw new ObjectDisposedException(nameof(_firmata));
}

return new GpioController(PinNumberingScheme.Logical, new ArduinoGpioControllerDriver(_firmata, _supportedPinConfigurations));
return new GpioController(new ArduinoGpioControllerDriver(_firmata, _supportedPinConfigurations));
}

/// <inheritdoc />
Expand Down Expand Up @@ -841,7 +841,7 @@ public virtual AnalogController CreateAnalogController(int chip)
{
Initialize();

return new ArduinoAnalogController(this, SupportedPinConfigurations, PinNumberingScheme.Logical);
return new ArduinoAnalogController(this, SupportedPinConfigurations);
}

/// <summary>
Expand Down
22 changes: 4 additions & 18 deletions src/devices/Board/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ protected Board()
/// </summary>
protected bool Disposed => _disposed;

/// <summary>
/// The default pin numbering scheme for this board.
/// </summary>
public PinNumberingScheme DefaultPinNumberingScheme
{
get
{
// This is currently hardcoded to logical numbering, since it makes the API simpler and there are few really useful cases where you would need it otherwise.
return PinNumberingScheme.Logical;
}
}

/// <summary>
/// Reserves a pin for a specific usage. This is done automatically if a known interface (i.e. GpioController) is
/// used to open the pin, but may be used to block a pin explicitly, i.e. for UART.
Expand Down Expand Up @@ -507,9 +495,8 @@ internal bool RemoveBus(I2cBusManager bus)
/// <param name="connectionSettings">Connection parameters (contains bus number and CS pin number)</param>
/// <param name="pinAssignment">The set of pins to use for SPI. The parameter can be null if the hardware requires a fixed mapping from
/// pins to SPI for the given bus.</param>
/// <param name="pinNumberingScheme">The numbering scheme in which the <paramref name="pinAssignment"/> is given</param>
/// <returns>An SPI device instance</returns>
public SpiDevice CreateSpiDevice(SpiConnectionSettings connectionSettings, int[] pinAssignment, PinNumberingScheme pinNumberingScheme)
public SpiDevice CreateSpiDevice(SpiConnectionSettings connectionSettings, int[] pinAssignment)
{
Initialize();

Expand Down Expand Up @@ -539,7 +526,7 @@ public SpiDevice CreateSpiDevice(SpiConnectionSettings connectionSettings)
// Returns logical pin numbers for the selected bus (or an exception if using a bus number > 1, because that
// requires specifying the pins)
int[] pinAssignment = GetDefaultPinAssignmentForSpi(connectionSettings);
return CreateSpiDevice(connectionSettings, pinAssignment, PinNumberingScheme.Logical);
return CreateSpiDevice(connectionSettings, pinAssignment);
}

/// <summary>
Expand All @@ -560,10 +547,9 @@ public SpiDevice CreateSpiDevice(SpiConnectionSettings connectionSettings)
/// <param name="frequency">Initial frequency</param>
/// <param name="dutyCyclePercentage">Initial duty cycle</param>
/// <param name="pin">The pin number for the pwm channel. Used if not hardwired (i.e. on the Raspi, it is possible to use different pins for the same PWM channel)</param>
/// <param name="pinNumberingScheme">The pin numbering scheme for the pin</param>
/// <returns>A pwm channel instance</returns>
public PwmChannel CreatePwmChannel(int chip, int channel, int frequency, double dutyCyclePercentage,
int pin, PinNumberingScheme pinNumberingScheme)
int pin)
{
Initialize();
return AddManager(new PwmChannelManager(this, pin, chip, channel, frequency, dutyCyclePercentage, CreateSimplePwmChannel));
Expand All @@ -585,7 +571,7 @@ public PwmChannel CreatePwmChannel(
{
Initialize();
int pin = GetDefaultPinAssignmentForPwm(chip, channel);
return CreatePwmChannel(chip, channel, frequency, dutyCyclePercentage, pin, PinNumberingScheme.Logical);
return CreatePwmChannel(chip, channel, frequency, dutyCyclePercentage, pin);
}

/// <summary>
Expand Down
9 changes: 4 additions & 5 deletions src/devices/Board/RaspberryPiBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,10 @@ public override int[] GetDefaultPinAssignmentForSpi(SpiConnectionSettings connec
/// </summary>
/// <param name="pinNumber">Pin number to use</param>
/// <param name="usage">Requested usage</param>
/// <param name="pinNumberingScheme">Pin numbering scheme for the pin provided (logical or physical)</param>
/// <param name="bus">Optional bus argument, for SPI and I2C pins</param>
/// <returns>
/// A member of <see cref="RaspberryPi3Driver.AltMode"/> describing the mode the pin is in.</returns>
private RaspberryPi3Driver.AltMode GetHardwareModeForPinUsage(int pinNumber, PinUsage usage, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, int bus = 0)
private RaspberryPi3Driver.AltMode GetHardwareModeForPinUsage(int pinNumber, PinUsage usage, int bus = 0)
{
if (pinNumber >= PinCount)
{
Expand Down Expand Up @@ -607,19 +606,19 @@ public override PinUsage DetermineCurrentPinUsage(int pinNumber)

// Do some heuristics: If the given pin number can be used for I2C with the same Alt mode, we can assume that's what it
// it set to.
var possibleAltMode = GetHardwareModeForPinUsage(pinNumber, PinUsage.I2c, DefaultPinNumberingScheme);
var possibleAltMode = GetHardwareModeForPinUsage(pinNumber, PinUsage.I2c);
if (possibleAltMode == pinMode)
{
return PinUsage.I2c;
}

possibleAltMode = GetHardwareModeForPinUsage(pinNumber, PinUsage.Spi, DefaultPinNumberingScheme);
possibleAltMode = GetHardwareModeForPinUsage(pinNumber, PinUsage.Spi);
if (possibleAltMode == pinMode)
{
return PinUsage.Spi;
}

possibleAltMode = GetHardwareModeForPinUsage(pinNumber, PinUsage.Pwm, DefaultPinNumberingScheme);
possibleAltMode = GetHardwareModeForPinUsage(pinNumber, PinUsage.Pwm);
if (possibleAltMode == pinMode)
{
return PinUsage.Pwm;
Expand Down
1 change: 0 additions & 1 deletion src/devices/Board/tests/BoardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public void ThereIsAlwaysAMatchingBoard()
Assert.NotNull(board);
var property = board.GetType().GetProperty("Initialized", BindingFlags.Instance | BindingFlags.NonPublic)!;
Assert.True((bool)property.GetValue(board)!);
Assert.Equal(PinNumberingScheme.Logical, board.DefaultPinNumberingScheme);
board.Dispose();
}

Expand Down
4 changes: 1 addition & 3 deletions src/devices/Common/System/Device/Analog/AnalogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public abstract class AnalogController : IDisposable
/// <summary>
/// Initializes a new instance of the <see cref="GpioController"/> class that will use the specified numbering scheme and driver.
/// </summary>
/// <param name="numberingScheme">The numbering scheme used to represent pins provided by the controller.</param>
protected AnalogController(PinNumberingScheme numberingScheme)
protected AnalogController()
{
NumberingScheme = numberingScheme;
_openPins = new List<AnalogInputPin>();
}

Expand Down
5 changes: 2 additions & 3 deletions src/devices/Dhtxx/Devices/Dht11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public class Dht11 : DhtBase
/// Create a DHT11 sensor
/// </summary>
/// <param name="pin">The pin number (GPIO number)</param>
/// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
/// <param name="gpioController"><see cref="GpioController"/> related with operations on pins</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public Dht11(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController? gpioController = null, bool shouldDispose = true)
: base(pin, pinNumberingScheme, gpioController, shouldDispose)
public Dht11(int pin, GpioController? gpioController = null, bool shouldDispose = true)
: base(pin, gpioController, shouldDispose)
{
}

Expand Down
5 changes: 2 additions & 3 deletions src/devices/Dhtxx/Devices/Dht12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ public class Dht12 : DhtBase
/// Create a DHT12 sensor
/// </summary>
/// <param name="pin">The pin number (GPIO number)</param>
/// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
/// <param name="gpioController"><see cref="GpioController"/> related with operations on pins</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public Dht12(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController? gpioController = null, bool shouldDispose = true)
: base(pin, pinNumberingScheme, gpioController, shouldDispose)
public Dht12(int pin, GpioController? gpioController = null, bool shouldDispose = true)
: base(pin, gpioController, shouldDispose)
{
}

Expand Down
5 changes: 2 additions & 3 deletions src/devices/Dhtxx/Devices/Dht21.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public class Dht21 : DhtBase
/// Create a DHT22 sensor
/// </summary>
/// <param name="pin">The pin number (GPIO number)</param>
/// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
/// <param name="gpioController"><see cref="GpioController"/> related with operations on pins</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public Dht21(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController? gpioController = null, bool shouldDispose = true)
: base(pin, pinNumberingScheme, gpioController, shouldDispose)
public Dht21(int pin, GpioController? gpioController = null, bool shouldDispose = true)
: base(pin, gpioController, shouldDispose)
{
}

Expand Down
5 changes: 2 additions & 3 deletions src/devices/Dhtxx/Devices/Dht22.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public class Dht22 : DhtBase
/// Create a DHT22 sensor
/// </summary>
/// <param name="pin">The pin number (GPIO number)</param>
/// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
/// <param name="gpioController"><see cref="GpioController"/> related with operations on pins</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public Dht22(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController? gpioController = null, bool shouldDispose = true)
: base(pin, pinNumberingScheme, gpioController, shouldDispose)
public Dht22(int pin, GpioController? gpioController = null, bool shouldDispose = true)
: base(pin, gpioController, shouldDispose)
{
}

Expand Down
5 changes: 2 additions & 3 deletions src/devices/Dhtxx/DhtBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,13 @@ public TimeSpan MinTimeBetweenReads
/// Create a DHT sensor
/// </summary>
/// <param name="pin">The pin number (GPIO number)</param>
/// <param name="pinNumberingScheme">The GPIO pin numbering scheme</param>
/// <param name="gpioController"><see cref="GpioController"/> related with operations on pins</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public DhtBase(int pin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController? gpioController = null, bool shouldDispose = true)
public DhtBase(int pin, GpioController? gpioController = null, bool shouldDispose = true)
{
_protocol = CommunicationProtocol.OneWire;
_shouldDispose = shouldDispose || gpioController is null;
_controller = gpioController ?? new GpioController(pinNumberingScheme);
_controller = gpioController ?? new GpioController();
_pin = pin;

// These sensors typically require 2.5 seconds between read attempts, or the result will be garbage
Expand Down
5 changes: 2 additions & 3 deletions src/devices/Hcsr04/Hcsr04.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ public Hcsr04(GpioController? gpioController, int triggerPin, int echoPin, bool
/// </summary>
/// <param name="triggerPin">Trigger pulse input.</param>
/// <param name="echoPin">Trigger pulse output.</param>
/// <param name="pinNumberingScheme">Pin Numbering Scheme</param>
public Hcsr04(int triggerPin, int echoPin, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical)
: this(new GpioController(pinNumberingScheme), triggerPin, echoPin)
public Hcsr04(int triggerPin, int echoPin)
: this(new GpioController(), triggerPin, echoPin)
{
}

Expand Down
5 changes: 2 additions & 3 deletions src/devices/LiquidLevel/LiquidLevelSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ public class LiquidLevelSwitch : IDisposable
/// <summary>Creates a new instance of the LiquidLevelSwitch.</summary>
/// <param name="dataPin">The data pin</param>
/// <param name="liquidPresentPinState">Data pin state representing liquid being present</param>
/// <param name="pinNumberingScheme">Use the logical or physical pin layout</param>
/// <param name="gpioController">A Gpio Controller if you want to use a specific one</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public LiquidLevelSwitch(int dataPin, PinValue liquidPresentPinState, GpioController? gpioController = null, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, bool shouldDispose = true)
public LiquidLevelSwitch(int dataPin, PinValue liquidPresentPinState, GpioController? gpioController = null, bool shouldDispose = true)
Copy link
Member Author

@krwq krwq Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are touching this we should consider using Pin class here given we're taking controller and a pin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add those ctors, not replace them. And better in a separate PR.

{
_controller = gpioController ?? new GpioController(pinNumberingScheme);
_controller = gpioController ?? new GpioController();
_shouldDispose = shouldDispose || gpioController is null;
_dataPin = dataPin;
_liquidPresentPinState = liquidPresentPinState;
Expand Down
5 changes: 2 additions & 3 deletions src/devices/LiquidLevel/Llc200d3sh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ public class Llc200d3sh : LiquidLevelSwitch
{
/// <summary>Creates a new instance of the Llc200d3sh.</summary>
/// <param name="pin">The data pin</param>
/// <param name="pinNumberingScheme">Use the logical or physical pin layout</param>
/// <param name="gpioController">A Gpio Controller if you want to use a specific one</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public Llc200d3sh(int pin, GpioController? gpioController = null, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, bool shouldDispose = true)
: base(pin, PinValue.Low, gpioController, pinNumberingScheme, shouldDispose)
public Llc200d3sh(int pin, GpioController? gpioController = null, bool shouldDispose = true)
: base(pin, PinValue.Low, gpioController, shouldDispose)
{
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/devices/Nrf24l01/Nrf24l01.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ private void Irq_ValueChanged(object sender, PinValueChangedEventArgs args)
#if NET5_0_OR_GREATER
[MemberNotNull(nameof(_gpio))]
#endif
private void Initialize(PinNumberingScheme pinNumberingScheme, OutputPower outputPower, DataRate dataRate, byte channel, GpioController? gpioController)
private void Initialize(OutputPower outputPower, DataRate dataRate, byte channel, GpioController? gpioController)
{
// open pins
_gpio = gpioController ?? new GpioController(pinNumberingScheme);
_gpio = gpioController ?? new GpioController();
_gpio.OpenPin(_ce, PinMode.Output);
_gpio.OpenPin(_irq, PinMode.Input);
_gpio.RegisterCallbackForPinValueChangedEvent(_irq, PinEventTypes.Falling, Irq_ValueChanged);
Expand Down
2 changes: 1 addition & 1 deletion src/devices/Pn5180/Pn5180.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Pn5180(SpiDevice spiDevice, int pinBusy, int pinNss, GpioController? gpio
_logger = this.GetCurrentClassLogger();
_logger.LogDebug($"Opening PN5180, pin busy: {pinBusy}, pin NSS: {pinNss}");
_spiDevice = spiDevice ?? throw new ArgumentNullException(nameof(spiDevice));
_gpioController = gpioController ?? new GpioController(PinNumberingScheme.Logical);
_gpioController = gpioController ?? new GpioController();
_shouldDispose = shouldDispose || gpioController is null;
_pinBusy = pinBusy;
_pinNss = pinNss;
Expand Down
2 changes: 1 addition & 1 deletion src/devices/RGBLedMatrix/RgbLedMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public RGBLedMatrix(PinMapping mapping, int width, int height, int chainRows = 1
_deviceRows = height / chainRows;

_gpio = new Gpio(mapping, _deviceRows);
_controller = new GpioController(PinNumberingScheme.Logical, _gpio);
_controller = new GpioController(_gpio);

OpenAndWriteToPin(_mapping.A, PinValue.Low);
OpenAndWriteToPin(_mapping.B, PinValue.Low);
Expand Down
19 changes: 4 additions & 15 deletions src/devices/Tm16xx/Tm1637.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,20 @@ public Tm1637(int pinClk, int pinDio, GpioController controller)
/// <param name="pinClk">The clock pin.</param>
/// <param name="pinDio">The data pin.</param>
public Tm1637(int pinClk, int pinDio)
: this(pinClk, pinDio, PinNumberingScheme.Logical, null, true)
: this(pinClk, pinDio, null, true)
{
}

/// <summary>
/// Initializes an instance of TM1637 with new Gpio controller which will be disposed with this object.
/// </summary>
/// <param name="pinClk">The clock pin.</param>
/// <param name="pinDio">The data pin.</param>
/// <param name="pinNumberingScheme">Uses the logical or physical pin layout for new created Gpio controller.</param>
public Tm1637(int pinClk, int pinDio, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical)
: this(pinClk, pinDio, pinNumberingScheme, null, true)
{
}

/// <summary>
/// <summary>
/// Initializes an instance of TM1637.
/// </summary>
/// <param name="pinClk">The clock pin.</param>
/// <param name="pinDio">The data pin.</param>
/// <param name="pinNumberingScheme">Uses the logical or physical pin layout for new created Gpio controller.</param>
/// <param name="gpioController">The instance of the gpio controller. Set to <see langword="null" /> to create a new one.</param>
/// <param name="shouldDispose">Sets to <see langword="true" /> to dispose the Gpio controller with this object. If the <paramref name="gpioController"/> is set to <see langword="null"/>, this parameter will be ignored and the new created Gpio controller will always be disposed with this object.</param>
public Tm1637(int pinClk, int pinDio, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, GpioController? gpioController = null, bool shouldDispose = true)
: base(pinClk, pinDio, ClockWidthMicroseconds, pinNumberingScheme, gpioController, shouldDispose)
public Tm1637(int pinClk, int pinDio, GpioController? gpioController = null, bool shouldDispose = true)
: base(pinClk, pinDio, ClockWidthMicroseconds, gpioController, shouldDispose)
{
_maxCharacters = 6;
_brightness = 7;
Expand Down
Loading
Loading