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

added state change event to BiDirectionalMotor #729

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Changes from 1 commit
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
35 changes: 32 additions & 3 deletions Source/Meadow.Foundation.Core/Motors/BidirectionalDcMotor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Meadow.Hardware;
using System;

namespace Meadow.Foundation.Motors;

Expand All @@ -7,14 +8,40 @@ namespace Meadow.Foundation.Motors;
/// </summary>
public class BidirectionalDcMotor
{
public enum MotorState
{
Stopped,
RunningClockwise,
RunningCounterclockwise
}

public event EventHandler<MotorState> StateChanged = default!;

private IDigitalOutputPort _outputA;
private IDigitalOutputPort _outputB;
private bool _energizeHigh;

/// <summary>
/// Gets the current run state of the motor (true == running)
/// Gets the current run state of the motor
/// </summary>
public bool IsRunning => _outputB.State != _outputB.State;
public MotorState State
{
get
{
var a = _energizeHigh ? _outputA.State : !_outputA.State;
var b = _energizeHigh ? _outputB.State : !_outputB.State;

if (a == b)
{
return MotorState.Stopped;
}
if (a && !b)
{
return MotorState.RunningClockwise;
}
return MotorState.RunningCounterclockwise;
}
}

/// <summary>
/// Creates an instance of an BiDirectionalDcMotor
Expand All @@ -38,6 +65,7 @@ public BidirectionalDcMotor(
public void Stop()
{
_outputA.State = _outputB.State = _energizeHigh ? false : true;
StateChanged?.Invoke(this, State);
}

/// <summary>
Expand All @@ -46,6 +74,7 @@ public void Stop()
public void Clockwise()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we call these StartClockWise() and StartCounterClockWise()? it'll improve discoverability using intellisense when users type start... like we do on Led animations (i.e. StartBlink, StartPulse, etc.)

{
_outputA.State = !(_outputB.State = _energizeHigh ? true : false);
StateChanged?.Invoke(this, State);
}

/// <summary>
Expand All @@ -54,6 +83,6 @@ public void Clockwise()
public void CounterClockwise()
{
_outputA.State = !(_outputB.State = _energizeHigh ? false : true);
StateChanged?.Invoke(this, State);
}

}
Loading