Skip to content

Commit

Permalink
Merge pull request #822 from WildernessLabs/cleanup
Browse files Browse the repository at this point in the history
Nullability
  • Loading branch information
jorgedevs authored Oct 30, 2023
2 parents 6cf7d5a + 0045a0e commit f9a5b56
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public IAnalogOutputPort CreateAnalogOutputPort(IPin pin)
/// <inheritdoc/>
public IAnalogOutputPort CreateAnalogOutputPort(IPin pin, Gain gain = Gain.Gain1x, bool bufferedInput = false)
{
if (!pin.Controller.Equals(this))
if (pin.Controller == null || !pin.Controller.Equals(this))
{
throw new ArgumentException("The provided pin must be on this controller");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public abstract partial class Mcp23xxx : IDigitalInputOutputController, ISpiPeri
/// </summary>
public Frequency SpiBusSpeed
{
get => (mcpDevice as ISpiCommunications).BusSpeed;
set => (mcpDevice as ISpiCommunications).BusSpeed = value;
get => (mcpDevice as ISpiCommunications)!.BusSpeed;
set => (mcpDevice as ISpiCommunications)!.BusSpeed = value;
}

/// <summary>
Expand All @@ -48,8 +48,8 @@ public Frequency SpiBusSpeed
/// </summary>
public SpiClockConfiguration.Mode SpiBusMode
{
get => (mcpDevice as ISpiCommunications).BusMode;
set => (mcpDevice as ISpiCommunications).BusMode = value;
get => (mcpDevice as ISpiCommunications)!.BusMode;
set => (mcpDevice as ISpiCommunications)!.BusMode = value;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Ws2812(ISpiBus spiBus, IPin chipSelectPin, int numberOfLeds)
/// <param name="spiBus">SPI bus</param>
/// <param name="numberOfLeds">Number of leds</param>
/// <param name="chipSelectPort">SPI chip select port (optional)</param>
public Ws2812(ISpiBus spiBus, int numberOfLeds, IDigitalOutputPort chipSelectPort = null)
public Ws2812(ISpiBus spiBus, int numberOfLeds, IDigitalOutputPort? chipSelectPort = null)
{
spiComms = new SpiCommunications(spiBus, chipSelectPort, DefaultSpiBusSpeed, DefaultSpiBusMode);
this.numberOfLeds = numberOfLeds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ public int StepsPerRevolution

readonly IDigitalOutputPort stepPort;
readonly IDigitalOutputPort directionPort;
readonly IDigitalOutputPort enablePort;
readonly IDigitalOutputPort ms1Port;
readonly IDigitalOutputPort ms2Port;
readonly IDigitalOutputPort ms3Port;
readonly IDigitalOutputPort? enablePort;
readonly IDigitalOutputPort? ms1Port;
readonly IDigitalOutputPort? ms2Port;
readonly IDigitalOutputPort? ms3Port;
readonly object syncRoot = new object();

StepDivisor divisor;
Expand Down Expand Up @@ -156,7 +156,7 @@ public A4988(IPin step, IPin direction, IPin enable)
/// <param name="ms2Pin">The (optional) Meadow pin connected to the MS2 pin of the A4988</param>
/// <param name="ms3Pin">The (optional) Meadow pin connected to the MS3 pin of the A4988</param>
/// <remarks>You must provide either all of the micro-step (MS) lines or none of them</remarks>
public A4988(IPin step, IPin direction, IPin enablePin, IPin ms1Pin, IPin ms2Pin, IPin ms3Pin)
public A4988(IPin step, IPin direction, IPin? enablePin, IPin? ms1Pin, IPin? ms2Pin, IPin? ms3Pin)
{
stepPort = step.CreateDigitalOutputPort();

Expand All @@ -168,13 +168,13 @@ public A4988(IPin step, IPin direction, IPin enablePin, IPin ms1Pin, IPin ms2Pin
}

// micro-step lines (for now) are all-or-nothing TODO: rethink this?
if (new IPin[] { ms1Pin, ms2Pin, ms3Pin }.All(p => p != null))
if (new IPin?[] { ms1Pin, ms2Pin, ms3Pin }.All(p => p != null))
{
ms1Port = ms1Pin.CreateDigitalOutputPort();
ms2Port = ms2Pin.CreateDigitalOutputPort();
ms3Port = ms3Pin.CreateDigitalOutputPort();
ms1Port = ms1Pin?.CreateDigitalOutputPort();
ms2Port = ms2Pin?.CreateDigitalOutputPort();
ms3Port = ms3Pin?.CreateDigitalOutputPort();
}
else if (new IPin[] { ms1Pin, ms2Pin, ms3Pin }.All(p => p == null))
else if (new IPin?[] { ms1Pin, ms2Pin, ms3Pin }.All(p => p == null))
{ // nop
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class Ds3231 : Ds323x
/// <param name="interruptPin">Digital pin connected to the alarm interrupt pin on the RTC.</param>
/// <param name="i2cBus">The I2C Bus the peripheral is connected to</param>
/// <param name="address">I2C Bus address of the peripheral</param>
public Ds3231(II2cBus i2cBus, IPin interruptPin = null, byte address = (byte)Addresses.Default)
public Ds3231(II2cBus i2cBus, IPin? interruptPin = null, byte address = (byte)Addresses.Default)
: base(new I2cCommunications(i2cBus, address), interruptPin)
{ }

Expand All @@ -25,7 +25,7 @@ public Ds3231(II2cBus i2cBus, IPin interruptPin = null, byte address = (byte)Add
/// <param name="interruptPort">Digital port connected to the alarm interrupt pin on the RTC.</param>
public Ds3231(
II2cBus i2cBus,
IDigitalInterruptPort interruptPort = null,
IDigitalInterruptPort? interruptPort = null,
byte address = (byte)Addresses.Default)
: base(new I2cCommunications(i2cBus, address), interruptPort)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public partial class Ds323x : II2cPeripheral, IDisposable
/// <summary>
/// Create a new Ds323x object
/// </summary>
protected Ds323x(I2cCommunications i2cComms, IPin interruptPin)
protected Ds323x(I2cCommunications i2cComms, IPin? interruptPin)
{
this.i2cComms = i2cComms;

Expand Down Expand Up @@ -215,7 +215,7 @@ public Units.Temperature Temperature
/// <summary>
/// Interrupt port attached to the DS323x RTC module.
/// </summary>
protected IDigitalInterruptPort InterruptPort { get; private set; }
protected IDigitalInterruptPort? InterruptPort { get; private set; }

/// <summary>
/// Control register.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public bool GasConversionIsEnabled
/// <summary>
/// The default SPI bus speed for the device
/// </summary>
public Frequency DefaultSpiBusSpeed => new Frequency(10000, Frequency.UnitType.Kilohertz);
public Frequency DefaultSpiBusSpeed => new(10000, Frequency.UnitType.Kilohertz);

/// <summary>
/// The SPI bus speed for the device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,16 @@ bool RunCommand(byte cmd,
return true;
}

void SendCommand(byte cmd, byte[] args = null, byte argn = 0)
void SendCommand(byte cmd, byte[]? args = null, byte argn = 0)
{
serialPort.Write(new byte[] { 0x56, SerialNumber, cmd });

for (byte i = 0; i < argn; i++)
if (args != null)
{
serialPort.Write(new byte[] { args[i] });
for (byte i = 0; i < argn; i++)
{
serialPort.Write(new byte[] { args[i] });
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public bool IsShutdown
/// </summary>
public byte DefaultI2cAddress => (byte)Addresses.Default;

readonly IDigitalOutputPort shutdownPort;
readonly IDigitalOutputPort? shutdownPort;

byte stopVariable;

Expand All @@ -75,12 +75,12 @@ public Vl53l0x(II2cBus i2cBus, byte address = (byte)Addresses.Default)
/// <param name="shutdownPin">Shutdown pin</param>
/// <param name="address">VL53L0X address</param>

public Vl53l0x(II2cBus i2cBus, IPin shutdownPin, byte address = (byte)Addresses.Default)
public Vl53l0x(II2cBus i2cBus, IPin? shutdownPin, byte address = (byte)Addresses.Default)
: base(i2cBus, address)
{
if (shutdownPin != null)
{
shutdownPort = shutdownPin.CreateDigitalOutputPort(true);
shutdownPort = shutdownPin?.CreateDigitalOutputPort(true);
}
Initialize().Wait();
}
Expand Down Expand Up @@ -258,7 +258,7 @@ public void MeasureDistance()
/// Returns the current distance/range
/// </summary>
/// <returns>The distance in the specified Units. Default mm. Returns -1 if the shutdown pin is used and is off</returns>
protected override async Task<Length> ReadSensor()
protected override async Task<Length?> ReadSensor()
{
//Resolver.Log.Info("ReadSensor");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public partial class Adxl362
/// <summary>
/// Digital input port attached to interrupt pin 1 on the ADXL362
/// </summary>
private IDigitalInterruptPort digitalInputPort1;
private IDigitalInterruptPort? digitalInputPort1;

/// <summary>
/// Digital Input port attached to interrupt pin 2 on the ADXL362
/// </summary>
private IDigitalInterruptPort digitalInputPort2;
private IDigitalInterruptPort? digitalInputPort2;

/// <summary>
/// The current acceleration value
Expand All @@ -62,8 +62,8 @@ public partial class Adxl362
/// </summary>
public Frequency SpiBusSpeed
{
get => ((SpiCommunications)BusComms).BusSpeed;
set => ((SpiCommunications)BusComms).BusSpeed = value;
get => (BusComms as ISpiCommunications)!.BusSpeed;
set => (BusComms as ISpiCommunications)!.BusSpeed = value;
}

/// <summary>
Expand All @@ -77,8 +77,8 @@ public Frequency SpiBusSpeed
/// </summary>
public SpiClockConfiguration.Mode SpiBusMode
{
get => ((SpiCommunications)BusComms).BusMode;
set => ((SpiCommunications)BusComms).BusMode = value;
get => (BusComms as ISpiCommunications)!.BusMode;
set => (BusComms as ISpiCommunications)!.BusMode = value;
}

/// <summary>
Expand Down Expand Up @@ -333,7 +333,7 @@ public void Stop()
/// Read data from the sensor
/// </summary>
/// <returns>The sensor data</returns>
public override Task<(Acceleration3D? Acceleration3D, Units.Temperature? Temperature)> Read()
public override Task<(Acceleration3D? Acceleration3D, Units.Temperature? Temperature)?> Read()
{
Start();
return base.Read();
Expand Down Expand Up @@ -489,7 +489,7 @@ private InterruptMode MapInterruptMode(bool activeLow)
/// <param name="interruptPin1">Pin connected to interrupt pin 1 on the ADXL362</param>
/// <param name="interruptMap2">Bit mask for interrupt pin 2</param>
/// <param name="interruptPin2">Pin connected to interrupt pin 2 on the ADXL362</param>
private void ConfigureInterrupts(byte interruptMap1, IPin interruptPin1, byte interruptMap2 = 0, IPin interruptPin2 = null) // TODO: interrupPin2 = IDigitalPin.GPIO_NONE
private void ConfigureInterrupts(byte interruptMap1, IPin interruptPin1, byte interruptMap2 = 0, IPin? interruptPin2 = null) // TODO: interrupPin2 = IDigitalPin.GPIO_NONE
{
WriteBuffer.Span[0] = Commands.WRITE_REGISTER;
WriteBuffer.Span[1] = interruptMap1;
Expand Down Expand Up @@ -551,10 +551,10 @@ private void DisplayRegisters()
DebugInformation.DisplayRegisters(Registers.X_AXIS_8BITS, ReadBuffer.Span[2..].ToArray());
}

async Task<Acceleration3D> ISensor<Acceleration3D>.Read()
=> (await Read()).Acceleration3D.Value;
async Task<Acceleration3D?> ISensor<Acceleration3D>.Read()
=> (await Read()).Acceleration3D;

async Task<Units.Temperature> ISensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;
async Task<Units.Temperature?> ISensor<Units.Temperature>.Read()
=> (await Read()).Temperature;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ public Sensor TemperatureSource
{
get
{
return (Sensor)BusComms.ReadRegister(Registers.TemperatureSource);
return (Sensor)BusComms!.ReadRegister(Registers.TemperatureSource);
}
set
{
if ((value == Sensor.Accelerometer) || (value == Sensor.Gyroscope))
{
BusComms.WriteRegister(Registers.TemperatureSource, (byte)value);
BusComms?.WriteRegister(Registers.TemperatureSource, (byte)value);
}
else
{
Expand All @@ -141,10 +141,10 @@ public Sensor TemperatureSource
/// </summary>
public byte PowerMode
{
get => BusComms.ReadRegister(Registers.PowerMode);
get => BusComms!.ReadRegister(Registers.PowerMode);
set
{
BusComms.WriteRegister(Registers.PowerMode, value);
BusComms?.WriteRegister(Registers.PowerMode, value);
Thread.Sleep(15);
}
}
Expand All @@ -157,14 +157,14 @@ public byte PowerMode
/// </remarks>
public byte OperatingMode
{
get => BusComms.ReadRegister(Registers.OperatingMode);
get => BusComms!.ReadRegister(Registers.OperatingMode);
set
{
if (value > OperatingModes.MAXIMUM_VALUE)
{
throw new ArgumentOutOfRangeException();
}
BusComms.WriteRegister(Registers.OperatingMode, value);
BusComms?.WriteRegister(Registers.OperatingMode, value);
Thread.Sleep(20);
}
}
Expand All @@ -181,14 +181,14 @@ public byte OperatingMode
/// </remarks>
private byte Page
{
get => BusComms.ReadRegister(Registers.PageID);
get => BusComms!.ReadRegister(Registers.PageID);
set
{
if ((value != 0) && (value != 1))
{
throw new ArgumentOutOfRangeException();
}
BusComms.WriteRegister(Registers.PageID, value);
BusComms?.WriteRegister(Registers.PageID, value);
}
}

Expand All @@ -204,22 +204,22 @@ private byte Page
/// <summary>
/// Get the system calibration status.
/// </summary>
public bool IsSystemCalibrated => ((BusComms.ReadRegister(Registers.CalibrationStatus) >> 6) & 0x03) != 0;
public bool IsSystemCalibrated => ((BusComms!.ReadRegister(Registers.CalibrationStatus) >> 6) & 0x03) != 0;

/// <summary>
/// Get the accelerometer calibration status.
/// </summary>
public bool IsAccelerometerCalibrated => ((BusComms.ReadRegister(Registers.CalibrationStatus) >> 2) & 0x03) != 0;
public bool IsAccelerometerCalibrated => ((BusComms!.ReadRegister(Registers.CalibrationStatus) >> 2) & 0x03) != 0;

/// <summary>
/// Get the gyroscope calibration status.
/// </summary>
public bool IsGyroscopeCalibrated => ((BusComms.ReadRegister(Registers.CalibrationStatus) >> 4) & 0x03) != 0;
public bool IsGyroscopeCalibrated => ((BusComms!.ReadRegister(Registers.CalibrationStatus) >> 4) & 0x03) != 0;

/// <summary>
/// Get the magnetometer status.
/// </summary>
public bool IsMagnetometerCalibrated => (BusComms.ReadRegister(Registers.CalibrationStatus) & 0x03) != 0;
public bool IsMagnetometerCalibrated => (BusComms!.ReadRegister(Registers.CalibrationStatus) & 0x03) != 0;

/// <summary>
/// Is the system fully calibrated?
Expand Down Expand Up @@ -249,7 +249,7 @@ public Bno055(II2cBus i2cBus, Addresses address = Addresses.Default)
public Bno055(II2cBus i2cBus, byte address)
: base(i2cBus, address, readBufferSize: 256)
{
var id = BusComms.ReadRegister(Registers.ChipID);
var id = BusComms!.ReadRegister(Registers.ChipID);

if (id != 0xa0)
{
Expand Down Expand Up @@ -285,7 +285,7 @@ protected override Task<
(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D,
MagneticField3D? MagneticField3D, Quaternion? QuaternionOrientation,
Acceleration3D? LinearAcceleration, Acceleration3D? GravityVector,
EulerAngles? EulerOrientation, Units.Temperature? Temperature)> ReadSensor()
EulerAngles? EulerOrientation, Units.Temperature? Temperature)?> ReadSensor()
{
if (PowerMode != PowerModes.NORMAL)
{
Expand All @@ -310,7 +310,7 @@ protected override Task<
// in order to get the correct offset into the _sensorReadings array.

int length = Registers.GravityVectorZMSB + 1 - Registers.AccelerometerXLSB;
BusComms.ReadRegister(Registers.AccelerometerXLSB, ReadBuffer.Span[0..length]);
BusComms!.ReadRegister(Registers.AccelerometerXLSB, ReadBuffer.Span[0..length]);

// for debugging, you can look at the raw data:
//DebugInformation.DisplayRegisters(0x00, ReadBuffer.Span[0..length].ToArray());
Expand Down Expand Up @@ -441,7 +441,7 @@ public void DisplayRegisters()
int length = 0x6A;
byte[] buffer = new byte[length];

BusComms.ReadRegister(Registers.ChipID, buffer);
BusComms!.ReadRegister(Registers.ChipID, buffer);
DebugInformation.DisplayRegisters(0x00, buffer);

Resolver.Log.Info("== /REGISTERS =======================================================================");
Expand Down
Loading

0 comments on commit f9a5b56

Please sign in to comment.