Skip to content

Commit

Permalink
Merge pull request #330 from WildernessLabs/adrian-distance-cleanup
Browse files Browse the repository at this point in the history
Distance driver cleanup
  • Loading branch information
jorgedevs authored May 8, 2022
2 parents c895aaf + a7ecd12 commit b9702cd
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 36 deletions.
14 changes: 0 additions & 14 deletions Source/Meadow.Foundation.Core/Sensors/Spatial/IDistanceSensor.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ void AnalogInitialize()
ChangeResult<Length> changeResult = new ChangeResult<Length>()
{
New = await ReadSensorAnalog(),
Old = Length,
Old = Distance,
};
// save state
Length = changeResult.New;
Distance = changeResult.New;
// notify
RaiseEventsAndNotify(changeResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MaxBotix(ISerialMessagePort serialMessage, SensorType sensor)

Length ReadSensorSerial()
{ //I think we'll just cache it for serial
return Length.Value;
return Distance.Value;
}

private void SerialMessagePort_MessageReceived(object sender, SerialMessageData e)
Expand All @@ -53,10 +53,10 @@ private void SerialMessagePort_MessageReceived(object sender, SerialMessageData
ChangeResult<Length> changeResult = new ChangeResult<Length>()
{
New = new Length(value, units),
Old = Length,
Old = Distance,
};
// save state
Length = changeResult.New;
Distance = changeResult.New;
// notify
RaiseEventsAndNotify(changeResult);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
using Meadow.Units;
using Meadow.Peripherals.Sensors;
using Meadow.Units;
using System;
using System.Threading.Tasks;

namespace Meadow.Foundation.Sensors.Distance
{
public partial class MaxBotix : ByteCommsSensorBase<Length>
public partial class MaxBotix : ByteCommsSensorBase<Length>, IRangeFinder
{
/// <summary>
/// Raised when the value of the reading changes.
/// </summary>
public event EventHandler<IChangeResult<Length>> LengthUpdated = delegate { };
public event EventHandler<IChangeResult<Length>> DistanceUpdated = delegate { };

/// <summary>
/// Distance from sensor to object
/// </summary>
public Length? Length { get; protected set; }
public Length? Distance { get; protected set; }

/// <summary>
/// voltage common collector (VCC) typically 3.3V
/// </summary>
public double VCC { get; set; } = 3.3;

CommunicationType communication;
SensorType sensorType;
readonly CommunicationType communication;
readonly SensorType sensorType;

/// <summary>
/// Read the distance from the sensor
Expand All @@ -46,7 +47,7 @@ protected override async Task<Length> ReadSensor()
/// <param name="changeResult"></param>
protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
{
LengthUpdated?.Invoke(this, changeResult);
DistanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public MeadowApp()

maxBotix.Subscribe(consumer);

maxBotix.LengthUpdated += MaxBotix_LengthUpdated;
maxBotix.DistanceUpdated += MaxBotix_DistanceUpdated;

maxBotix.StartUpdating(new TimeSpan(0, 0, 1));
}

private void MaxBotix_LengthUpdated(object sender, IChangeResult<Length> e)
private void MaxBotix_DistanceUpdated(object sender, IChangeResult<Length> e)
{
Console.WriteLine($"Length: {e.New.Centimeters}cm");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Meadow.Foundation.Sensors.Distance
{
// TODO: why is `DistanceUpdated` never invoked? is this sensor done?
// In progress and not working - DistanceUpdated currently is never called
public class Mb10x0 : SensorBase<Length>, IRangeFinder
{
//==== events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ namespace Meadow.Foundation.Sensors.Distance
/// </summary>
public class Sfsr02: SensorBase<Length>, IRangeFinder
{
//==== events
/// <summary>
/// Raised when an received a rebound trigger signal
/// </summary>
public event EventHandler<IChangeResult<Length>> DistanceUpdated;

//==== internals
/// <summary>
/// Trigger/Echo Pin
/// </summary>
protected IBiDirectionalPort triggerEchoPort;

/// <summary>
/// Start time
/// </summary>
protected long tickStart;


//==== properties
/// <summary>
/// Returns current distance
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ namespace Meadow.Foundation.Sensors.Distance
/// </summary>
public partial class Vl53l0x : ByteCommsSensorBase<Length>, IRangeFinder
{
/// <summary>
/// Distance updated event
/// </summary>
public event EventHandler<IChangeResult<Length>> DistanceUpdated = delegate { };

/// <summary>
/// Is the hardware shutdown / off
/// </summary>
public bool IsShutdown
{
get
Expand Down Expand Up @@ -48,16 +54,27 @@ public bool IsShutdown

byte stopVariable;

/// <summary>
/// Creates a new Vl53l0x object
/// </summary>
/// <param name="device">Meadow device</param>
/// <param name="i2cBus">I2C bus</param>
/// <param name="address">I2C address</param>
public Vl53l0x(
IDigitalOutputController device, II2cBus i2cBus,
byte address = (byte)Addresses.Default)
: this (device, i2cBus, null, address)
{
}

/// <summary>
/// Creates a new Vl53l0x object
/// </summary>
/// <param name="device">Meadow device</param>
/// <param name="i2cBus">I2C bus</param>
/// <param name="shutdownPin">Shutdown pin</param>
/// <param name="address">VL53L0X address</param>
/// <param name="units">Unit of measure</param>

public Vl53l0x(
IDigitalOutputController device, II2cBus i2cBus, IPin shutdownPin,
byte address = (byte)Addresses.Default)
Expand All @@ -69,6 +86,10 @@ public Vl53l0x(
Initialize().Wait();
}

/// <summary>
/// Raise distance change event and notify subscribers
/// </summary>
/// <param name="changeResult"></param>
protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
{
DistanceUpdated?.Invoke(this, changeResult);
Expand Down Expand Up @@ -101,8 +122,6 @@ protected async Task Initialize()
Peripheral.WriteRegister(0xFF, 0x00);
Peripheral.WriteRegister(0x80, 0x00);

//var configControl = (byte)(Read((byte)Register.MsrcConfigControl) | 0x12);

Peripheral.WriteRegister((byte)Register.SystemSequenceConfig, 0xFF);
var spadInfo = GetSpadInfo();
int spadCount = spadInfo.Item1;
Expand Down

0 comments on commit b9702cd

Please sign in to comment.