Skip to content

Commit

Permalink
Fix for MobiFlight#643-Incorrect update of pin selection combo boxes
Browse files Browse the repository at this point in the history
TEST VERSION, LIMITED TO LedSegment!
  • Loading branch information
GioCC committed Jan 5, 2022
1 parent 3792b56 commit 4082999
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 28 deletions.
25 changes: 25 additions & 0 deletions Base/ComboBoxHelper.cs
Expand Up @@ -47,7 +47,13 @@ static public bool SetSelectedItemByPart(ComboBox comboBox, string value)

static public bool BindMobiFlightFreePins(ComboBox comboBox, List<MobiFlightPin> Pins, String CurrentPin, bool analogOnly = false)
{
// This function returns a list of all currently free pins, plus the specified one marked as free.
// Required because, in a selection list for a device signal, the already assigned pin must be in the list in order to be selectable.

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)))
UsablePins.Find(x => x.Pin == byte.Parse(CurrentPin)).Used = false;

Expand All @@ -56,11 +62,30 @@ static public bool BindMobiFlightFreePins(ComboBox comboBox, List<MobiFlightPin>
UsablePins = UsablePins.FindAll(x => x.isAnalog == true);
}

// Assign the all-free list to the combobox
comboBox.DataSource = UsablePins.FindAll(x => x.Used == false);
comboBox.DisplayMember = "Name";
comboBox.ValueMember = "Pin";

return false;
}
static public void reassignPin(ComboBox comboBox, List<MobiFlightPin> pinList, ref string before)
{
string after = comboBox.SelectedItem.ToString();
byte nBefore = byte.Parse(before);
byte nAfter = byte.Parse(after);
try {
if (before != after) {
pinList.Find(x => x.Pin == nBefore).Used = false;
pinList.Find(x => x.Pin == nAfter).Used = true;
}
}
catch (Exception e) {
Log.Instance.log($"Pin reassignment from {before} to {after} went wrong", LogSeverity.Debug);
}
ComboBoxHelper.BindMobiFlightFreePins(comboBox, pinList, after);
comboBox.SelectedValue = nAfter;
before = after;
}
}
}
76 changes: 48 additions & 28 deletions UI/Panels/Device/MFLedSegmentPanel.cs
Expand Up @@ -16,6 +16,7 @@ public partial class MFLedSegmentPanel : UserControl
/// </summary>
public event EventHandler Changed;
private MobiFlight.Config.LedModule ledModule;
private List<MobiFlightPin> pinList; // COMPLETE list of pins (includes status)
bool initialized = false;

public MFLedSegmentPanel()
Expand All @@ -27,53 +28,72 @@ public MFLedSegmentPanel()
if (Parent != null) mfIntensityTrackBar.BackColor = Parent.BackColor;
}

public MFLedSegmentPanel(MobiFlight.Config.LedModule ledModule, List<MobiFlightPin> Pins):this()
public MFLedSegmentPanel(MobiFlight.Config.LedModule ledModule, List<MobiFlightPin> Pins) : this()
{
ComboBoxHelper.BindMobiFlightFreePins(mfPin1ComboBox, Pins, ledModule.DinPin);
ComboBoxHelper.BindMobiFlightFreePins(mfPin2ComboBox, Pins, ledModule.ClsPin);
ComboBoxHelper.BindMobiFlightFreePins(mfPin3ComboBox, Pins, ledModule.ClkPin);
pinList = Pins; // Keep pin list stored
// Since the panel is synced whenever a new device is selected, the free/used list won't change
// (because of changes elsewhere) as long as we remain in this panel, so we can keep it stored
// for the lifetime of the panel.

if (mfPin1ComboBox.Items.Count > 2)
{
mfPin1ComboBox.SelectedIndex = 0;
mfPin2ComboBox.SelectedIndex = 1;
mfPin3ComboBox.SelectedIndex = 2;
}

// TODO: Complete member initialization
this.ledModule = ledModule;

mfPin1ComboBox.SelectedValue = byte.Parse(ledModule.DinPin);
mfPin2ComboBox.SelectedValue = byte.Parse(ledModule.ClsPin);
mfPin3ComboBox.SelectedValue = byte.Parse(ledModule.ClkPin);
update_lists();

ComboBoxHelper.SetSelectedItem(mfNumModulesComboBox, ledModule.NumModules);
if (mfPin1ComboBox.Items.Count > 2) {
// mfPin1ComboBox.SelectedIndex = 0;
// mfPin2ComboBox.SelectedIndex = 1;
// mfPin3ComboBox.SelectedIndex = 2;
}

textBox1.Text = ledModule.Name;
mfIntensityTrackBar.Value = ledModule.Brightness;
//setValues();
ComboBoxHelper.SetSelectedItem(mfNumModulesComboBox, ledModule.NumModules);

initialized = true;
}
private void setNonPinValues()
{
ledModule.Name = textBox1.Text;
ledModule.Brightness = (byte)(mfIntensityTrackBar.Value);
ledModule.NumModules = mfNumModulesComboBox.Text;
}
private void update_lists()
{
bool ex_initialized = initialized;
initialized = false; // inhibit value_Changed events
ComboBoxHelper.BindMobiFlightFreePins(mfPin1ComboBox, pinList, ledModule.DinPin);
mfPin1ComboBox.SelectedValue = byte.Parse(ledModule.DinPin);
ComboBoxHelper.BindMobiFlightFreePins(mfPin2ComboBox, pinList, ledModule.ClsPin);
mfPin2ComboBox.SelectedValue = byte.Parse(ledModule.ClsPin);
ComboBoxHelper.BindMobiFlightFreePins(mfPin3ComboBox, pinList, ledModule.ClkPin);
mfPin3ComboBox.SelectedValue = byte.Parse(ledModule.ClkPin);

private void value_Changed(object sender, EventArgs e)
initialized = ex_initialized;
}

private void update_all(ComboBox comboBox)
{
if (!initialized) return;
bool ex_initialized = initialized;
initialized = false; // inhibit value_Changed events

setValues();
// First update the one that is changed
if (comboBox == mfPin1ComboBox) { ComboBoxHelper.reassignPin(mfPin1ComboBox, pinList, ref ledModule.DinPin); } else
if (comboBox == mfPin2ComboBox) { ComboBoxHelper.reassignPin(mfPin2ComboBox, pinList, ref ledModule.ClsPin); } else
if (comboBox == mfPin3ComboBox) { ComboBoxHelper.reassignPin(mfPin3ComboBox, pinList, ref ledModule.ClkPin); }
// then the others
update_lists();

if (Changed != null)
Changed(ledModule, new EventArgs());
initialized = ex_initialized;
}

private void setValues()
private void value_Changed(object sender, EventArgs e)
{
ledModule.DinPin = mfPin1ComboBox.SelectedItem.ToString();
ledModule.ClsPin = mfPin2ComboBox.SelectedItem.ToString();
ledModule.ClkPin = mfPin3ComboBox.SelectedItem.ToString();
ledModule.Name = textBox1.Text;
ledModule.Brightness = (byte)(mfIntensityTrackBar.Value);
ledModule.NumModules = mfNumModulesComboBox.Text;
if (!initialized) return;
update_all(sender as ComboBox);
setNonPinValues();
if (Changed != null)
Changed(ledModule, new EventArgs());
}
}
}

0 comments on commit 4082999

Please sign in to comment.