Skip to content
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
98 changes: 70 additions & 28 deletions Base/ComboBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ static public bool BindMobiFlightFreePins(ComboBox comboBox, List<MobiFlightPin>
if (Pins == null) return false;
// Deep-clone list as 'Used' list
List<MobiFlightPin> UsablePins = Pins.ConvertAll(pin => new MobiFlightPin(pin));
// Mark current pin as free
if (UsablePins.Exists(x => x.Pin == byte.Parse(CurrentPin)))
// Mark current pin (if any specified) as free
if (CurrentPin != "" && UsablePins.Exists(x => x.Pin == byte.Parse(CurrentPin))) {
UsablePins.Find(x => x.Pin == byte.Parse(CurrentPin)).Used = false;
}

if (analogOnly == true)
if (analogOnly == true)
{
UsablePins = UsablePins.FindAll(x => x.isAnalog == true);
}
Expand All @@ -78,40 +79,81 @@ static public bool BindMobiFlightFreePins(ComboBox comboBox, List<MobiFlightPin>
comboBox.DisplayMember = "Name";
comboBox.ValueMember = "Pin";

// Restore the original item selection
comboBox.SelectedValue = byte.Parse(CurrentPin);
// Restore the original item selection (if any)
if (CurrentPin != "") {
var pinNo = byte.Parse(CurrentPin);
try {
comboBox.SelectedValue = pinNo;
}
catch { }
}

return false;
}
static public void reassignPin(ComboBox comboBox, List<MobiFlightPin> pinList, ref string signalPin)
static public void reassignPin(string newPin, List<MobiFlightPin> pinList, ref string currentPin)
{
// This function updates the config data (currentPin) with the new value passed.
// The assignment flags in the "base" pin list are accordingly updated
// (the current pin is marked as free and the new one as used)
try {
if (currentPin == newPin) return;
freePin(pinList, currentPin);
// Temporarily assign desired target - will remain this if available
string newCurrentPin = newPin;
assignPin(pinList, ref newCurrentPin);
// If no errors, confirm assignment of the new value in the configuration data
currentPin = newCurrentPin;
}
catch (Exception ex) {
Log.Instance.log($"Pin reassignment from {currentPin} to {newPin} went wrong: {ex.Message}", LogSeverity.Error);
}

}
static public void freePin(List<MobiFlightPin> pinList, string currentPin)
{
// This function updates the config data (signalPin) with the new value read from the ComboBox.
// At the same time:
// - the assignment flags in the "base" pin list are accordingly updated (the current pin no. is marked as free
// and the new one as used)
// - an updated pin list is associated to the ComboBox
string after = comboBox.SelectedItem.ToString();
byte nBefore = byte.Parse(signalPin);
byte nAfter = byte.Parse(after);
byte nBefore = byte.Parse(currentPin);
try {
if (signalPin != after) {
// Pin 0 is used for the stepper.
// But Pin 0 is not a correct Pin for the Mega.
if (pinList.Find(x => x.Pin == nBefore)!=null)
pinList.Find(x => x.Pin == nBefore).Used = false;
if (pinList.Find(x => x.Pin == nAfter)!=null)
pinList.Find(x => x.Pin == nAfter).Used = true;
MobiFlightPin p;
p = pinList.Find(x => x.Pin == nBefore);
if (p != null) p.Used = false;
}
catch (Exception ex) {
Log.Instance.log($"Release of pin {currentPin} went wrong: {ex.Message}", LogSeverity.Error);
}
}
static public void assignPin(List<MobiFlightPin> pinList, ref string newPin)
{
// This function tries to reserve the specified pin as newly occupied.
// If it is no longer available (or ""), the first free one will be assigned.
// If no more pins are available, an empty string is returned.
try {
MobiFlightPin p = null;
if (newPin != "") {
// A desired pin is specified: seek it
byte newPinNo = byte.Parse(newPin);
p = pinList.Find(x => x.Pin == newPinNo);
if (p == null) throw new Exception("Nonexistent pin number");
}
if (newPin == "" || p.Used) {
// Either no desired pin is specified, or desired pin is not available:
// assign first free one
p = pinList.Find(x => x.Used == false);
// If no pin free, raise error
if(p == null) throw new ArgumentOutOfRangeException();
}
p.Used = true;
newPin = p.Pin.ToString();
}
catch (ArgumentOutOfRangeException ex) {
MessageBox.Show(i18n._tr("uiMessageNotEnoughPinsMessage"),
i18n._tr("uiMessageNotEnoughPinsHint"),
MessageBoxButtons.OK, MessageBoxIcon.Error);
newPin = "";
}
catch (Exception ex) {
Log.Instance.log($"Pin reassignment from {signalPin} to {after} went wrong: {ex.Message}", LogSeverity.Error);
Log.Instance.log($"Pin assignment to {newPin} went wrong: {ex.Message}", LogSeverity.Error);
}
// now confirm assignment of the new value in the configuration data
signalPin = after;

//ComboBoxHelper.BindMobiFlightFreePins(comboBox, pinList, after);
// the function above has rebuilt its datasource, therefore the ComboBox selection must be restored:
//comboBox.SelectedValue = nAfter;
}

}
}
72 changes: 72 additions & 0 deletions MobiFlight/Config/Compatibility/LedModuleDeprecated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Linq;
using System.Xml.Serialization;

namespace MobiFlight.Config
{
public class LedModuleDeprecated : BaseDevice
{
const ushort _paramCount = 6;
[XmlAttribute]
public String DinPin = "-1";
[XmlAttribute]
public String ClsPin = "-2";
[XmlAttribute]
public String ClkPin = "-3";
[XmlAttribute]
public Byte Brightness = 15;
[XmlAttribute]
public String NumModules = "1";

public LedModuleDeprecated() { Name = "LedModule"; _type = DeviceType.LedModule; }

override public String ToInternal()
{
return base.ToInternal() + Separator
+ DinPin + Separator
+ ClsPin + Separator
+ ClkPin + Separator
+ Brightness + Separator
+ NumModules + Separator
+ Name + End;
}

override public bool FromInternal(String value)
{
if (value.Length == value.IndexOf(End) + 1) value = value.Substring(0, value.Length - 1);
String[] paramList = value.Split(Separator);
if (paramList.Count() != _paramCount + 1)
{
throw new ArgumentException("Param count does not match. " + paramList.Count() + " given, " + _paramCount + " expected");
}
DinPin = paramList[1];
ClsPin = paramList[2];
ClkPin = paramList[3];
Byte.TryParse(paramList[4], out byte brightness);
Brightness = brightness;
NumModules = paramList[5];
Name = paramList[6];

return true;
}

public override bool Equals(object obj)
{
LedModuleDeprecated other = obj as LedModuleDeprecated;
if (other == null)
{
return false;
}

return this.Name == other.Name
&& this.DinPin == other.DinPin
&& this.ClsPin == other.ClsPin
&& this.ClkPin == other.ClkPin;
}

public override string ToString()
{
return Type + ":" + Name + " DinPin:" + DinPin + " ClsPin:" + ClsPin + " ClkPin:" + ClkPin;
}
}
}
9 changes: 7 additions & 2 deletions MobiFlight/Config/Config.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using MobiFlight.Config.Compatibility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MobiFlight.Config
Expand Down Expand Up @@ -133,6 +131,13 @@ public Config FromInternal(String value, bool throwException = false)
currentItem.FromInternal(item + BaseDevice.End);
break;

// backward compatibility
case DeviceType.LedModuleDeprecated:
currentItem = new MobiFlight.Config.LedModuleDeprecated();
currentItem.FromInternal(item + BaseDevice.End);
currentItem = new MobiFlight.Config.LedModule(currentItem as MobiFlight.Config.LedModuleDeprecated);
break;

case DeviceType.LcdDisplay:
currentItem = new MobiFlight.Config.LcdDisplay();
currentItem.FromInternal(item + BaseDevice.End);
Expand Down
52 changes: 39 additions & 13 deletions MobiFlight/Config/LedModule.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MobiFlight.Config
{
public class LedModule : BaseDevice
{
const ushort _paramCount = 6;
public const string MODEL_TYPE_MAX72xx = "1";
public const string MODEL_TYPE_TM1637_4DIGIT = "253";
public const string MODEL_TYPE_TM1637_6DIGIT = "254";

const ushort _paramCount = 7;
[XmlAttribute]
public String ModelType = MODEL_TYPE_MAX72xx;
[XmlAttribute]
public String DinPin = "-1";
[XmlAttribute]
Expand All @@ -22,9 +26,22 @@ public class LedModule : BaseDevice

public LedModule() { Name = "LedModule"; _type = DeviceType.LedModule; }

public LedModule(LedModuleDeprecated module)
{
Name = module.Name;
_type = DeviceType.LedModule;
ModelType = MODEL_TYPE_MAX72xx;
DinPin = module.DinPin;
ClsPin = module.ClsPin;
ClkPin = module.ClkPin;
Brightness = module.Brightness;
NumModules = module.NumModules;
}

override public String ToInternal()
{
return base.ToInternal() + Separator
+ ModelType + Separator
+ DinPin + Separator
+ ClsPin + Separator
+ ClkPin + Separator
Expand All @@ -41,15 +58,14 @@ override public bool FromInternal(String value)
{
throw new ArgumentException("Param count does not match. " + paramList.Count() + " given, " + _paramCount + " expected");
}

DinPin = paramList[1];
ClsPin = paramList[2];
ClkPin = paramList[3];
byte brightness = 15;
Byte.TryParse(paramList[4], out brightness);
ModelType = paramList[1];
DinPin = paramList[2];
ClsPin = paramList[3];
ClkPin = paramList[4];
Byte.TryParse(paramList[5], out byte brightness);
Brightness = brightness;
NumModules = paramList[5];
Name = paramList[6];
NumModules = paramList[6];
Name = paramList[7];

return true;
}
Expand All @@ -63,14 +79,24 @@ public override bool Equals(object obj)
}

return this.Name == other.Name
&& this.ModelType == other.ModelType
&& this.DinPin == other.DinPin
&& this.ClsPin == other.ClsPin
&& this.ClkPin == other.ClkPin;
&& this.ClkPin == other.ClkPin
&& this.NumModules == other.NumModules
&& this.Brightness == other.Brightness;
}

public override string ToString()
{
return Type + ":" + Name + " DinPin:" + DinPin + " ClsPin:" + ClsPin + " ClkPin:" + ClkPin;
return Type + ":" + Name
+ " TypeId:" + ModelType
+ " DinPin:" + DinPin
+ " ClsPin:" + ClsPin
+ " ClkPin:" + ClkPin
+ " Brightness:" + Brightness
+ " NumModules:" + NumModules;

}
}
}
17 changes: 8 additions & 9 deletions MobiFlight/MobiFlightLedModule.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using System;
using CommandMessenger;
using MobiFlight.Base;
using MobiFlight.Config;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms.Design;
using CommandMessenger;
using MobiFlight.Base;
using Newtonsoft.Json.Linq;
using SharpDX.DirectInput;

namespace MobiFlight
{
Expand All @@ -31,6 +26,7 @@ public int SubModules
ClearState();
}
}
public string ModelType { get; set; }

List<LedModuleState> _state = new List<LedModuleState>();

Expand All @@ -55,6 +51,7 @@ public MobiFlightLedModule()
{
Brightness = 15;
SubModules = 1;
ModelType = LedModule.MODEL_TYPE_MAX72xx;
}

protected void Initialize()
Expand All @@ -67,6 +64,8 @@ public void Display(int subModule, String value, byte points, byte mask, bool re
{
if (!_initialized) Initialize();

if (subModule > 1 && ModelType != LedModule.MODEL_TYPE_MAX72xx) return;

var command = new SendCommand((int)MobiFlightModule.Command.SetModule);

if (value.IndexOf(".") >=0 )
Expand Down
Loading