Skip to content

Commit

Permalink
Merge pull request #80 from WildernessLabs/develop
Browse files Browse the repository at this point in the history
Merge to main for RC2-2
  • Loading branch information
jorgedevs authored Mar 5, 2023
2 parents c1f3ce8 + 59f730c commit 4f28169
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 24 deletions.
5 changes: 4 additions & 1 deletion Source/Meadow.Contracts/Hardware/Bases/Pin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class Pin : IPin
{
public IList<IChannelInfo>? SupportedChannels { get; protected set; }

public IPinController Controller { get; }

public string Name { get; protected set; }
/// <summary>
/// Identifier that the parent Device can use to identify the I/O (address, port, pin, etc)
Expand All @@ -19,8 +21,9 @@ public class Pin : IPin

//public abstract IChannelInfo ActiveChannel { get; protected set; }

public Pin(string name, object key, IList<IChannelInfo>? supportedChannels)
public Pin(IPinController controller, string name, object key, IList<IChannelInfo>? supportedChannels)
{
Controller = controller;
Name = name;
Key = key;
SupportedChannels = supportedChannels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Meadow.Hardware
/// <summary>
/// Contract for devices that expose `IAnalogInputPort(s)`.
/// </summary>
public interface IAnalogInputController
public interface IAnalogInputController : IPinController
{
// TODO: if Microsoft ever gets around to fixing the compile time const
// thing, we should make this a `Voltage`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Meadow.Hardware
/// <summary>
/// Contract for devices that expose `IBiDirectionPort(s)`.
/// </summary>
public interface IBiDirectionalController
public interface IBiDirectionalController : IPinController
{
/// <summary>
/// Creates an `IBiDirectionPort` on the specified pin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Contract for devices capable of creating `ICounter` instances
/// </summary>
public interface ICounterController
public interface ICounterController : IPinController
{
public ICounter CreateCounter(
IPin pin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Meadow.Hardware
/// Contract for IO devices that are capable of creating `IDigitalInputPort`
/// instances.
/// </summary>
public interface IDigitalInputController
public interface IDigitalInputController : IPinController
{
/// <summary>
/// Creates an IDigitalInputPort on the specified pin.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
namespace Meadow.Hardware
namespace Meadow.Hardware
{
/// <summary>
/// Contract for IO devices that are capable of creating `IDigitalOuputPort`
/// instances.
/// </summary>
public interface IDigitalOutputController
public interface IDigitalOutputController : IPinController
{
/// <summary>
/// Creates an IDigitalOutputPort on the specified pin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Contract for devices that expose an `II2cBus`.
/// </summary>
public interface II2cController
public interface II2cController : IPinController
{
/// <summary>
/// The default I2C Bus speed, in Hz, used when speed parameters are not provided
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Meadow.Units;
using System;

namespace Meadow.Hardware
{
/// <summary>
/// Contract for devices that expose `IPwmPort(s)`.
/// </summary>
public interface IPwmOutputController
public interface IPwmOutputController : IPinController
{
public const float DefaultPwmFrequency = 100f;
public const float DefaultPwmDutyCycle = 0.5f;
Expand Down
5 changes: 3 additions & 2 deletions Source/Meadow.Contracts/Hardware/Contracts/IPin.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Meadow.Hardware
{
/// <summary>
/// Contract for a pin on the Meadow board.
/// Contract for a pin
/// </summary>
public interface IPin : IEquatable<IPin>
{
public IPinController Controller { get; }

/// <summary>
/// Supported channels
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions Source/Meadow.Contracts/Hardware/Contracts/IPinController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Meadow.Hardware
{
/// <summary>
/// Contract for a container/parent of an IPin
/// </summary>
public interface IPinController
{
}
}
13 changes: 6 additions & 7 deletions Source/Meadow.Contracts/Hardware/Contracts/IPinDefinitions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Meadow.Hardware;

namespace Meadow.Hardware
{
Expand All @@ -18,11 +15,13 @@ public interface IPinDefinitions : IEnumerable<IPin>
/// <value>All the pins.</value>
IList<IPin> AllPins { get; }

IPin this[string name]
IPin this[string name]
{
get => AllPins.FirstOrDefault(p =>
string.Compare(p.Name, name, true) == 0
get => AllPins.FirstOrDefault(p =>
string.Compare(p.Name, name, true) == 0
|| string.Compare($"{p.Key}", name, true) == 0);
}

IPinController Controller { get; set; }
}
}
149 changes: 148 additions & 1 deletion Source/Meadow.Contracts/Hardware/Contracts/IPinExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Meadow.Units;
using System;
using System.Linq;

namespace Meadow.Hardware
Expand Down Expand Up @@ -33,5 +34,151 @@ public static bool Supports<TChannel>(this IPin pin, Func<TChannel, bool> where)
var chan = pin.SupportedChannels.OfType<TChannel>().Where(where).FirstOrDefault();
return chan != null;
}

/// <summary>
/// Creates an IDigitalOutputPort on a capable IPin
/// </summary>
/// <param name="pin">The source pin</param>
/// <param name="initialState">Initial state of the port</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IDigitalOutputPort CreateDigitalOutputPort(this IPin pin, bool initialState = false)
{
if (pin.Controller is IDigitalOutputController controller)
{
return controller.CreateDigitalOutputPort(pin, initialState);
}

throw new ArgumentException("Pin is not digital output capable");
}

/// <summary>
/// Creates an IDigitalInputPort on a capable IPin
/// </summary>
/// <param name="pin">The source pin</param>
/// <param name="interruptMode"></param>
/// <param name="resistorMode"></param>
/// <param name="debounceDuration"></param>
/// <param name="glitchDuration"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IDigitalInputPort CreateDigitalInputPort(this IPin pin, InterruptMode interruptMode, ResistorMode resistorMode, TimeSpan debounceDuration, TimeSpan glitchDuration)
{
if (pin.Controller is IDigitalInputController controller)
{
return controller.CreateDigitalInputPort(pin, interruptMode, resistorMode, debounceDuration, glitchDuration);
}

throw new ArgumentException("Pin is not digital input capable");
}

/// <summary>
/// Creates an IDigitalInputPort on a capable IPin
/// </summary>
/// <param name="pin"></param>
/// <param name="interruptMode"></param>
/// <param name="resistorMode"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IDigitalInputPort CreateDigitalInputPort(this IPin pin, InterruptMode interruptMode = InterruptMode.None, ResistorMode resistorMode = ResistorMode.Disabled)
{
if (pin.Controller is IDigitalInputController controller)
{
return controller.CreateDigitalInputPort(pin, interruptMode, resistorMode);
}

throw new ArgumentException("Pin is not digital input capable");
}

/// <summary>
/// Creates an IAnalogInputPort on a capable IPin
/// </summary>
/// <param name="pin"></param>
/// <param name="sampleCount"></param>
/// <param name="sampleInterval"></param>
/// <param name="referenceVoltage"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IAnalogInputPort CreateAnalogInputPort(this IPin pin, int sampleCount, TimeSpan sampleInterval, Units.Voltage referenceVoltage)
{
if (pin.Controller is IAnalogInputController controller)
{
return controller.CreateAnalogInputPort(pin, sampleCount, sampleInterval, referenceVoltage);
}

throw new ArgumentException("Pin is not analog input capable");
}

/// <summary>
/// Creates an IAnalogInputPort on a capable IPin
/// </summary>
/// <param name="pin"></param>
/// <param name="sampleCount"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IAnalogInputPort CreateAnalogInputPort(this IPin pin, int sampleCount)
{
if (pin.Controller is IAnalogInputController controller)
{
return controller.CreateAnalogInputPort(pin, sampleCount);
}

throw new ArgumentException("Pin is not analog input capable");
}

/// <summary>
/// Creates an IPwmPort on a capable IPin
/// </summary>
/// <param name="pin"></param>
/// <param name="frequency"></param>
/// <param name="dutyCycle"></param>
/// <param name="invert"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IPwmPort CreatePwmPort(this IPin pin, Frequency frequency, float dutyCycle = 0.5f, bool invert = false)
{
if (pin.Controller is IPwmOutputController controller)
{
return controller.CreatePwmPort(pin, frequency, dutyCycle, invert);
}

throw new ArgumentException("Pin is not PWM output capable");
}

/// <summary>
/// Creates an IBiDirectionalPort on a capable IPin
/// </summary>
/// <param name="pin"></param>
/// <param name="initialState"></param>
/// <param name="interruptMode"></param>
/// <param name="resistorMode"></param>
/// <param name="initialDirection"></param>
/// <param name="debounceDuration"></param>
/// <param name="glitchDuration"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IBiDirectionalPort CreateBiDirectionalPort(this IPin pin, bool initialState, InterruptMode interruptMode, ResistorMode resistorMode, PortDirectionType initialDirection, TimeSpan debounceDuration, TimeSpan glitchDuration)
{
if (pin.Controller is IBiDirectionalController controller)
{
return controller.CreateBiDirectionalPort(pin, initialState, interruptMode, resistorMode, initialDirection, debounceDuration, glitchDuration);
}

throw new ArgumentException("Pin controller is not an IBiDirectionalController");
}

/// <summary>
/// Creates an IBiDirectionalPort on a capable IPin
/// </summary>
/// <param name="pin"></param>
/// <param name="initialState"></param>
/// <param name="interruptMode"></param>
/// <param name="resistorMode"></param>
/// <param name="initialDirection"></param>
/// <returns></returns>
public static IBiDirectionalPort CreateBiDirectionalPort(this IPin pin, bool initialState = false, InterruptMode interruptMode = InterruptMode.None, ResistorMode resistorMode = ResistorMode.Disabled, PortDirectionType initialDirection = PortDirectionType.Input)
{
return pin.CreateBiDirectionalPort(initialState, interruptMode, resistorMode, initialDirection, TimeSpan.Zero, TimeSpan.Zero);
}
}
}
6 changes: 3 additions & 3 deletions Source/Meadow.Contracts/Meadow.Contracts.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
Expand All @@ -24,7 +24,7 @@
<None Include="icon.png" Link="icon.png" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Meadow.Logging" Version="0.*" />
<PackageReference Include="Meadow.Units" Version="0.*" />
<PackageReference Include="Meadow.Logging" Version="0.*" />
<PackageReference Include="Meadow.Units" Version="0.*" />
</ItemGroup>
</Project>

0 comments on commit 4f28169

Please sign in to comment.