Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #821

Merged
merged 8 commits into from
Oct 30, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SpiCommunications : ISpiCommunications
/// <summary>
/// The SPI chip select port
/// </summary>
public IDigitalOutputPort? ChipSelect { get; }
public IDigitalOutputPort ChipSelect { get; }

/// <summary>
/// The chip select mode (active high or active low)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class AdafruitIO
/// <param name="userName">Adafruit username</param>
/// <param name="iokey">Write key</param>
/// <param name="group">group</param>
public AdafruitIO(string userName, string iokey, string group = null)
public AdafruitIO(string userName, string iokey, string group = "")
{
Group = group;
UserName = userName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class TimeBase : InputBase
/// Raised if the input value changes
/// </summary>

public override event ValueChangedHandler ValueChanged;
public override event ValueChangedHandler ValueChanged = delegate { };

/// <summary>
/// Create a new TimeBase object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public int BitDepth
/// <summary>
/// Create a new PixelBufferBase object
/// </summary>
public PixelBufferBase() { }
public PixelBufferBase()
{
Buffer = new byte[0];
}

/// <summary>
/// Create a new PixelBufferBase object
Expand All @@ -82,6 +85,7 @@ public PixelBufferBase(int width, int height)
{
Width = width;
Height = height;
Buffer = new byte[0];
InitializeBuffer();
}

Expand Down Expand Up @@ -355,7 +359,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Buffer = null;
Buffer = new byte[0];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public partial class Image
/// <summary>
/// The image width in pixels
/// </summary>
public int Width => DisplayBuffer.Width;
public int Width => DisplayBuffer?.Width ?? 0;

/// <summary>
/// The image height in pixels
/// </summary>
public int Height => DisplayBuffer.Height;
public int Height => DisplayBuffer?.Height ?? 0;

/// <summary>
/// The image bits per pixel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ public MicroGraphics(IGraphicsDisplay display)
/// </summary>
/// <param name="pixelBuffer">The pixel buffer</param>
/// <param name="initializeBuffer">Initialize the off-screen buffer if true</param>
public MicroGraphics(PixelBufferBase pixelBuffer, bool initializeBuffer)
public MicroGraphics(IPixelBuffer pixelBuffer, bool initializeBuffer)
{
memoryBuffer = pixelBuffer;

if (initializeBuffer)
if (initializeBuffer && pixelBuffer is PixelBufferBase buf)
{
pixelBuffer.InitializeBuffer();
buf.InitializeBuffer();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public override Task Initialize()
colorMode: ColorMode.Format16bppRgb565
);

screen = new DisplayScreen(display, Meadow.Foundation.Graphics.RotationType._270Degrees);

screen.BackgroundColor = Color.Black;
screen = new DisplayScreen(display, RotationType._270Degrees)
{
BackgroundColor = Color.Black
};

return base.Initialize();
}
Expand All @@ -42,8 +43,8 @@ public void Text()
{
var label = new Label(0, 0, screen.Width, screen.Height);
label.Font = new Font12x20();
label.HorizontalAlignment = Meadow.Foundation.Graphics.HorizontalAlignment.Center;
label.VerticalAlignment = Meadow.Foundation.Graphics.VerticalAlignment.Center;
label.HorizontalAlignment = HorizontalAlignment.Center;
label.VerticalAlignment = VerticalAlignment.Center;
label.TextColor = Color.Red;
label.Text = "HELLO";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

public class MeadowApp : App<Windows>
{
private readonly Ft232h _expander = new Ft232h();
private DisplayScreen _screen;
private readonly Ft232h expander = new Ft232h();
private DisplayScreen screen;

public override Task Initialize()
{
var display = new Max7219(
_expander.CreateSpiBus(),
_expander.Pins.C0.CreateDigitalOutputPort(), // CS
expander.CreateSpiBus(),
expander.Pins.C0.CreateDigitalOutputPort(), // CS
deviceRows: 4,
deviceColumns: 1);

_screen = new DisplayScreen(display, Meadow.Foundation.Graphics.RotationType._270Degrees);
_screen.BackgroundColor = Color.Black;
screen = new DisplayScreen(display, Meadow.Foundation.Graphics.RotationType._270Degrees);
screen.BackgroundColor = Color.Black;

return base.Initialize();
}
Expand All @@ -33,27 +33,27 @@ public override Task Run()

public void Text()
{
var label = new Label(0, 0, _screen.Width, _screen.Height);
var label = new Label(0, 0, screen.Width, screen.Height);
label.HorizontalAlignment = Meadow.Foundation.Graphics.HorizontalAlignment.Center;
label.VerticalAlignment = Meadow.Foundation.Graphics.VerticalAlignment.Center;
label.TextColor = Color.White;
label.Text = "HELLO";

_screen.Controls.Add(label);
screen.Controls.Add(label);

}

public void TextOnBox()
{
var box = new Box(0, 0, _screen.Width / 4, _screen.Height);
var box = new Box(0, 0, screen.Width / 4, screen.Height);
box.ForeColor = Color.Red;
var label = new Label(0, 0, _screen.Width / 4, _screen.Height);
var label = new Label(0, 0, screen.Width / 4, screen.Height);
label.HorizontalAlignment = Meadow.Foundation.Graphics.HorizontalAlignment.Center;
label.VerticalAlignment = Meadow.Foundation.Graphics.VerticalAlignment.Center;
label.TextColor = Color.Black;
label.Text = "Meadow";

_screen.Controls.Add(box, label);
screen.Controls.Add(box, label);

while (true)
{
Expand All @@ -66,10 +66,10 @@ public void TextOnBox()

public void Sweep()
{
var box = new Box(0, 0, _screen.Width / 4, _screen.Height);
var box = new Box(0, 0, screen.Width / 4, screen.Height);
box.ForeColor = Color.Red;

_screen.Controls.Add(box);
screen.Controls.Add(box);

var direction = 1;
var speed = 1;
Expand All @@ -80,7 +80,7 @@ public void Sweep()

box.Left = left;

if ((box.Right >= _screen.Width) || box.Left <= 0)
if ((box.Right >= screen.Width) || box.Left <= 0)
{
direction *= -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public SpiClockConfiguration.Mode SpiBusMode
/// <param name="resetPin">Reset pin</param>
/// <param name="width">Width of display in pixels</param>
/// <param name="height">Height of display in pixels</param>
public Ch1115(ISpiBus spiBus, IPin chipSelectPin, IPin dcPin, IPin resetPin,
public Ch1115(ISpiBus spiBus, IPin? chipSelectPin, IPin dcPin, IPin resetPin,
int width = 128, int height = 64) :
this(spiBus, chipSelectPin?.CreateDigitalOutputPort(), dcPin.CreateDigitalOutputPort(),
this(spiBus, chipSelectPin?.CreateDigitalOutputPort() ?? null, dcPin.CreateDigitalOutputPort(),
resetPin.CreateDigitalOutputPort(), width, height)
{
}
Expand All @@ -105,7 +105,7 @@ public Ch1115(ISpiBus spiBus, IPin chipSelectPin, IPin dcPin, IPin resetPin,
/// <param name="width">Width of display in pixels</param>
/// <param name="height">Height of display in pixels</param>
public Ch1115(ISpiBus spiBus,
IDigitalOutputPort chipSelectPort,
IDigitalOutputPort? chipSelectPort,
IDigitalOutputPort dataCommandPort,
IDigitalOutputPort resetPort,
int width = 128, int height = 64)
Expand Down
19 changes: 15 additions & 4 deletions Source/Meadow.Foundation.Peripherals/Displays.Gtk/Driver/Gtk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class GtkDisplay : IGraphicsDisplay, ITouchScreen
private bool _leftButtonState = false;

private EventWaitHandle ShowComplete { get; } = new EventWaitHandle(true, EventResetMode.ManualReset);

/// <inheritdoc/>
public IPixelBuffer PixelBuffer => _pixelBuffer;

/// <summary>
Expand Down Expand Up @@ -60,14 +62,20 @@ static GtkDisplay()
}

/// <summary>
/// Create a new WinFormsDisplay with a default size of 800x600
/// Create a new GTK Display with a default size of 800x600
/// </summary>
/// <param name="mode">Color mode of the display</param>
public GtkDisplay(ColorMode mode = ColorMode.Format24bppRgb888)
{
Initialize(800, 600, mode); // TODO: query screen size and caps
}

/// <summary>
/// Create a new GTK Display
/// </summary>
/// <param name="width">Width of display in pixels</param>
/// <param name="height">Height of display in pixels</param>
/// <param name="mode">Color mode of the display</param>
public GtkDisplay(int width, int height, ColorMode mode = ColorMode.Format24bppRgb888)
{
Initialize(width, height, mode);
Expand All @@ -76,7 +84,7 @@ public GtkDisplay(int width, int height, ColorMode mode = ColorMode.Format24bppR
private void Initialize(int width, int height, ColorMode mode)
{
_window = new Window(WindowType.Popup);
_window.WidgetEvent += _window_WidgetEvent;
_window.WidgetEvent += WindowWidgetEvent;

switch (mode)
{
Expand Down Expand Up @@ -121,7 +129,7 @@ private void RaiseTouchUp(double x, double y)

}

private void _window_WidgetEvent(object o, WidgetEventArgs args)
private void WindowWidgetEvent(object o, WidgetEventArgs args)
{
if (args.Event is Gdk.EventButton b)
{
Expand All @@ -142,6 +150,9 @@ private void _window_WidgetEvent(object o, WidgetEventArgs args)
}
}

/// <summary>
/// Run the application
/// </summary>
public void Run()
{
Application.Run();
Expand Down Expand Up @@ -276,7 +287,7 @@ public void WriteBuffer(int x, int y, IPixelBuffer displayBuffer)
private int GetStrideForBitDepth(int width, int bpp)
{
var cairo_stride_alignment = sizeof(UInt32);
var stride = ((((bpp) * (width) + 7) / 8 + cairo_stride_alignment - 1) & -cairo_stride_alignment);
var stride = ((bpp * width + 7) / 8 + cairo_stride_alignment - 1) & -cairo_stride_alignment;
return stride;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Meadow.Foundation.Displays.Led
/// </summary>
public class FourDigitSevenSegment
{
CancellationTokenSource cts = null;
CancellationTokenSource? cts = null;

readonly IDigitalOutputPort[] digits;
readonly SevenSegment[] sevenSegments;
Expand Down Expand Up @@ -111,7 +111,7 @@ public void SetDisplay(string characters, int decimalLocation = -1)
/// <param name="decimalLocation">The decimal position (0 indexed)</param>
public void SetDisplay(char[] characters, int decimalLocation = -1)
{
if (!cts.Token.IsCancellationRequested)
if (cts != null && !cts.Token.IsCancellationRequested)
{
cts.Cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public SpiClockConfiguration.Mode SpiBusMode
/// <param name="chipSelectPin">Chip select pin</param>
/// <param name="dcPin">Data command pin</param>
/// <param name="resetPin">Reset pin</param>
public Pcd8544(ISpiBus spiBus, IPin chipSelectPin, IPin dcPin, IPin resetPin) :
this(spiBus, chipSelectPin?.CreateDigitalOutputPort(), dcPin.CreateDigitalOutputPort(true),
public Pcd8544(ISpiBus spiBus, IPin? chipSelectPin, IPin dcPin, IPin resetPin) :
this(spiBus, chipSelectPin?.CreateDigitalOutputPort() ?? null, dcPin.CreateDigitalOutputPort(true),
resetPin.CreateDigitalOutputPort(true))
{
}
Expand All @@ -108,7 +108,7 @@ public Pcd8544(ISpiBus spiBus, IPin chipSelectPin, IPin dcPin, IPin resetPin) :
/// <param name="dataCommandPort">Data command output port</param>
/// <param name="resetPort">Reset output port</param>
public Pcd8544(ISpiBus spiBus,
IDigitalOutputPort chipSelectPort,
IDigitalOutputPort? chipSelectPort,
IDigitalOutputPort dataCommandPort,
IDigitalOutputPort resetPort)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public Sh110x(II2cBus i2cBus, byte address, int width, int height)
/// <param name="resetPin">Reset pin</param>
/// <param name="width">Display width in pixels</param>
/// <param name="height">Display height in pixels</param>
public Sh110x(ISpiBus spiBus, IPin chipSelectPin, IPin dcPin, IPin resetPin, int width, int height) :
this(spiBus, chipSelectPin?.CreateDigitalOutputPort(), dcPin.CreateDigitalOutputPort(),
public Sh110x(ISpiBus spiBus, IPin? chipSelectPin, IPin dcPin, IPin resetPin, int width, int height) :
this(spiBus, chipSelectPin?.CreateDigitalOutputPort() ?? null, dcPin.CreateDigitalOutputPort(),
resetPin.CreateDigitalOutputPort(), width, height)
{
}
Expand All @@ -151,7 +151,7 @@ public Sh110x(ISpiBus spiBus, IPin chipSelectPin, IPin dcPin, IPin resetPin, int
/// <param name="width">Display width in pixels</param>
/// <param name="height">Display height in pixels</param>
public Sh110x(ISpiBus spiBus,
IDigitalOutputPort chipSelectPort,
IDigitalOutputPort? chipSelectPort,
IDigitalOutputPort dataCommandPort,
IDigitalOutputPort resetPort,
int width, int height)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public partial class Ssd1327 : IGraphicsDisplay, ISpiPeripheral
/// <summary>
/// The default SPI bus speed for the device
/// </summary>
public Frequency DefaultSpiBusSpeed => new Frequency(10000, Frequency.UnitType.Kilohertz);
public Frequency DefaultSpiBusSpeed => new(10000, Frequency.UnitType.Kilohertz);

/// <summary>
/// The SPI bus speed for the device
Expand Down Expand Up @@ -71,7 +71,7 @@ public SpiClockConfiguration.Mode SpiBusMode
protected ISpiCommunications spiComms;

readonly IDigitalOutputPort dataCommandPort;
readonly IDigitalOutputPort resetPort;
readonly IDigitalOutputPort? resetPort;

readonly BufferGray4 imageBuffer;

Expand All @@ -85,8 +85,8 @@ public SpiClockConfiguration.Mode SpiBusMode
/// <param name="chipSelectPin">Chip select pin</param>
/// <param name="dcPin">Data command pin</param>
/// <param name="resetPin">Reset pin</param>
public Ssd1327(ISpiBus spiBus, IPin chipSelectPin, IPin dcPin, IPin resetPin)
: this(spiBus, chipSelectPin?.CreateDigitalOutputPort(false), dcPin?.CreateDigitalOutputPort(false), resetPin?.CreateDigitalOutputPort(true))
public Ssd1327(ISpiBus spiBus, IPin? chipSelectPin, IPin dcPin, IPin? resetPin)
: this(spiBus, chipSelectPin?.CreateDigitalOutputPort(false) ?? null, dcPin.CreateDigitalOutputPort(false), resetPin?.CreateDigitalOutputPort(true) ?? null)
{
}

Expand All @@ -98,9 +98,9 @@ public Ssd1327(ISpiBus spiBus, IPin chipSelectPin, IPin dcPin, IPin resetPin)
/// <param name="dataCommandPort">Data command output port</param>
/// <param name="resetPort">Reset output port</param>
public Ssd1327(ISpiBus spiBus,
IDigitalOutputPort chipSelectPort,
IDigitalOutputPort? chipSelectPort,
IDigitalOutputPort dataCommandPort,
IDigitalOutputPort resetPort)
IDigitalOutputPort? resetPort)
{
imageBuffer = new BufferGray4(Width, Height);

Expand Down
Loading