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

Minor cleanup #896

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,22 @@ public partial class Vl53l0x : ByteCommsSensorBase<Length>, IRangeFinder, II2cPe
/// <summary>
/// Is the hardware shutdown / off
/// </summary>
public bool IsShutdown
{
get
{
if (shutdownPort != null)
{
return !shutdownPort.State;
}
else
{
return false;
}
}
}
public bool IsShutdown => shutdownPort?.State ?? false;

/// <summary>
/// The distance to the measured object
/// </summary>
public Length? Distance { get; protected set; } = new Length(0);
public Length? Distance { get; protected set; }

/// <summary>
/// Minimum valid distance
/// </summary>
public Length MinimumDistance => new Length(30, Length.UnitType.Millimeters);
public static Length MinimumDistance = new(30, Length.UnitType.Millimeters);

/// <summary>
/// Maximum valid distance (CurrentDistance returns -1 if above)
/// Maximum valid distance
/// </summary>
public Length MaximumDistance => new Length(2000, Length.UnitType.Millimeters);
public static Length MaximumDistance = new(2, Length.UnitType.Meters);

/// <summary>
/// The default I2C address for the peripheral
Expand Down Expand Up @@ -242,23 +229,19 @@ public void MeasureDistance()
/// <summary>
/// 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>
/// <returns>The current distance, returns 0 if the shutdown pin is used and is off</returns>
protected override async Task<Length> ReadSensor()
{
//Resolver.Log.Info("ReadSensor");

if (IsShutdown)
{
return new Length(-1f, Length.UnitType.Millimeters);
return new Length(0, Length.UnitType.Millimeters);
}

// get the distance
Distance = new Length(await GetRawRangeData(), Length.UnitType.Millimeters);

// throw away invalid distances if out of range
if (Distance > MaximumDistance)
{
Distance = new Length(-1, Length.UnitType.Millimeters);
Distance = MaximumDistance;
}

return Distance.Value;
Expand All @@ -271,7 +254,9 @@ protected override async Task<Length> ReadSensor()
public void SetAddress(byte newAddress)
{
if (IsShutdown)
{
return;
}

byte address = (byte)(newAddress & 0x7F);
BusComms.WriteRegister((byte)Register.I2CSlaveDeviceAddress, address);
Expand All @@ -280,7 +265,7 @@ public void SetAddress(byte newAddress)
/// <summary>
/// Set the Shutdown state of the device
/// </summary>
/// <param name="state">true = off/shutdown. false = on</param>
/// <param name="state">returns true if off/shutdown, false if on</param>
public async Task ShutDown(bool state)
{
if (shutdownPort == null)
Expand All @@ -294,16 +279,14 @@ public async Task ShutDown(bool state)
if (state == false)
{
await Initialize();
// TODO: is this still needed? the previous line wasn't awaited before.
await Task.Delay(2).ConfigureAwait(false);
}
}

/// <summary>
/// Get the spad info
/// Get the SPAD info
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
protected Tuple<int, bool> GetSpadInfo()
{
BusComms.WriteRegister(0x80, 0x01);
Expand Down Expand Up @@ -388,6 +371,9 @@ int Read16(byte address)
return (ReadBuffer.Span[0] << 8) | ReadBuffer.Span[1];
}

/// <summary>
/// Returns the raw range data from the sensor in millimeters
/// </summary>
async Task<int> GetRawRangeData()
{
BusComms.WriteRegister(0x80, 0x01);
Expand Down
Loading