Skip to content

Commit

Permalink
Add InvertPixel implementation + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed May 22, 2024
1 parent 729f5b7 commit e88febc
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions Source/CharlieWing/Driver/CharlieWing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ namespace Meadow.Foundation.FeatherWings
/// </summary>
public class CharlieWing : IPixelDisplay
{
private const int WidthInPixels = 15;
private const int HeightInPixels = 7;
private const int MaxFrames = 7;

/// <summary>
/// Is31fl3731 object to manage the leds
/// Is31fl3731 object to manage the LEDs
/// </summary>
protected readonly Is31fl3731 iS31FL3731;
protected readonly Is31fl3731 iS31Fl3731;

/// <summary>
/// Color mode of display
Expand All @@ -23,12 +27,12 @@ public class CharlieWing : IPixelDisplay
/// <summary>
/// Width of display in pixels
/// </summary>
public int Width => 15;
public int Width => WidthInPixels;

/// <summary>
/// Height of display in pixels
/// </summary>
public int Height => 7;
public int Height => HeightInPixels;

/// <summary>
/// The Is31fl3731 active frame
Expand All @@ -53,13 +57,13 @@ public class CharlieWing : IPixelDisplay
/// <param name="address">The I2C address</param>
public CharlieWing(II2cBus i2cBus, byte address = (byte)Is31fl3731.Addresses.Default)
{
iS31FL3731 = new Is31fl3731(i2cBus, address);
iS31FL3731.Initialize();
iS31Fl3731 = new Is31fl3731(i2cBus, address);
iS31Fl3731.Initialize();

for (byte i = 0; i <= 7; i++)
for (byte i = 0; i <= MaxFrames; i++)
{
iS31FL3731.SetLedState(i, true);
iS31FL3731.Clear(i);
iS31Fl3731.SetLedState(i, true);
iS31Fl3731.Clear(i);
}
}

Expand All @@ -69,7 +73,7 @@ public CharlieWing(II2cBus i2cBus, byte address = (byte)Is31fl3731.Addresses.Def
/// <param name="updateDisplay">Force a display update if true, false to clear the buffer</param>
public void Clear(bool updateDisplay = false)
{
iS31FL3731.Clear(Frame);
iS31Fl3731.Clear(Frame);
}

/// <summary>
Expand Down Expand Up @@ -102,6 +106,11 @@ public void DrawPixel(int x, int y, bool colored)
/// <param name="brightness">The led brightness from 0-255</param>
public void DrawPixel(int x, int y, byte brightness)
{
if (x < 0 || x >= WidthInPixels || y < 0 || y >= HeightInPixels)
{
throw new ArgumentOutOfRangeException($"Pixel coordinates ({x}, {y}) are out of bounds.");
}

if (x > 7)
{
x = 15 - x;
Expand All @@ -112,10 +121,10 @@ public void DrawPixel(int x, int y, byte brightness)
y = 7 - y;
}

//Swap
// Swap
(y, x) = (x, y);

iS31FL3731.SetLedPwm(Frame, (byte)(x + y * 16), brightness);
iS31Fl3731.SetLedPwm(Frame, (byte)(x + y * 16), brightness);
}

/// <summary>
Expand All @@ -125,7 +134,9 @@ public void DrawPixel(int x, int y, byte brightness)
/// <param name="y">The y position in pixels 0 indexed from the top</param>
public void InvertPixel(int x, int y)
{
throw new NotImplementedException();
byte currentBrightness = iS31Fl3731.ReadLedPwm(Frame, (byte)(x + y * 16));
byte invertedBrightness = (byte)(255 - currentBrightness);
DrawPixel(x, y, invertedBrightness);
}

/// <summary>
Expand Down Expand Up @@ -185,7 +196,7 @@ public void Fill(int x, int y, int width, int height, Color fillColor)
/// </summary>
public void Show()
{
iS31FL3731.DisplayFrame(Frame);
iS31Fl3731.DisplayFrame(Frame);
}

/// <summary>
Expand All @@ -201,12 +212,12 @@ public void Show(int left, int top, int right, int bottom)
}

/// <summary>
/// Update the display from a specific iS31FL3731 frame
/// Update the display from a specific Is31fl3731 frame
/// </summary>
/// <param name="frame">The frame to show (0-7)</param>
public void Show(byte frame)
{
iS31FL3731.DisplayFrame(frame);
iS31Fl3731.DisplayFrame(frame);
}
}
}
}

0 comments on commit e88febc

Please sign in to comment.