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

Feature/pcan #902

Merged
merged 35 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9a3e07c
Starting work on the MCP2515
ctacke Jul 12, 2022
6235687
More MCP2515 work
ctacke Jul 13, 2022
37eb8b7
Merge remote-tracking branch 'origin/adrian-led-cleanup' into feature…
ctacke Jul 13, 2022
bd1411a
MP2542 CAN work
ctacke Jul 15, 2022
1d20911
starting PCAN basic driver
ctacke Feb 12, 2024
b38b4a9
Merge remote-tracking branch 'origin/develop' into feature/pcan
ctacke Feb 12, 2024
a67354e
more PCAn driver work
ctacke Feb 13, 2024
5c63703
Merge branch 'develop' into feature/pcan
ctacke Feb 16, 2024
4a1fe91
Merge branch 'develop' into feature/pcan
ctacke Feb 26, 2024
4b0eca0
Merge branch 'develop' into feature/pcan
ctacke Jul 5, 2024
91ddae3
Merge branch 'feature/mcp-2515' into feature/pcan
ctacke Jul 5, 2024
13da116
can
ctacke Jul 6, 2024
a4b2601
removed some build warnings
ctacke Jul 10, 2024
7172e68
more MCP2515 work
ctacke Jul 10, 2024
0665d2d
still working on MCP2515
ctacke Jul 12, 2024
0d80ad4
Merge remote-tracking branch 'origin/develop' into feature/pcan
ctacke Jul 15, 2024
fa69960
improved error in calibration
ctacke Jul 15, 2024
e94537c
MCP2515 bitrate to config register lookups
ctacke Jul 15, 2024
bfa4ead
bug fixes in MCP2515 - finally standard ID messages can send and receive
ctacke Jul 16, 2024
14aef81
MCP2515 standard and extended frames, tx and rx working
ctacke Jul 16, 2024
5df4f25
Merge remote-tracking branch 'origin/develop' into feature/pcan
ctacke Jul 16, 2024
a83be10
improve interrupt handling for MCP2515
ctacke Jul 17, 2024
1095a8d
Merge branch 'develop' into feature/pcan
ctacke Jul 18, 2024
8d64267
Merge branch 'develop' into feature/pcan
ctacke Jul 23, 2024
03ba9db
Merge branch 'develop' into feature/pcan
ctacke Jul 24, 2024
87907a9
fixes in push buttons and joystick
ctacke Jul 24, 2024
6b9cd1e
Merge branch 'bug/push-button-fixes' into feature/pcan
ctacke Jul 25, 2024
fa93b42
Mcp2515 updates
ctacke Jul 26, 2024
77a22b4
updating PCan to meet new can interfaces
ctacke Jul 26, 2024
e20b924
CAN samples
ctacke Jul 26, 2024
2422fa5
bug fix for tank leven sensor read at startup
ctacke Jul 29, 2024
53d6251
bound checking for histogram boxes
ctacke Jul 29, 2024
04b39c9
updates for interrupt port changes
ctacke Jul 29, 2024
1e9674f
improved messages for touch calibration
ctacke Jul 29, 2024
3007854
Merge branch 'develop' into feature/pcan
ctacke Jul 29, 2024
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
10 changes: 10 additions & 0 deletions Source/Meadow.Foundation.Core/Sensors/Buttons/PushButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ private void DigitalInChanged(object sender, DigitalPortResult result)
{
UpdateEvents(GetNormalizedState(result.New.State));
}

/// <inheritdoc/>
protected override bool GetNormalizedState(bool state)
{
return DigitalIn.Resistor switch
{
ResistorMode.ExternalPullUp or ResistorMode.InternalPullUp => !state,
_ => state,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected PushButtonBase(IDigitalInputPort inputPort)
/// Returns the sanitized state of the button
/// Inverts the state when using a pull-up resistor
/// </summary>
protected bool GetNormalizedState(bool state)
protected virtual bool GetNormalizedState(bool state)
{
return DigitalIn.Resistor switch
{
Expand Down
317 changes: 158 additions & 159 deletions Source/Meadow.Foundation.Core/Sensors/Hid/DigitalJoystick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,187 +3,186 @@
using Meadow.Peripherals.Sensors.Hid;
using System;

namespace Meadow.Foundation.Sensors.Hid
namespace Meadow.Foundation.Sensors.Hid;

/// <summary>
/// Represents a 4 switch digital joystick / directional pad (D-pad)
/// </summary>
public class DigitalJoystick : IDigitalJoystick, IDisposable
{
/// <summary>
/// Represents a 4 switch digital joystick / directional pad (D-pad)
/// Get the current digital joystick position
/// </summary>
public DigitalJoystickPosition? Position { get; protected set; } = DigitalJoystickPosition.Center;

/// <summary>
/// Raised when the digital joystick position changes
/// </summary>
public class DigitalJoystick : IDigitalJoystick, IDisposable
public event EventHandler<ChangeResult<DigitalJoystickPosition>> Updated = default!;

/// <summary>
/// The PushButton class for the up digital joystick switch
/// </summary>
public PushButton ButtonUp { get; protected set; }
/// <summary>
/// The PushButton class for the down digital joystick switch
/// </summary>
public PushButton ButtonDown { get; protected set; }
/// <summary>
/// The PushButton class for the left digital joystick switch
/// </summary>
public PushButton ButtonLeft { get; protected set; }
/// <summary>
/// The PushButton class for the right digital joystick switch
/// </summary>
public PushButton ButtonRight { get; protected set; }

/// <summary>
/// Is the object disposed
/// </summary>
public bool IsDisposed { get; private set; }

/// <summary>
/// Did we create the port(s) used by the peripheral
/// </summary>
private readonly bool createdPorts = false;
private readonly IDigitalInterruptPort portUp;
private readonly IDigitalInterruptPort portDown;
private readonly IDigitalInterruptPort portLeft;
private readonly IDigitalInterruptPort portRight;

/// <summary>
/// Create a new DigitalJoystick object
/// </summary>
/// <param name="pinUp">The pin connected to the up switch</param>
/// <param name="pinDown">The pin connected to the down switch</param>
/// <param name="pinLeft">The pin connected to the left switch</param>
/// <param name="pinRight">The pin connected to the right switch</param>
/// <param name="resistorMode">The resistor mode for all pins</param>
public DigitalJoystick(IPin pinUp, IPin pinDown, IPin pinLeft, IPin pinRight, ResistorMode resistorMode)
: this(pinUp.CreateDigitalInterruptPort(InterruptMode.EdgeBoth, resistorMode),
pinDown.CreateDigitalInterruptPort(InterruptMode.EdgeBoth, resistorMode),
pinLeft.CreateDigitalInterruptPort(InterruptMode.EdgeBoth, resistorMode),
pinRight.CreateDigitalInterruptPort(InterruptMode.EdgeBoth, resistorMode))
{
/// <summary>
/// Get the current digital joystick position
/// </summary>
public DigitalJoystickPosition? Position { get; protected set; } = DigitalJoystickPosition.Center;

/// <summary>
/// Raised when the digital joystick position changes
/// </summary>
public event EventHandler<ChangeResult<DigitalJoystickPosition>> Updated = default!;

/// <summary>
/// The PushButton class for the up digital joystick switch
/// </summary>
public PushButton ButtonUp { get; protected set; }
/// <summary>
/// The PushButton class for the down digital joystick switch
/// </summary>
public PushButton ButtonDown { get; protected set; }
/// <summary>
/// The PushButton class for the left digital joystick switch
/// </summary>
public PushButton ButtonLeft { get; protected set; }
/// <summary>
/// The PushButton class for the right digital joystick switch
/// </summary>
public PushButton ButtonRight { get; protected set; }

/// <summary>
/// Is the object disposed
/// </summary>
public bool IsDisposed { get; private set; }

/// <summary>
/// Did we create the port(s) used by the peripheral
/// </summary>
readonly bool createdPorts = false;

readonly IDigitalInterruptPort portUp;
readonly IDigitalInterruptPort portDown;
readonly IDigitalInterruptPort portLeft;
readonly IDigitalInterruptPort portRight;

/// <summary>
/// Create a new DigitalJoystick object
/// </summary>
/// <param name="pinUp">The pin connected to the up switch</param>
/// <param name="pinDown">The pin connected to the down switch</param>
/// <param name="pinLeft">The pin connected to the left switch</param>
/// <param name="pinRight">The pin connected to the right switch</param>
/// <param name="resistorMode">The resistor mode for all pins</param>
public DigitalJoystick(IPin pinUp, IPin pinDown, IPin pinLeft, IPin pinRight, ResistorMode resistorMode)
: this(pinUp.CreateDigitalInterruptPort(InterruptMode.EdgeBoth, resistorMode),
pinDown.CreateDigitalInterruptPort(InterruptMode.EdgeBoth, resistorMode),
pinLeft.CreateDigitalInterruptPort(InterruptMode.EdgeBoth, resistorMode),
pinRight.CreateDigitalInterruptPort(InterruptMode.EdgeBoth, resistorMode))
{
createdPorts = true;
}
createdPorts = true;
}

/// <summary>
/// Create a new DigitalJoystick object
/// </summary>
/// <param name="portUp">The digital port for the up switch</param>
/// <param name="portDown">The digital port for the down switch</param>
/// <param name="portLeft">The digital port for the left switch</param>
/// <param name="portRight">The digital port for the right switch</param>
public DigitalJoystick(IDigitalInterruptPort portUp,
IDigitalInterruptPort portDown,
IDigitalInterruptPort portLeft,
IDigitalInterruptPort portRight)
{
ButtonUp = new PushButton(this.portUp = portUp);
ButtonDown = new PushButton(this.portDown = portDown);
ButtonLeft = new PushButton(this.portLeft = portLeft);
ButtonRight = new PushButton(this.portRight = portRight);

ButtonUp.PressStarted += PressStarted;
ButtonDown.PressStarted += PressStarted;
ButtonLeft.PressStarted += PressStarted;
ButtonRight.PressStarted += PressStarted;

ButtonUp.PressEnded += PressEnded;
ButtonDown.PressEnded += PressEnded;
ButtonLeft.PressEnded += PressEnded;
ButtonUp.PressEnded += PressEnded;
}
/// <summary>
/// Create a new DigitalJoystick object
/// </summary>
/// <param name="portUp">The digital port for the up switch</param>
/// <param name="portDown">The digital port for the down switch</param>
/// <param name="portLeft">The digital port for the left switch</param>
/// <param name="portRight">The digital port for the right switch</param>
public DigitalJoystick(IDigitalInterruptPort portUp,
IDigitalInterruptPort portDown,
IDigitalInterruptPort portLeft,
IDigitalInterruptPort portRight)
{
Resolver.Log.Info($"DJ Up resistor: {portUp.Resistor}");
ButtonUp = new PushButton(this.portUp = portUp);
ButtonDown = new PushButton(this.portDown = portDown);
ButtonLeft = new PushButton(this.portLeft = portLeft);
ButtonRight = new PushButton(this.portRight = portRight);

ButtonUp.PressStarted += PressStarted;
ButtonDown.PressStarted += PressStarted;
ButtonLeft.PressStarted += PressStarted;
ButtonRight.PressStarted += PressStarted;

ButtonUp.PressEnded += PressEnded;
ButtonDown.PressEnded += PressEnded;
ButtonLeft.PressEnded += PressEnded;
ButtonRight.PressEnded += PressEnded;
}

private void PressEnded(object sender, EventArgs e)
=> Update();
private void PressEnded(object sender, EventArgs e)
=> Update();

private void PressStarted(object sender, EventArgs e)
=> Update();
private void PressStarted(object sender, EventArgs e)
=> Update();

private void Update()
{
var isLeftPressed = ButtonLeft.State;
var isRightPressed = ButtonRight.State;
var isUpPressed = ButtonUp.State;
var isDownPressed = ButtonDown.State;
private void Update()
{
var isLeftPressed = ButtonLeft.State;
var isRightPressed = ButtonRight.State;
var isUpPressed = ButtonUp.State;
var isDownPressed = ButtonDown.State;

var newPosition = GetDigitalPosition(isLeftPressed, isRightPressed, isUpPressed, isDownPressed);
var newPosition = GetDigitalPosition(isLeftPressed, isRightPressed, isUpPressed, isDownPressed);

if (newPosition != Position)
{
Updated?.Invoke(this, new ChangeResult<DigitalJoystickPosition>(newPosition, Position));
Position = newPosition;
}
if (newPosition != Position)
{
Updated?.Invoke(this, new ChangeResult<DigitalJoystickPosition>(newPosition, Position));
Position = newPosition;
}
}

private DigitalJoystickPosition GetDigitalPosition(bool isLeftPressed, bool isRightPressed, bool isUpPressed, bool isDownPressed)
{
if (isRightPressed)
{ //Right
if (isUpPressed)
{
return DigitalJoystickPosition.UpRight;
}
if (isDownPressed)
{
return DigitalJoystickPosition.DownRight;
}
return DigitalJoystickPosition.Right;
}
else if (isLeftPressed)
{ //Left
if (isUpPressed)
{
return DigitalJoystickPosition.UpLeft;
}
if (isDownPressed)
{
return DigitalJoystickPosition.DownLeft;
}
return DigitalJoystickPosition.Left;
private DigitalJoystickPosition GetDigitalPosition(bool isLeftPressed, bool isRightPressed, bool isUpPressed, bool isDownPressed)
{
if (isRightPressed)
{ //Right
if (isUpPressed)
{
return DigitalJoystickPosition.UpRight;
}
else if (isUpPressed)
{ //Up
return DigitalJoystickPosition.Up;
if (isDownPressed)
{
return DigitalJoystickPosition.DownRight;
}
else if (isDownPressed)
{ //Down
return DigitalJoystickPosition.Down;
return DigitalJoystickPosition.Right;
}
else if (isLeftPressed)
{ //Left
if (isUpPressed)
{
return DigitalJoystickPosition.UpLeft;
}
else
{ //Center
return DigitalJoystickPosition.Center;
if (isDownPressed)
{
return DigitalJoystickPosition.DownLeft;
}
return DigitalJoystickPosition.Left;
}

///<inheritdoc/>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
else if (isUpPressed)
{ //Up
return DigitalJoystickPosition.Up;
}
else if (isDownPressed)
{ //Down
return DigitalJoystickPosition.Down;
}
else
{ //Center
return DigitalJoystickPosition.Center;
}
}

/// <summary>
/// Dispose of the object
/// </summary>
/// <param name="disposing">Is disposing</param>
protected virtual void Dispose(bool disposing)
///<inheritdoc/>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Dispose of the object
/// </summary>
/// <param name="disposing">Is disposing</param>
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (!IsDisposed)
if (disposing && createdPorts)
{
if (disposing && createdPorts)
{
portDown?.Dispose();
portLeft?.Dispose();
portRight?.Dispose();
portUp?.Dispose();
}

IsDisposed = true;
portDown?.Dispose();
portLeft?.Dispose();
portRight?.Dispose();
portUp?.Dispose();
}

IsDisposed = true;
}
}
}
Loading
Loading