Skip to content

Commit

Permalink
Merge pull request #72 from WildernessLabs/develop
Browse files Browse the repository at this point in the history
Merge to main for RC2-1
  • Loading branch information
adrianstevens authored Feb 1, 2023
2 parents 7674c40 + c4bf4ee commit 32320ec
Show file tree
Hide file tree
Showing 37 changed files with 1,841 additions and 79 deletions.
95 changes: 95 additions & 0 deletions Source/Meadow.Contracts/BitHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
namespace Meadow.Utilities
{
public static class BitHelpers
{
/// <summary>
/// Returns a new byte mask based on the input mask, with a single
/// bit set. To the passed in value.
/// </summary>
/// <param name="mask">The original byte mask value.</param>
/// <param name="bitIndex">The index of the bit to set.</param>
/// <param name="value">The value to set the bit. Should be 0 or 1.</param>
public static byte SetBit(byte mask, byte bitIndex, byte value)
{
return SetBit(mask, bitIndex, (value == 0) ? false : true);
}

/// <summary>
/// Returns a new byte mask based on the input mask, with a single
/// bit set. To the passed in value.
/// </summary>
/// <param name="mask">The original byte mask value.</param>
/// <param name="bitIndex">The index of the bit to set.</param>
/// <param name="value">The value to set the bit. true for 1, false for 0.</param>
/// <returns></returns>
public static byte SetBit(byte mask, byte bitIndex, bool value)
{
byte newMask = mask;

if (value)
{
newMask |= (byte)(1 << bitIndex);
}
else
{
newMask &= (byte)~(1 << bitIndex); // tricky to zero
}

return newMask;
}

/// <summary>
/// Returns a new 16-bit short with the single bit set or cleared
/// </summary>
/// <param name="mask">The original value</param>
/// <param name="bitIndex">The index of the bit to affect</param>
/// <param name="value">True to set, False to clear</param>
/// <returns></returns>
public static short SetBit(short mask, byte bitIndex, bool value)
{
short b = mask;
if (value)
{
return (short)(b | (short)(1 << bitIndex));
}
return (short)(b & (short)(~(1 << bitIndex)));
}

/// <summary>
/// Returns the value of the mask at the given ordinal.
/// </summary>
/// <param name="mask"></param>
/// <param name="bitIndex"></param>
/// <returns></returns>
public static bool GetBitValue(byte mask, byte bitIndex)
{

return ((mask & (byte)(1 << bitIndex)) != 0) ? true : false;
}

/// <summary>
/// Determines if a specified bit in a 16-bit short is set
/// </summary>
/// <param name="mask">The value to check</param>
/// <param name="bitIndex">The index of the bit to check</param>
/// <returns></returns>
public static bool GetBitValue(short mask, byte bitIndex)
{
if ((mask & (short)(1 << bitIndex)) == 0)
{
return false;
}
return true;
}

/// <summary>
/// Converts the first 2 bytes of an array to a little-endian 16-bit short
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static short ToInt16(byte[] data)
{
return (short)(data[0] | (data[1] << 8));
}
}
}
40 changes: 40 additions & 0 deletions Source/Meadow.Contracts/Channelnfo/AnalogChannelInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
namespace Meadow.Hardware
{
/// <summary>
/// Information about an analog channel
/// </summary>
public class AnalogChannelInfo : ChannelInfoBase, IAnalogChannelInfo
{
/// <summary>
/// Whether or not the channel is capable of reading input (i.e. ADC).
/// </summary>
/// <value><c>true</c> if input capable; otherwise, <c>false</c>.</value>
public bool InputCapable { get; protected set; }
/// <summary>
/// Whether or not the channel is capable of writing output (i.e. DAC).
/// </summary>
/// <value><c>true</c> if output capable; otherwise, <c>false</c>.</value>
public bool OutputCapable { get; protected set; }
/// <summary>
/// Precision (in bits) of the channel
/// </summary>
public byte Precision { get; protected set; }

/// <summary>
/// Create an AnalogChannelInfo instance
/// </summary>
/// <param name="name">The channel name</param>
/// <param name="precision">The precision (in bits) of the channel</param>
/// <param name="inputCapable">Whether or not the channel is ADC capable</param>
/// <param name="outputCapable">Whether or not the channel is DAC capable</param>
public AnalogChannelInfo(string name, byte precision, bool inputCapable, bool outputCapable)
: base (name)
{
this.Precision = precision;
this.InputCapable = inputCapable;
this.OutputCapable = outputCapable;
}

}
}
15 changes: 15 additions & 0 deletions Source/Meadow.Contracts/Channelnfo/CanChannelInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
namespace Meadow.Hardware
{
public class CanChannelInfo : DigitalChannelInfoBase, ICanChannelInfo
{
public SerialDirectionType SerialDirection { get; protected set; }

public CanChannelInfo(string name, SerialDirectionType serialDirection)
: base(name, true, true, true, true, true, false)
{
this.SerialDirection = serialDirection;
}

}
}
21 changes: 21 additions & 0 deletions Source/Meadow.Contracts/Channelnfo/DigitalChannelInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
namespace Meadow.Hardware
{
public class DigitalChannelInfo : DigitalChannelInfoBase
{
public DigitalChannelInfo(
string name,
bool inputCapable = true,
bool outputCapable = true,
bool interruptCapable = true,
bool pullDownCapable = true,
bool pullUpCapable = true,
bool inverseLogic = false,
int? interruptGroup = null
)
: base(name, inputCapable, outputCapable, interruptCapable,
pullDownCapable, pullUpCapable, inverseLogic, interruptGroup)
{
}
}
}
33 changes: 33 additions & 0 deletions Source/Meadow.Contracts/Channelnfo/PwmChannelInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
 using System;
namespace Meadow.Hardware
{
public class PwmChannelInfo : DigitalChannelInfoBase, IPwmChannelInfo
{
public float MinimumFrequency { get; protected set; }
public float MaximumFrequency { get; protected set; }
public uint Timer { get; protected set; }
public uint TimerChannel { get; protected set; }

public PwmChannelInfo(string name,
uint timer,
uint timerChannel,
float minimumFrequency = 0,
float maximumFrequency = 100000,
bool pullDownCapable = false, // does this mean anything in PWM?
bool pullUpCapable = false) // ibid
: base(
name,
inputCapable: true,
outputCapable: true,
interruptCapable: false, // ?? i mean, technically, yes, but will we have events?
pullDownCapable: pullDownCapable,
pullUpCapable: pullUpCapable,
inverseLogic: false) //TODO: switch to C# 7.2+ to get rid of trailing names
{
this.Timer = timer;
this.TimerChannel = timerChannel;
this.MinimumFrequency = minimumFrequency;
this.MaximumFrequency = maximumFrequency;
}
}
}
Loading

0 comments on commit 32320ec

Please sign in to comment.