Skip to content

Commit

Permalink
Merge pull request #831 from WildernessLabs/more-warnings
Browse files Browse the repository at this point in the history
Warnings and null validation
  • Loading branch information
jorgedevs authored Nov 7, 2023
2 parents 76ec47e + d911c98 commit 4df3097
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class InputBase : IMenuInputItem
/// <summary>
/// The ITextDisplay object
/// </summary>
protected ITextDisplay? display = null;
protected ITextDisplay display;

/// <summary>
/// Is the item initialized
Expand Down Expand Up @@ -58,9 +58,9 @@ public void Init(ITextDisplay display)
/// <param name="text">The new text to display</param>
protected void UpdateInputLine(string text)
{
display?.ClearLine(1);
display?.WriteLine(text, 1, true);
display?.Show();
display.ClearLine(1);
display.WriteLine(text, 1, true);
display.Show();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class MenuItem
/// The menu item value
/// </summary>
[JsonPropertyName("value")]
public object Value { get; set; }
public object? Value { get; set; }

/// <summary>
/// Does the item have sub items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public class TextDisplayMenu
public TextDisplayMenu(ITextDisplay display, byte[] menuJson, bool showBackOnRoot = false)
{
this.showBackOnRoot = showBackOnRoot;
var items = ParseMenuData(menuJson);

var items = ParseMenuData(menuJson) ?? throw new ArgumentNullException("Menu data is null");
Init(display, CreateMenuPage(items, showBackOnRoot));
}

Expand Down Expand Up @@ -208,16 +207,23 @@ protected string GetItemText(MenuItem item, bool isSelected)
}
}

itemText = displayText.Substring(0, (displayText.Length >= display.DisplayConfig.Width - 1) ? display.DisplayConfig.Width - 1 : displayText.Length);

if (isSelected || item.HasSubItems)
if (display == null)
{
// calculate any necessary padding to put selector on far right
int paddingLength = (display.DisplayConfig.Width / display.DisplayConfig.FontScale - 1 - displayText.Length);
string padding = string.Empty;
if (paddingLength > 0) { padding = new string(' ', paddingLength); }
itemText = displayText;
}
else
{
itemText = displayText.Substring(0, (displayText.Length >= display.DisplayConfig.Width - 1) ? display.DisplayConfig.Width - 1 : displayText.Length);

if (isSelected || item.HasSubItems)
{
// calculate any necessary padding to put selector on far right
int paddingLength = (display.DisplayConfig.Width / display.DisplayConfig.FontScale - 1 - displayText.Length);
string padding = string.Empty;
if (paddingLength > 0) { padding = new string(' ', paddingLength); }

itemText += padding + (isSelected ? "*" : ">");
itemText += padding + (isSelected ? "*" : ">");
}
}

return itemText;
Expand Down Expand Up @@ -288,9 +294,9 @@ public bool Back()
currentInputItem.Back();
return true;
}
else if (pageStack.Count > 0)
else if (pageStack?.Count > 0)
{
MenuPage parent = pageStack.Pop() as MenuPage;
MenuPage? parent = pageStack?.Pop() as MenuPage;
currentMenuPage = parent;
ShowCurrentPage();
return true;
Expand All @@ -316,13 +322,17 @@ public bool Select()
currentInputItem.Select();
return true;
}
else if (currentMenuPage == null)
{
return false;
}
else
{
currentMenuPage.Select();
currentMenuPage!.Select();
}

if (currentMenuPage.ScrollPosition == 0
&& pageStack.Count == 0
&& pageStack?.Count == 0
&& showBackOnRoot
&& Exited != null)
{
Expand All @@ -332,9 +342,9 @@ public bool Select()
}

// if currently on a subpage and user selects back, pop back to parent page.
if (currentMenuPage.ScrollPosition == 0 && pageStack.Count > 0)
if (currentMenuPage.ScrollPosition == 0 && pageStack?.Count > 0)
{
MenuPage parent = pageStack.Pop() as MenuPage;
MenuPage? parent = pageStack.Pop() as MenuPage;
currentMenuPage = parent;
ShowCurrentPage();
return true;
Expand All @@ -346,49 +356,53 @@ public bool Select()
// go to the submenu if children are present
if (menuItem.HasSubItems)
{
pageStack.Push(currentMenuPage);
pageStack?.Push(currentMenuPage);
// currentMenuPage = child.SubMenu;
currentMenuPage = CreateMenuPage(menuItem.SubItems, true);
ShowCurrentPage();
return true;
}
// if there is a command, notify the subscribers
else if (menuItem.Command != string.Empty)
else if (menuItem?.Command != string.Empty)
{
Selected?.Invoke(this, new MenuSelectedEventArgs(menuItem.Command));
Selected?.Invoke(this, new MenuSelectedEventArgs(menuItem!.Command));
return true;
}
// if there is a type, then let the type handle the input
else if (menuItem.Type != string.Empty)
{
pageStack.Push(currentMenuPage);
pageStack?.Push(currentMenuPage);

isEditMode = true;

currentInputItem = GetMenuInputItemFromName(menuItem.Type);

// setup callback
currentInputItem.ValueChanged += delegate (object sender, ValueChangedEventArgs e)
if (currentInputItem != null)
{
// set the value and notify the eager listeners
menuItem.Value = e.Value;
ValueChanged?.Invoke(this, new ValueChangedEventArgs(menuItem.Id, menuItem.Value));

// reload the parent menu
var parent = pageStack.Pop() as MenuPage;
currentMenuPage = parent;
currentInputItem = null;
ShowCurrentPage();
isEditMode = false;
};
currentInputItem.ValueChanged += delegate (object sender, ValueChangedEventArgs e)
{
// set the value and notify the eager listeners
menuItem.Value = e.Value;
ValueChanged?.Invoke(this, new ValueChangedEventArgs(menuItem.Id, menuItem.Value));

// reload the parent menu
var parent = pageStack?.Pop() as MenuPage;
currentMenuPage = parent;
currentInputItem = null;
ShowCurrentPage();
isEditMode = false;
};
}


// initialize input mode and get new value
if (display != null)
if (currentInputItem != null && display != null)
{
currentInputItem.Init(display);
}

currentInputItem.GetInput(menuItem.Id, menuItem.Value);
currentInputItem?.GetInput(menuItem.Id, menuItem.Value);
return true;
}
else
Expand Down Expand Up @@ -462,7 +476,7 @@ public void UpdateItemValue(Hashtable values)
private void UpdateMenuItemValue(string id, object value)
{
MenuItem? node = null;
foreach (var menuItem in rootMenuPage.MenuItems)
foreach (var menuItem in rootMenuPage!.MenuItems)
{
node = FindNodeById(menuItem, id);
if (node != null) { break; }
Expand All @@ -484,7 +498,7 @@ private void UpdateMenuItemValue(string id, object value)
{
return menuItem;
}
else if (menuItem.SubItems.Length > 0)
else if (menuItem != null && menuItem.SubItems?.Length > 0)
{
foreach (var subMenuItem in menuItem.SubItems)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public int BitDepth
/// </summary>
public PixelBufferBase()
{
Buffer = new byte[0];
}

/// <summary>
Expand Down Expand Up @@ -113,7 +114,7 @@ public PixelBufferBase(int width, int height, byte[] buffer)
/// <param name="replaceIfExists">If true, will recreates the buffer if it already exists</param>
public void InitializeBuffer(bool replaceIfExists = false)
{
if (Buffer == null || replaceIfExists)
if (Buffer == null || Buffer.Length == 0 || replaceIfExists)
{
Buffer = new byte[ByteCount];
}
Expand Down Expand Up @@ -357,7 +358,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 @@ -57,7 +57,7 @@ public class GraphicsPath
/// <summary>
/// The collection of points
/// </summary>
public Point[] Points;
public Point[]? Points;

/// <summary>
/// The number of verbs/actions used
Expand All @@ -76,6 +76,11 @@ public Rect Bounds
{
get
{
if (Points == null)
{
return new Rect(0, 0, 0, 0);
}

Point min = Points[0];
Point max = Points[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CharacterDisplay : ITextDisplay
/// <summary>
/// The display configuration for text display menu
/// </summary>
public TextDisplayConfig? DisplayConfig => characterDisplay?.DisplayConfig;
public TextDisplayConfig DisplayConfig => characterDisplay!.DisplayConfig;

/// <summary>
/// Create a new character display object using GPIO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public abstract partial class Ssd130xBase : IGraphicsDisplay, ISpiPeripheral, II
/// <summary>
/// The width of the display in pixels
/// </summary>
public int Width => imageBuffer.Width;
public int Width => imageBuffer!.Width;

/// <summary>
/// The height of the display in pixels
/// </summary>
public int Height => imageBuffer.Height;
public int Height => imageBuffer!.Height;

/// <summary>
/// The buffer the holds the pixel data for the display
Expand Down Expand Up @@ -122,7 +122,7 @@ public SpiClockConfiguration.Mode SpiBusMode
/// <summary>
/// Read buffer
/// </summary>
protected byte[] readBuffer;
protected byte[]? readBuffer;

/// <summary>
/// Display command buffer
Expand All @@ -132,12 +132,12 @@ public SpiClockConfiguration.Mode SpiBusMode
/// <summary>
/// Page buffer to hold one page of data
/// </summary>
protected byte[] pageBuffer;
protected byte[]? pageBuffer;

/// <summary>
/// Sequence of command bytes that must be sent to the display before
/// </summary>
protected byte[] showPreamble;
protected byte[]? showPreamble;

/// <summary>
/// Invert the entire display (true) or return to normal mode (false)
Expand Down Expand Up @@ -255,7 +255,7 @@ public void Show()
else// I2C
{ // Send the buffer page by page
// This can be optimized when we move to Memory<byte>
pageBuffer[0] = 0x40;
pageBuffer![0] = 0x40;

for (ushort index = 0; index < imageBuffer.ByteCount; index += PAGE_SIZE)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public partial class As1115 : IGraphicsDisplay, II2cPeripheral, IDisposable
/// <summary>
/// Readonly collection that contains all 16 key scan button objects
/// </summary>
public ReadOnlyDictionary<KeyScanButtonType, KeyScanButton> KeyScanButtons { get; protected set; }
public ReadOnlyDictionary<KeyScanButtonType, KeyScanButton>? KeyScanButtons { get; protected set; }

/// <summary>
/// Helper method to get IButton object references for keyscan buttons
/// </summary>
/// <param name="buttonType">The button type</param>
/// <returns>The button object reference</returns>
public IButton GetButton(KeyScanButtonType buttonType) => KeyScanButtons[buttonType];
public IButton GetButton(KeyScanButtonType buttonType) => KeyScanButtons![buttonType];

/// <summary>
/// Last button pressed, used internally to raise key up events
Expand Down Expand Up @@ -148,7 +148,7 @@ private void InterruptPort_Changed(object sender, DigitalPortResult e)
{
KeyScanPressStarted?.Invoke(this, new KeyScanEventArgs(keyScanButton, data[0], data[1]));

KeyScanButtons[keyScanButton].Update(true);
KeyScanButtons![keyScanButton].Update(true);

lastButtonPressed = keyScanButton;
}
Expand All @@ -158,7 +158,7 @@ private void InterruptPort_Changed(object sender, DigitalPortResult e)

if (lastButtonPressed != KeyScanButtonType.None)
{
KeyScanButtons[lastButtonPressed].Update(false);
KeyScanButtons![lastButtonPressed].Update(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Sc16is7x2Channel : ISerialPort
{
/// <inheritdoc/>
public event SerialDataReceivedEventHandler DataReceived = default!;

/// <inheritdoc/>
public event EventHandler BufferOverrun = default!;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class BiDirectionalPort : BiDirectionalPortBase
/// <summary>
/// Serial Wombat controller
/// </summary>
private readonly SerialWombatBase _controller;
private readonly SerialWombatBase? _controller;

/// <summary>
/// The port direction
Expand All @@ -36,8 +36,8 @@ protected BiDirectionalPort(
/// </summary>
public override bool State
{
get => _controller.ReadPublicData((byte)Pin.Key) != 0;
set => _controller.ConfigureOutputPin((byte)Pin.Key, value, OutputType.PushPull);
get => _controller!.ReadPublicData((byte)Pin.Key) != 0;
set => _controller!.ConfigureOutputPin((byte)Pin.Key, value, OutputType.PushPull);
}
}
}
Expand Down
Loading

0 comments on commit 4df3097

Please sign in to comment.