Skip to content

Commit

Permalink
#363 - Added check int the UI/Settings/MobiFlight Dialog for max numb…
Browse files Browse the repository at this point in the history
…er of devices that the firmware can support.
  • Loading branch information
DocMoebiuz committed Jul 3, 2021
1 parent 53272b8 commit c7ad037
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 1 deletion.
19 changes: 19 additions & 0 deletions MobiFlight/MaximumDeviceNumberReachedMobiFlightException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MobiFlight
{
class MaximumDeviceNumberReachedMobiFlightException : Exception
{
public String DeviceType;
public int MaxNumber;
public MaximumDeviceNumberReachedMobiFlightException(String DeviceType, int MaxNumber)
: base(String.Format("Max number of {0} is {1}", DeviceType, MaxNumber))
{
this.DeviceType = DeviceType;
this.MaxNumber = MaxNumber;
}
}
}
1 change: 1 addition & 0 deletions MobiFlight/MobiFlightButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace MobiFlight
{
class MobiFlightButton : IConnectedDevice
{
public const string TYPE = "Button";
public enum InputEvent
{
PRESS,
Expand Down
1 change: 1 addition & 0 deletions MobiFlight/MobiFlightEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace MobiFlight
{
class MobiFlightEncoder : IConnectedDevice
{
public const string TYPE = "Encoder";
public enum InputEvent
{
LEFT,
Expand Down
26 changes: 26 additions & 0 deletions MobiFlight/MobiFlightModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public Config.Config Config {
Dictionary<String, MobiFlightServo> servoModules = new Dictionary<string, MobiFlightServo>();
Dictionary<String, MobiFlightOutput> outputs = new Dictionary<string,MobiFlightOutput>();
Dictionary<String, MobiFlightLcdDisplay> lcdDisplays = new Dictionary<string, MobiFlightLcdDisplay>();
Dictionary<String, MobiFlightButton> buttons = new Dictionary<string, MobiFlightButton>();
Dictionary<String, MobiFlightEncoder> encoders = new Dictionary<string, MobiFlightEncoder>();

Dictionary<String, int> buttonValues = new Dictionary<String, int>();

Expand Down Expand Up @@ -285,6 +287,8 @@ public void LoadConfig()
servoModules.Clear();
outputs.Clear();
lcdDisplays.Clear();
buttons.Clear();
encoders.Clear();

foreach (Config.BaseDevice device in Config.Items)
{
Expand Down Expand Up @@ -319,6 +323,14 @@ public void LoadConfig()
device.Name = GenerateUniqueDeviceName(outputs.Keys.ToArray(), device.Name);
lcdDisplays.Add(device.Name, new MobiFlightLcdDisplay() { CmdMessenger = _cmdMessenger, Name = device.Name, Address = lcdDisplays.Count, Cols = (device as Config.LcdDisplay).Cols, Lines = (device as Config.LcdDisplay).Lines });
break;
case DeviceType.Button:
device.Name = GenerateUniqueDeviceName(outputs.Keys.ToArray(), device.Name);
buttons.Add(device.Name, new MobiFlightButton() { Name = device.Name });
break;
case DeviceType.Encoder:
device.Name = GenerateUniqueDeviceName(outputs.Keys.ToArray(), device.Name);
encoders.Add(device.Name, new MobiFlightEncoder() { Name = device.Name });
break;
}
}
}
Expand Down Expand Up @@ -771,6 +783,20 @@ public List<IConnectedDevice> GetConnectedDevices()
return result;
}

public Dictionary<String, int> GetConnectedDevicesStatistics()
{
Dictionary<String, int> result = new Dictionary<string, int>();
result[MobiFlightOutput.TYPE] = outputs.Count;
result[MobiFlightLedModule.TYPE] = ledModules.Count;
result[MobiFlightStepper.TYPE] = stepperModules.Count;
result[MobiFlightServo.TYPE] = servoModules.Count;
result[MobiFlightLcdDisplay.TYPE] = lcdDisplays.Count;
result[MobiFlightButton.TYPE] = buttons.Count;
result[MobiFlightEncoder.TYPE] = encoders.Count;

return result;
}

public List<IConnectedDevice> GetConnectedDevices(String name)
{
List<IConnectedDevice> result = new List<IConnectedDevice>();
Expand Down
75 changes: 75 additions & 0 deletions MobiFlight/MobiFlightModuleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,59 @@

namespace MobiFlight
{
public abstract class MobiFlightCapabilities
{
public int MaxOutputs = 0;
public int MaxButtons = 0;
public int MaxLedSegments = 0;
public int MaxEncoders = 0;
public int MaxSteppers = 0;
public int MaxServos = 0;
public int MaxLcdI2C = 0;
}

public class MegaCapabilities : MobiFlightCapabilities
{
public MegaCapabilities()
{
MaxOutputs = 40;
MaxButtons = 68;
MaxLedSegments = 4;
MaxEncoders = 20;
MaxSteppers = 10;
MaxServos = 10;
MaxLcdI2C = 2;
}
}

public class MicroCapabilities : MobiFlightCapabilities
{
public MicroCapabilities()
{
MaxOutputs = 10;
MaxButtons = 16;
MaxLedSegments = 1;
MaxEncoders = 4;
MaxSteppers = 2;
MaxServos = 2;
MaxLcdI2C = 2;
}
}

public class UnoCapabilities : MobiFlightCapabilities
{
public UnoCapabilities()
{
MaxOutputs = 8;
MaxButtons = 8;
MaxLedSegments =1;
MaxEncoders = 2;
MaxSteppers = 2;
MaxServos = 2;
MaxLcdI2C = 2;
}
}

public class MobiFlightPin
{
public byte Pin { get; set; }
Expand Down Expand Up @@ -284,5 +337,27 @@ public void SetTypeByVidPid(String VidPid)
Type = TYPE_ARDUINO_UNO;
}
}

public MobiFlightCapabilities GetCapabilities()
{
MobiFlightCapabilities result = null;

switch(Type)
{
case TYPE_MEGA:
result = new MegaCapabilities();
break;

case TYPE_MICRO:
result = new MicroCapabilities();
break;

case TYPE_UNO:
result = new UnoCapabilities();
break;
}

return result;
}
}
}
1 change: 1 addition & 0 deletions MobiFlightConnector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
<Compile Include="Base\SerialNumber.cs" />
<Compile Include="Base\WriteCacheInterface.cs" />
<Compile Include="MobiFlight\Config\IConfigRefConfigItem.cs" />
<Compile Include="MobiFlight\MaximumDeviceNumberReachedMobiFlightException.cs" />
<Compile Include="MobiFlight\InputConfig\MSFS2020EventIdInputAction.cs" />
<Compile Include="MobiFlight\MobiFlightButton.cs" />
<Compile Include="MobiFlight\MobiFlightEncoder.cs" />
Expand Down
9 changes: 9 additions & 0 deletions ProjectMessages/ProjectMessages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ProjectMessages/ProjectMessages.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,7 @@ Du kannst es jederzeit wieder in den Einstellungen aktivieren </value>
<data name="uiMessageSimConnectConnectionLost" xml:space="preserve">
<value>Die Verbindung zu SimConnect wurde unterbrochen.</value>
</data>
<data name="uiMessageMaxNumberOfDevicesReached" xml:space="preserve">
<value>Du kannst kein weiteres {0} hinzufügen. Die maximale Anzahl sind {1} bei einem {2}.</value>
</data>
</root>
3 changes: 3 additions & 0 deletions ProjectMessages/ProjectMessages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,7 @@ You can always enable it later again in the settings menu.</value>
<data name="uiMessageSimConnectConnectionLost" xml:space="preserve">
<value>The connection to SimConnect got lost.</value>
</data>
<data name="uiMessageMaxNumberOfDevicesReached" xml:space="preserve">
<value>You cannot add another {1}. The maximum is {0} on a {2}.</value>
</data>
</root>
49 changes: 48 additions & 1 deletion UI/Panels/Settings/MobiFlightPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,34 @@ private void syncPanelWithSelectedDevice(TreeNode selectedNode)
/// <param name="e"></param>
private void addDeviceTypeToolStripMenuItem_Click(object sender, EventArgs e)
{
MobiFlight.Config.BaseDevice cfgItem = null;
MobiFlightModule tempModule = null;
try
{
MobiFlight.Config.BaseDevice cfgItem = null;
cfgItem = null;
tempModule = getVirtualModuleFromTree();
tempModule.LoadConfig();
Dictionary<String, int> statistics = tempModule.GetConnectedDevicesStatistics();

switch ((sender as ToolStripMenuItem).Name)
{
case "servoToolStripMenuItem":
case "addServoToolStripMenuItem":
if (statistics[MobiFlightServo.TYPE] == tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxServos)
{
throw new MaximumDeviceNumberReachedMobiFlightException(MobiFlightServo.TYPE, tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxServos);
}

cfgItem = new MobiFlight.Config.Servo();
(cfgItem as MobiFlight.Config.Servo).DataPin = getVirtualModuleFromTree().GetFreePins().ElementAt(0).Pin.ToString();
break;
case "stepperToolStripMenuItem":
case "addStepperToolStripMenuItem":
if (statistics[MobiFlightStepper.TYPE] == tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxSteppers)
{
throw new MaximumDeviceNumberReachedMobiFlightException(MobiFlightStepper.TYPE, tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxSteppers);
}

cfgItem = new MobiFlight.Config.Stepper();
(cfgItem as MobiFlight.Config.Stepper).Pin1 = getVirtualModuleFromTree().GetFreePins().ElementAt(0).Pin.ToString();
(cfgItem as MobiFlight.Config.Stepper).Pin2 = getVirtualModuleFromTree().GetFreePins().ElementAt(1).Pin.ToString();
Expand All @@ -368,29 +384,54 @@ private void addDeviceTypeToolStripMenuItem_Click(object sender, EventArgs e)
break;
case "ledOutputToolStripMenuItem":
case "addOutputToolStripMenuItem":
if (statistics[MobiFlightOutput.TYPE] == tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxOutputs)
{
throw new MaximumDeviceNumberReachedMobiFlightException(MobiFlightOutput.TYPE, tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxOutputs);
}

cfgItem = new MobiFlight.Config.Output();
(cfgItem as MobiFlight.Config.Output).Pin = getVirtualModuleFromTree().GetFreePins().ElementAt(0).Pin.ToString();
break;
case "ledSegmentToolStripMenuItem":
case "addLedModuleToolStripMenuItem":
if (statistics[MobiFlightLedModule.TYPE] == tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxLedSegments)
{
throw new MaximumDeviceNumberReachedMobiFlightException(MobiFlightLedModule.TYPE, tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxLedSegments);
}

cfgItem = new MobiFlight.Config.LedModule();
(cfgItem as MobiFlight.Config.LedModule).DinPin = getVirtualModuleFromTree().GetFreePins().ElementAt(0).Pin.ToString();
(cfgItem as MobiFlight.Config.LedModule).ClkPin = getVirtualModuleFromTree().GetFreePins().ElementAt(1).Pin.ToString();
(cfgItem as MobiFlight.Config.LedModule).ClsPin = getVirtualModuleFromTree().GetFreePins().ElementAt(2).Pin.ToString();
break;
case "buttonToolStripMenuItem":
case "addButtonToolStripMenuItem":
if (statistics[MobiFlightButton.TYPE] == tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxButtons)
{
throw new MaximumDeviceNumberReachedMobiFlightException(MobiFlightButton.TYPE, tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxButtons);
}

cfgItem = new MobiFlight.Config.Button();
(cfgItem as MobiFlight.Config.Button).Pin = getVirtualModuleFromTree().GetFreePins().ElementAt(0).Pin.ToString();
break;
case "encoderToolStripMenuItem":
case "addEncoderToolStripMenuItem":
if (statistics[MobiFlightEncoder.TYPE] == tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxEncoders)
{
throw new MaximumDeviceNumberReachedMobiFlightException(MobiFlightEncoder.TYPE, tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxEncoders);
}

cfgItem = new MobiFlight.Config.Encoder();
(cfgItem as MobiFlight.Config.Encoder).PinLeft = getVirtualModuleFromTree().GetFreePins().ElementAt(0).Pin.ToString();
(cfgItem as MobiFlight.Config.Encoder).PinRight = getVirtualModuleFromTree().GetFreePins().ElementAt(1).Pin.ToString();
break;
case "LcdDisplayToolStripMenuItem":
case "addLcdDisplayToolStripMenuItem":
if (statistics[MobiFlightLcdDisplay.TYPE] == tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxLcdI2C)
{
throw new MaximumDeviceNumberReachedMobiFlightException(MobiFlightLcdDisplay.TYPE, tempModule.ToMobiFlightModuleInfo().GetCapabilities().MaxLcdI2C);
}

cfgItem = new MobiFlight.Config.LcdDisplay();
// does not deal yet with these kind of pins because we use I2C
break;
Expand Down Expand Up @@ -422,6 +463,12 @@ private void addDeviceTypeToolStripMenuItem_Click(object sender, EventArgs e)
mfModulesTreeView.SelectedNode = newNode;
syncPanelWithSelectedDevice(newNode);
}
catch (MaximumDeviceNumberReachedMobiFlightException ex)
{
MessageBox.Show(String.Format(i18n._tr("uiMessageMaxNumberOfDevicesReached"), ex.MaxNumber, ex.DeviceType, tempModule.Type),
i18n._tr("uiMessageNotEnoughPinsHint"),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentOutOfRangeException ex)
{
MessageBox.Show(i18n._tr("uiMessageNotEnoughPinsMessage"),
Expand Down

0 comments on commit c7ad037

Please sign in to comment.