Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
58 changes: 58 additions & 0 deletions Source/Meadow.Foundation.Core/Graphics/Buffers/Buffer1bpp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,63 @@ public override void WriteBuffer(int x, int y, IPixelBuffer buffer)
base.WriteBuffer(x, y, buffer);
}
}

/// <summary>
/// Draw a horizontal line using a native 1bpp value
/// </summary>
/// <param name="x">X start position</param>
/// <param name="y">Y position</param>
/// <param name="length">Length of the line in pixels</param>
/// <param name="enabled">True to enable (on) or false to disable (off)</param>
public void DrawHorizontalLine(int x, int y, int length, bool enabled)
{
if (length <= 0 || y < 0 || y >= Height || x >= Width) return;

// Clamp to buffer bounds
if (x < 0)
{
length += x;
x = 0;
}
if (x + length > Width)
{
length = Width - x;
}
if (length <= 0) return;

for (int i = 0; i < length; i++)
{
SetPixel(x + i, y, enabled);
}
}

/// <summary>
/// Draw a vertical line using a native 1bpp value
/// </summary>
/// <param name="x">X position</param>
/// <param name="y">Y start position</param>
/// <param name="length">Length of the line in pixels</param>
/// <param name="enabled">True to enable (on) or false to disable (off)</param>
public void DrawVerticalLine(int x, int y, int length, bool enabled)
{
if (length <= 0 || x < 0 || x >= Width || y >= Height) return;

// Clamp to buffer bounds
if (y < 0)
{
length += y;
y = 0;
}
if (y + length > Height)
{
length = Height - y;
}
if (length <= 0) return;

for (int i = 0; i < length; i++)
{
SetPixel(x, y + i, enabled);
}
}
}
}
63 changes: 62 additions & 1 deletion Source/Meadow.Foundation.Core/Graphics/Buffers/BufferRgb332.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,66 @@ public override void WriteBuffer(int x, int y, IPixelBuffer buffer)
base.WriteBuffer(x, y, buffer);
}
}

/// <summary>
/// Draw a horizontal line using a native 8bpp color value
/// </summary>
/// <param name="x">X start position</param>
/// <param name="y">Y position</param>
/// <param name="length">Length of the line in pixels</param>
/// <param name="color">The color as an 8bpp RGB332 byte</param>
public void DrawHorizontalLine(int x, int y, int length, byte color)
{
if (length <= 0 || y < 0 || y >= Height || x >= Width) return;

// Clamp to buffer bounds
if (x < 0)
{
length += x;
x = 0;
}
if (x + length > Width)
{
length = Width - x;
}
if (length <= 0) return;

int index = y * Width + x;
for (int i = 0; i < length; i++)
{
Buffer[index + i] = color;
}
}

/// <summary>
/// Draw a vertical line using a native 8bpp color value
/// </summary>
/// <param name="x">X position</param>
/// <param name="y">Y start position</param>
/// <param name="length">Length of the line in pixels</param>
/// <param name="color">The color as an 8bpp RGB332 byte</param>
public void DrawVerticalLine(int x, int y, int length, byte color)
{
if (length <= 0 || x < 0 || x >= Width || y >= Height) return;

// Clamp to buffer bounds
if (y < 0)
{
length += y;
y = 0;
}
if (y + length > Height)
{
length = Height - y;
}
if (length <= 0) return;

int index = y * Width + x;
for (int i = 0; i < length; i++)
{
Buffer[index] = color;
index += Width;
}
}
}
}
}
71 changes: 71 additions & 0 deletions Source/Meadow.Foundation.Core/Graphics/Buffers/BufferRgb565.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,76 @@ public override void WriteBuffer(int x, int y, IPixelBuffer buffer)
base.WriteBuffer(x, y, buffer);
}
}

/// <summary>
/// Draw a horizontal line using a native 16bpp color value
/// </summary>
/// <param name="x">X start position</param>
/// <param name="y">Y position</param>
/// <param name="length">Length of the line in pixels</param>
/// <param name="color">The color as a 16bpp RGB565 ushort</param>
public unsafe void DrawHorizontalLine(int x, int y, int length, ushort color)
{
if (length <= 0 || y < 0 || y >= Height || x >= Width) return;

// Clamp to buffer bounds
if (x < 0)
{
length += x;
x = 0;
}
if (x + length > Width)
{
length = Width - x;
}
if (length <= 0) return;

fixed (byte* ptr = Buffer)
{
var pixelPtr = (ushort*)(ptr + ((y * Width + x) << 1));
ushort swappedColor = (ushort)((color << 8) | (color >> 8));

for (int i = 0; i < length; i++)
{
pixelPtr[i] = swappedColor;
}
}
}

/// <summary>
/// Draw a vertical line using a native 16bpp color value
/// </summary>
/// <param name="x">X position</param>
/// <param name="y">Y start position</param>
/// <param name="length">Length of the line in pixels</param>
/// <param name="color">The color as a 16bpp RGB565 ushort</param>
public unsafe void DrawVerticalLine(int x, int y, int length, ushort color)
{
if (length <= 0 || x < 0 || x >= Width || y >= Height) return;

// Clamp to buffer bounds
if (y < 0)
{
length += y;
y = 0;
}
if (y + length > Height)
{
length = Height - y;
}
if (length <= 0) return;

fixed (byte* ptr = Buffer)
{
ushort swappedColor = (ushort)((color << 8) | (color >> 8));
int stride = Width;

for (int i = 0; i < length; i++)
{
var pixelPtr = (ushort*)(ptr + (((y + i) * stride + x) << 1));
*pixelPtr = swappedColor;
}
}
}
}
}
Loading