using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using FusionBrain_WinUSB; namespace FCC_Uber_MDX_Configurator { public partial class AutoSearchBrains : Form { public List[] FoundBrains = { new List(), new List() }; int current_selected_version = -1, current_selected_index = -1; #region RAW BrainID public class RAW_BrainID { public int raw_index = -1; public string humanName; public int version = -1; } #endregion #region Initialization public AutoSearchBrains() { InitializeComponent(); AutoSearchAndUpdate(); this.FormClosing += new FormClosingEventHandler(AutoSearchBrains_FormClosing); } #endregion #region Auto Search and Update public void AutoSearchAndUpdate() { bool success = false; try { success = FindAllAttachedBrains(); } catch (DllNotFoundException) { //(new ErrorForm("Drivers Not Installed", "The LibUSB Drivers are not installed properly." + // "You can still use the software, but USB communication will not work until the driver is installed", true)).ShowDialog(); } if (success) { LISTBOX_AllBrains_03.Items.Clear(); LISTBOX_AllBrains_04.Items.Clear(); foreach (RAW_BrainID _FB in FoundBrains[0]) { LISTBOX_AllBrains_03.Items.Add(_FB.humanName); LISTBOX_AllBrains_03.Enabled = true; } foreach (RAW_BrainID _FB in FoundBrains[1]) { LISTBOX_AllBrains_04.Items.Add(_FB.humanName); LISTBOX_AllBrains_03.Enabled = true; } BTN_SAVE_AUTOSEARCH.Enabled = true; BTN_IDENTIFY.Enabled = true; } if (FoundBrains[0].Count == 0) { LISTBOX_AllBrains_03.Items.Clear(); LISTBOX_AllBrains_03.Items.Add("No Version 3 Fusion Brain's were detected"); LISTBOX_AllBrains_03.Enabled = false; } if (FoundBrains[1].Count == 0) { LISTBOX_AllBrains_04.Items.Clear(); LISTBOX_AllBrains_04.Items.Add("No Version 4 Fusion Brain's were detected"); LISTBOX_AllBrains_04.Enabled = false; } if (FoundBrains[0].Count == 0 && FoundBrains[1].Count == 0) { BTN_SAVE_AUTOSEARCH.Enabled = false; BTN_IDENTIFY.Enabled = false; } } #endregion #region Refresh Button Clicked private void BTN_REFRESH_Click(object sender, EventArgs e) { AutoSearchAndUpdate(); } #endregion #region Find All Attached Brains public bool FindAllAttachedBrains() { FoundBrains[0].Clear(); FoundBrains[1].Clear(); FoundBrains = Find_The_Damned_Things(); if (FoundBrains[0].Count == 0 && FoundBrains[1].Count == 0) { ErrorForm _tempErrorForm = new ErrorForm("No Brains", "There were no attached Fusion Brain devices connected. " + "Verify that you have the latest drivers (Version 4.0+ using WinUSB), " + "verify that the blue power LED is illuminated on your Fusion Brain, " + "and verify that the Fusion Brain is connected to your computer through a USB 2.0 Cable less than 2m"); _tempErrorForm.ShowDialog(); return false; } return true; } #endregion #region Find the Little Buggers public List[] Find_The_Damned_Things() { List[] _VersionList = new List[2]; _VersionList[0] = new List(); _VersionList[1] = new List(); List _03 = new List(); List _04 = new List(); MainUSBClass.FindAllDevices(ref _03, ref _04); for (int i = 0; i < _03.Count; i++) { RAW_BrainID _rbi = new RAW_BrainID(); _rbi.raw_index = i; _rbi.humanName = _03[i].Substring(_03[i].LastIndexOf('{')); _rbi.version = 3; _VersionList[0].Add(_rbi); } for (int i = 0; i < _04.Count; i++) { RAW_BrainID _rbi = new RAW_BrainID(); _rbi.raw_index = i; _rbi.humanName = _04[i].Substring(_04[i].LastIndexOf('{')); _rbi.version = 4; _VersionList[1].Add(_rbi); } return _VersionList; } #endregion #region Cancel Button Clicked private void BTN_CANCEL_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } #endregion #region Form Closing void AutoSearchBrains_FormClosing(object sender, FormClosingEventArgs e) { DisposeOfAllBrains(); } #endregion #region Dispose of Brains public void DisposeOfAllBrains() { //try //{ // for (int i = 0; i < FoundBrains.Count; i++) // { // if (FoundBrains[i].Device != null && FoundBrains[i].Stream_Reader != null && FoundBrains[i].Stream_Writer != null) // { // // FoundBrains[i].Stream_Writer.Dispose(); // FoundBrains[i].Stream_Reader.Dispose(); // FoundBrains[i].Device.Close(); // } // } //} //catch (Exception) //{ // return; //} } #endregion #region Save Button Clicked private void BTN_SAVE_Click(object sender, EventArgs e) { if (current_selected_index < 0 || current_selected_version < 0) { ErrorForm _tempError = new ErrorForm("Select a Fusion Brain", "Please Select a Fusion Brain Instance from the list"); _tempError.ShowDialog(); return; } this.DialogResult = DialogResult.OK; } #endregion #region Get the Data Back public string GetSelectedBrainIDBack() { if (current_selected_index < 0 || current_selected_version < 0) { return null; } return FoundBrains[current_selected_version - 3][current_selected_index].humanName.ToString(); } public int GetSelectedVersionBack() { if (current_selected_index < 0 || current_selected_version < 0) { return -1; } return FoundBrains[current_selected_version - 3][current_selected_index].version; } #endregion #region Identify Button Clicked private void BTN_IDENTIFY_Click(object sender, EventArgs e) { if (current_selected_version < 0 || current_selected_index < 0) { (new ErrorForm("Select a Brain Instance", "Please select a Fusion Brain ID from the list to identify it")).ShowDialog(); return; } AutoSearchBrainIdentifyForm _asif = (new AutoSearchBrainIdentifyForm(FoundBrains[current_selected_version - 3][current_selected_index])); _asif.ShowDialog(); _asif.Dispose(); } #endregion #region Help private void BTN_HELP_IDENTIFYBRAIN_Click(object sender, EventArgs e) { GlobalStaticClass.FireHelpEvent("Auto Search Brain Form", "General"); } #endregion private void LISTBOX_AllBrains_03_SelectedIndexChanged(object sender, EventArgs e) { current_selected_version = 3; current_selected_index = LISTBOX_AllBrains_03.SelectedIndex; } private void LISTBOX_AllBrains_04_SelectedIndexChanged(object sender, EventArgs e) { current_selected_version = 4; current_selected_index = LISTBOX_AllBrains_04.SelectedIndex; } } }