Skip to content

Commit

Permalink
Merge pull request #254 from WildernessLabs/feature/can
Browse files Browse the repository at this point in the history
Feature/can
  • Loading branch information
adrianstevens authored Jul 29, 2024
2 parents 2aac55d + 773eace commit 062b242
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class DigitalInterruptPortBase : DigitalInputPortBase, IDigitalI
/// Gets or sets a value indicating the type of interrupt monitoring this input.
/// </summary>
/// <value><c>true</c> if interrupt enabled; otherwise, <c>false</c>.</value>
public InterruptMode InterruptMode { get; protected set; }
public abstract InterruptMode InterruptMode { get; set; }

/// <summary>
/// Gets or sets the debounce duration for the port
Expand All @@ -35,17 +35,13 @@ public abstract class DigitalInterruptPortBase : DigitalInputPortBase, IDigitalI
/// </summary>
/// <param name="pin">The pin associated with the port.</param>
/// <param name="channel">The channel information for the port.</param>
/// <param name="interruptMode"></param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="pin"/> or <paramref name="channel"/> is <c>null</c>.</exception>
protected DigitalInterruptPortBase(
IPin pin,
IDigitalChannelInfo channel,
InterruptMode interruptMode = InterruptMode.None
IDigitalChannelInfo channel
)
: base(pin, channel)
{
// TODO: check interrupt mode (i.e. if != none, make sure channel info agrees)
InterruptMode = interruptMode;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Meadow.Hardware;

/// <summary>
/// Contract for devices that expose `ICanBus(es)`.
/// </summary>
public interface ICanController
{
/// <summary>
/// Creates a CAN bus instance for the requested bus number and bus speed
/// </summary>
/// <param name="busNumber">The bus number</param>
/// <param name="bitrate">The bus bit rate</param>
/// <returns>An instance of an <see cref="ISpiBus"/></returns>
ICanBus CreateCanBus(CanBitrate bitrate, int busNumber);
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
namespace Meadow.Hardware
namespace Meadow.Hardware;

/// <summary>
/// Contract for devices that expose `ISpiBus(es)`.
/// </summary>
public interface ISpiController : IDigitalOutputController
{
/// <summary>
/// Contract for devices who expose `ISpiBus(es)`.
/// The default SPI Bus speed, in kHz, used when speed parameters are not provided
/// </summary>
public interface ISpiController : IDigitalOutputController
{
/// <summary>
/// The default SPI Bus speed, in kHz, used when speed parameters are not provided
/// </summary>
public static Units.Frequency DefaultSpiBusSpeed = new Units.Frequency(375, Units.Frequency.UnitType.Kilohertz);
public static Units.Frequency DefaultSpiBusSpeed = new Units.Frequency(375, Units.Frequency.UnitType.Kilohertz);

/// <summary>
/// Creates a SPI bus instance for the requested control pins and clock configuration
/// </summary>
/// <param name="clock">The IPin instance to use as the bus clock</param>
/// <param name="copi">The IPin instance to use for data transmit (controller out/peripheral in)</param>
/// <param name="cipo">The IPin instance to use for data receive (controller in/peripheral out)</param>
/// <param name="config">The bus clock configuration parameters</param>
/// <returns>An instance of an <see cref="ISpiBus"/></returns>
public ISpiBus CreateSpiBus(
IPin clock,
IPin copi,
IPin cipo,
SpiClockConfiguration config
);
/// <summary>
/// Creates a SPI bus instance for the requested control pins and clock configuration
/// </summary>
/// <param name="clock">The IPin instance to use as the bus clock</param>
/// <param name="copi">The IPin instance to use for data transmit (controller out/peripheral in)</param>
/// <param name="cipo">The IPin instance to use for data receive (controller in/peripheral out)</param>
/// <param name="config">The bus clock configuration parameters</param>
/// <returns>An instance of an <see cref="ISpiBus"/></returns>
public ISpiBus CreateSpiBus(
IPin clock,
IPin copi,
IPin cipo,
SpiClockConfiguration config
);

/// <summary>
/// Creates a SPI bus instance for the requested control pins and bus speed
/// </summary>
/// <param name="clock">The IPin instance to use as the bus clock</param>
/// <param name="copi">The IPin instance to use for data transmit (controller out/peripheral in)</param>
/// <param name="cipo">The IPin instance to use for data receive (controller in/peripheral out)</param>
/// <param name="speed">The bus speed</param>
/// <returns>An instance of an <see cref="ISpiBus"/></returns>
ISpiBus CreateSpiBus(
IPin clock,
IPin copi,
IPin cipo,
Units.Frequency speed
);
/// <summary>
/// Creates a SPI bus instance for the requested control pins and bus speed
/// </summary>
/// <param name="clock">The IPin instance to use as the bus clock</param>
/// <param name="copi">The IPin instance to use for data transmit (controller out/peripheral in)</param>
/// <param name="cipo">The IPin instance to use for data receive (controller in/peripheral out)</param>
/// <param name="speed">The bus speed</param>
/// <returns>An instance of an <see cref="ISpiBus"/></returns>
ISpiBus CreateSpiBus(
IPin clock,
IPin copi,
IPin cipo,
Units.Frequency speed
);

/// <summary>
/// Creates a SPI bus instance for the requested bus number and bus speed
/// </summary>
/// <param name="speed">The bus speed</param>
/// <param name="busNumber">The bus number</param>
/// <returns>An instance of an <see cref="ISpiBus"/></returns>
ISpiBus CreateSpiBus(
int busNumber,
Units.Frequency speed
);
}
/// <summary>
/// Creates a SPI bus instance for the requested bus number and bus speed
/// </summary>
/// <param name="speed">The bus speed</param>
/// <param name="busNumber">The bus number</param>
/// <returns>An instance of an <see cref="ISpiBus"/></returns>
ISpiBus CreateSpiBus(
int busNumber,
Units.Frequency speed
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents an active error frame for CAN communication.
/// </summary>
public class ActiveErrorFrame : Frame
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
namespace Meadow.Hardware;

/// <summary>
/// Specifies the bitrate for CAN communication.
/// </summary>
public enum CanBitrate
{
/// <summary>
/// 5 kbps CAN bitrate.
/// </summary>
Can_5kbps,

/// <summary>
/// 10 kbps CAN bitrate.
/// </summary>
Can_10kbps,

/// <summary>
/// 20 kbps CAN bitrate.
/// </summary>
Can_20kbps,

/// <summary>
/// 33 kbps CAN bitrate.
/// </summary>
Can_33kbps,

/// <summary>
/// 40 kbps CAN bitrate.
/// </summary>
Can_40kbps,

/// <summary>
/// 47 kbps CAN bitrate.
/// </summary>
Can_47kbps,

/// <summary>
/// 50 kbps CAN bitrate.
/// </summary>
Can_50kbps,

/// <summary>
/// 80 kbps CAN bitrate.
/// </summary>
Can_80kbps,

/// <summary>
/// 83 kbps CAN bitrate.
/// </summary>
Can_83kbps,

/// <summary>
/// 95 kbps CAN bitrate.
/// </summary>
Can_95kbps,

/// <summary>
/// 100 kbps CAN bitrate.
/// </summary>
Can_100kbps,

/// <summary>
/// 125 kbps CAN bitrate.
/// </summary>
Can_125kbps,

/// <summary>
/// 200 kbps CAN bitrate.
/// </summary>
Can_200kbps,

/// <summary>
/// 250 kbps CAN bitrate.
/// </summary>
Can_250kbps,

/// <summary>
/// 500 kbps CAN bitrate.
/// </summary>
Can_500kbps,

/// <summary>
/// 800 kbps CAN bitrate.
/// </summary>
Can_800kbps,

/// <summary>
/// 1 Mbps CAN bitrate.
/// </summary>
Can_1Mbps,

/// <summary>
/// Flexible Data Rate
/// </summary>
Can_FD,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents an abstract data frame for CAN communication, inheriting from Frame.
/// </summary>
public abstract class DataFrame : Frame
{
/// <summary>
/// Gets or sets the identifier for the data frame.
/// </summary>
public int ID { get; set; }

/// <summary>
/// Gets or sets the payload of the data frame.
/// </summary>
public byte[] Payload { get; set; } = new byte[0];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents an extended data frame for CAN communication.
/// </summary>
public class ExtendedDataFrame : DataFrame
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents an extended remote transfer request (RTR) frame for CAN communication.
/// </summary>
public class ExtendedRtrFrame : RemoteTransferRequestFrame
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents an abstract base class for CAN (Controller Area Network) frames.
/// </summary>
public abstract class Frame : ICanFrame
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;

namespace Meadow.Hardware;

/// <summary>
/// Represents a Controller Area Network (CAN) bus interface.
/// </summary>
public interface ICanBus
{
/// <summary>
/// Occurs when a CAN frame is received.
/// </summary>
event EventHandler<ICanFrame>? FrameReceived;

/// <summary>
/// Writes a CAN frame to the specified buffer.
/// </summary>
/// <param name="frame">The CAN frame to write.</param>
void WriteFrame(ICanFrame frame);

/// <summary>
/// Checks if a CAN frame is available to read.
/// </summary>
/// <returns><c>true</c> if a frame is available; otherwise, <c>false</c>.</returns>
bool IsFrameAvailable();

/// <summary>
/// Reads a CAN frame.
/// </summary>
/// <returns>The CAN frame if available; otherwise, <c>null</c>.</returns>
ICanFrame? ReadFrame();

/// <summary>
/// Sets the CAN filter.
/// </summary>
/// <param name="filter">The filter value.</param>
void SetFilter(int filter);

/// <summary>
/// Sets the CAN mask.
/// </summary>
/// <param name="filter">The mask value.</param>
void SetMask(int filter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents a CAN (Controller Area Network) frame.
/// </summary>
public interface ICanFrame
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents an overload frame for CAN communication.
/// </summary>
public class OverloadFrame : Frame
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents a remote transfer request frame for CAN communication.
/// </summary>
public class RemoteTransferRequestFrame : DataFrame
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents a standard data frame for CAN communication.
/// </summary>
public class StandardDataFrame : DataFrame
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Meadow.Hardware;

/// <summary>
/// Represents a standard remote transfer request frame (RTR) for CAN communication.
/// </summary>
public class StandardRtrFrame : RemoteTransferRequestFrame
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Meadow.Peripherals.Sensors.Buttons;

namespace Meadow.Peripherals.Sensors.Hid;

/// <summary>
/// Interface describing digital joysticks and d-pads with a center push button
/// </summary>
public interface IDigitalPushButtonJoystick : IDigitalJoystick, IButton
{
}

0 comments on commit 062b242

Please sign in to comment.