Skip to content

Commit

Permalink
Merge branch 'WildernessLabs:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
engunneer authored Oct 30, 2023
2 parents 5240428 + 6f85849 commit f51f772
Show file tree
Hide file tree
Showing 19 changed files with 155 additions and 86 deletions.
16 changes: 8 additions & 8 deletions Source/Meadow.Foundation.Core/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@ public static Color Default
/// <summary>
/// Get the 4bpp grayscale value for current color
/// </summary>
public byte Color4bppGray => (byte)((byte)(0.2989 * R + 0.5870 * G + 0.114 * B) >> 4);
public readonly byte Color4bppGray => (byte)((byte)(0.2989 * R + 0.5870 * G + 0.114 * B) >> 4);

/// <summary>
/// Get the 8bpp grayscale value for current color
/// </summary>
public byte Color8bppGray => (byte)(0.2989 * R + 0.5870 * G + 0.114 * B);
public readonly byte Color8bppGray => (byte)(0.2989 * R + 0.5870 * G + 0.114 * B);

/// <summary>
/// Get the 8bpp (332) color value for current color
/// </summary>
public byte Color8bppRgb332 => (byte)((R & 0b11100000) | (G & 0b1110000) >> 3 | ((B & 0b11000000) >> 6));
public readonly byte Color8bppRgb332 => (byte)((R & 0b11100000) | (G & 0b1110000) >> 3 | ((B & 0b11000000) >> 6));

/// <summary>
/// Get the 12bpp (444) color value for current color
/// </summary>
public ushort Color12bppRgb444 =>
public readonly ushort Color12bppRgb444 =>
(ushort)(((R & 0b11110000) << 4) | (G & 0b11110000) | ((B & 0b11110000) >> 4));

/// <summary>
/// Get the 16bpp (565) color value for current color
/// </summary>
public ushort Color16bppRgb565 =>
public readonly ushort Color16bppRgb565 =>
(ushort)(((R & 0b11111000) << 8) | ((G & 0b11111100) << 3) | (B >> 3));

/// <summary>
/// Get the 1bpp (on or off) value for current color
/// </summary>
public bool Color1bpp => R > 0 || G > 0 || B > 0;
public readonly bool Color1bpp => R > 0 || G > 0 || B > 0;

/// <summary>
/// Current alpha value (0-255)
Expand Down Expand Up @@ -273,7 +273,7 @@ static void ConvertToHsb(double r, double g, double b, out double h, out double
/// Get hash of color
/// </summary>
/// <returns>hash as 32bit int</returns>
public override int GetHashCode()
public override readonly int GetHashCode()
{
return HashCode.Combine(R, G, B, A);
}
Expand All @@ -283,7 +283,7 @@ public override int GetHashCode()
/// </summary>
/// <param name="obj"></param>
/// <returns>true if equals</returns>
public override bool Equals(object obj)
public override readonly bool Equals(object obj)
{
if (obj is Color color)
{
Expand Down
24 changes: 24 additions & 0 deletions Source/Meadow.Foundation.Core/Extensions/ColorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,29 @@ public static Color FromAhsv(this Color color, double alpha, double hue, double

return new Color((byte)(red * 255), (byte)(green * 255), (byte)(blue * 255), (byte)(alpha * 255));
}

/// <summary>
/// Blend a new color with the current color
/// </summary>
/// <param name="color">The source color</param>
/// <param name="blendColor">The color to blend</param>
/// <param name="ratio">The ratio of the blend color to source color</param>
/// <returns>The resulting blended color</returns>
public static Color Blend(this Color color, Color blendColor, double ratio)
{
if (ratio == 0)
{
return color;
}
if (ratio == 1)
{
return blendColor;
}

byte r = (byte)(color.R * (1 - ratio) + blendColor.R * ratio);
byte g = (byte)(color.G * (1 - ratio) + blendColor.G * ratio);
byte b = (byte)(color.B * (1 - ratio) + blendColor.B * ratio);
return Color.FromRgb(r, g, b);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public async Task StartPulse(
brightness = highBrightness;
}
SetBrightness(brightness);
_ = SetBrightness(brightness);
Thread.Sleep(intervalTime);
}
Expand Down
15 changes: 15 additions & 0 deletions Source/Meadow.Foundation.Core/Motors/BidirectionalDcMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ namespace Meadow.Foundation.Motors;
/// </summary>
public class BidirectionalDcMotor
{
/// <summary>
/// Represents the state of a motor
/// </summary>
public enum MotorState
{
/// <summary>
/// The motor is stopped
/// </summary>
Stopped,
/// <summary>
/// The motor is running clockwise
/// </summary>
RunningClockwise,
/// <summary>
/// The motor is running counterclockwise
/// </summary>
RunningCounterclockwise
}

/// <summary>
/// Occurs when the state of the motor changes
/// </summary>
public event EventHandler<MotorState> StateChanged = default!;

private readonly IDigitalOutputPort _outputA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ public abstract class PushButtonBase : IButton, IDisposable
protected DateTime ButtonPressStart { get; set; } = DateTime.MaxValue;

/// <summary>
/// The button port resistor mode
/// The digital input port used by the button
/// </summary>
// protected ResistorMode resistorMode => DigitalIn.Resistor;

protected IDigitalInputPort DigitalIn { get; private set; }

/// <summary>
/// The minimum duration for a long press
/// </summary>
public TimeSpan LongClickedThreshold { get; set; } = TimeSpan.Zero;

/// <summary>
/// Initializes a new instance of the PushButtonBase class with the specified digital input port
/// </summary>
/// <param name="inputPort">The digital input port to associate with the push button</param>
protected PushButtonBase(IDigitalInputPort inputPort)
{
LongClickedThreshold = DefaultLongPressThreshold;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public partial class MicroAudio
{
readonly IToneGenerator speaker;

SystemSounds systemSounds;
GameSounds gameSounds;
SystemSounds? systemSounds;
GameSounds? gameSounds;

/// <summary>
/// Create a new MicroAudio instance from a IToneGenerator driver instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public override void InvertPixel(int x, int y)
/// <param name="buffer">buffer to write</param>
public override void WriteBuffer(int x, int y, IPixelBuffer buffer)
{
if (buffer.ColorMode == ColorMode)
if (buffer is Buffer1bpp buf1bpp)
{
for (int i = 0; i < buffer.Width; i++)
{
Expand All @@ -207,7 +207,7 @@ public override void WriteBuffer(int x, int y, IPixelBuffer buffer)
}
else
{ //else 1 bit at a time
SetPixel(x + i, y + j, (buffer as Buffer1bpp).GetPixelIsEnabled(i, j));
SetPixel(x + i, y + j, buf1bpp.GetPixelIsEnabled(i, j));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,13 @@ public override void InvertPixel(int x, int y)
/// <param name="buffer">buffer to write</param>
public override void WriteBuffer(int x, int y, IPixelBuffer buffer)
{
if (buffer.ColorMode == ColorMode)
if (buffer is Buffer1bppV buf1bppV)
{
for (int i = 0; i < buffer.Width; i++)
{
for (int j = 0; j < buffer.Height; j++)
{
//1 bit at a time
SetPixel(x + i, y + j, (buffer as Buffer1bpp).GetPixelIsEnabled(i, j));
{ //1 bit at a time - ToDo
SetPixel(x + i, y + j, buf1bppV.GetPixelIsEnabled(i, j));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ public void InitializeBuffer(bool replaceIfExists = false)
/// </summary>
public virtual void Clear()
{
Array.Clear(Buffer, 0, Buffer.Length);
if (Buffer != null)
{
Array.Clear(Buffer, 0, Buffer.Length);
}
}

/// <summary>
Expand Down Expand Up @@ -257,7 +260,7 @@ public T RotateAndConvert<T>(RotationType rotation)
public T ScaleUp<T>(int scaleFactor)
where T : PixelBufferBase, new()
{
T newBuffer = new T
T newBuffer = new()
{
Width = Width * scaleFactor,
Height = Height * scaleFactor,
Expand Down Expand Up @@ -289,7 +292,7 @@ public T ConvertPixelBuffer<T>()
return Clone<T>();
}

T newBuffer = new T()
T newBuffer = new()
{
Width = Width,
Height = Height,
Expand Down Expand Up @@ -346,7 +349,7 @@ public double GetColorDistance(Color color1, Color color2)
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if(!isDisposed)
if (!isDisposed)
{
if (createdBuffer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class Image
/// <summary>
/// The image pixel data
/// </summary>
public IPixelBuffer DisplayBuffer { get; private set; }
public IPixelBuffer? DisplayBuffer { get; private set; }

/// <summary>
/// The image width in pixels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected class CanvasState
/// <summary>
/// The current font
/// </summary>
public IFont CurrentFont { get; set; }
public IFont? CurrentFont { get; set; }

/// <summary>
/// The current stroke when drawing primitives
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ public void WriteLine(string text, byte lineNumber, bool showCursor = false)

void DrawCursor()
{
InvertRectangle(CursorColumn * currentFont.Width * DisplayConfig.FontScale,
CursorLine * currentFont.Height * DisplayConfig.FontScale,
currentFont.Width, currentFont.Height);
if (currentFont != null)
{
InvertRectangle(CursorColumn * currentFont.Width * DisplayConfig.FontScale,
CursorLine * currentFont.Height * DisplayConfig.FontScale,
currentFont.Width, currentFont.Height);
}
}

/// <summary>
Expand Down
Loading

0 comments on commit f51f772

Please sign in to comment.