using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace FCC_Uber_MDX_Configurator { public partial class AutoSearchBrainIdentifyForm : Form { #region FusionUSB DLL Calls public static class FusionUSB { [System.Runtime.InteropServices.DllImport("FusionUSB.dll")] public static extern bool FUSB_Initialize(ref UInt32 pFusionUSB, String deviceID, UInt32 instanceNumber); [System.Runtime.InteropServices.DllImport("FusionUSB.dll")] public static extern bool FUSB_Receive(UInt32 pFusionUSB, byte[] buffer); [System.Runtime.InteropServices.DllImport("FusionUSB.dll")] public static extern bool FUSB_Send(UInt32 pFusionUSB, byte[] buffer); [System.Runtime.InteropServices.DllImport("FusionUSB.dll")] public static extern UInt32 FUSB_Free(ref UInt32 pFusionUSB); [System.Runtime.InteropServices.DllImport("FusionUSB.dll")] public static extern bool FUSB_printf(UInt32 pFusionUSB); [System.Runtime.InteropServices.DllImport("FusionUSB.dll")] public static extern UInt32 FUSB_GetWinUSBerror(UInt32 pFusionUSB); [System.Runtime.InteropServices.DllImport("FusionUSB.dll")] public static extern UInt32 FUSB_GetFUSBstatus(UInt32 pFusionUSB); [System.Runtime.InteropServices.DllImport("FusionUSB.dll")] public static extern String FUSB_GetDeviceInstanceID(UInt32 pFusionUSB); } #endregion #region USB Global Constants and Variables //UInt32 instanceNumber = 0; //const UInt32 INVALID_HANDLE_VALUE = 0xFFFFFFFF; //const UInt32 NULL = 0; //UInt32 pFusionUSB = NULL; //const UInt32 FUSB_OK = 0; //const UInt32 FUSB_DEVICE_NOT_FOUND = 13; //const UInt32 MAX_DEVICES = 100; byte[] DataToSend = new byte[64]; int currentIteration = 0; string idOfBrain = "No Brain"; bool flipper = true; bool isFirstTime = true; AutoSearchBrains.RAW_BrainID _FB = null; #endregion #region Initialization public AutoSearchBrainIdentifyForm(AutoSearchBrains.RAW_BrainID _id) { try { InitializeComponent(); LBL_BRAIN_ID.Text = "Initializing..."; LBL_BRAIN_ID.Update(); _FB = _id; Timer_Blink.Enabled = true; Timer_Blink.Tick += new EventHandler(Timer_Blink_Tick); this.FormClosing += new FormClosingEventHandler(AutoSearchBrainIdentifyForm_FormClosing); } catch (Exception) { (new ErrorForm("Unknown Error", "There was an unexpected error. If you can reproduce this error, please email me at FusionControlCentre@gmail.com")).ShowDialog(); this.Close(); return; } } void AutoSearchBrainIdentifyForm_FormClosing(object sender, FormClosingEventArgs e) { DataToSend = new byte[64]; if (_FB.version == 3) { for (int i = 0; i < 12; i++) { int val = 252; if (flipper) { val += 2; } DataToSend[i] = (byte)val; } DataToSend[61] = (byte)255; } else { for (int i = 0; i < 32; i++) { int val = 0; DataToSend[i] = (byte)val; DataToSend[i + 1] = (byte)val; } DataToSend[61] = (byte)2; if (flipper) { DataToSend[61] += 1; } } //int successWrite = 0; //int successRead = 0; try { FusionBrain_WinUSB.MainUSBClass.SendAndReceiveViaInterruptTransfers(ref DataToSend, _FB.raw_index, _FB.version); //if (_FB.Stream_Reader != null && _FB.Stream_Writer != null) //{ // successWrite = _FB.Stream_Writer.Write(DataToSend, 1000); // successRead = _FB.Stream_Reader.Read(DataToSend, 1000); //} bool success = FusionBrain_WinUSB.MainUSBClass.Zombie(); } catch (Exception com_error) { System.Windows.Forms.MessageBox.Show("TEMPORARY ERROR CATCH: FAILED USB COMMUNICATION FOR BRAIN[" + _FB.humanName + "]\r\n" + com_error.ToString()); } Timer_Blink.Enabled = false; this.Close(); } #endregion #region USB -- OLD /* #region Initializing Brain public bool InitializeBrain(string _id) { try { idOfBrain = _id; LBL_BRAIN_ID.Text = "Getting Instance Handle"; LBL_BRAIN_ID.Update(); if (FusionUSB.FUSB_Initialize(ref pFusionUSB, _id, instanceNumber)) { LBL_BRAIN_ID.Text = "Received Instance Handle"; LBL_BRAIN_ID.Update(); return true; } else { (new ErrorForm("Error Connecting", "Cannot connect to Brain with ID:\r\n" + _id)).ShowDialog(); return false; } } catch (Exception e) { (new ErrorForm("Unexpected Error", e.ToString())).ShowDialog(); this.DialogResult = DialogResult.Cancel; this.Close(); return false; } } #endregion */ /* #region Free USB Handle public UInt32 FreeTheDevice() { try { try { FusionUSB.FUSB_Send(pFusionUSB, new byte[64]); } catch (Exception) { (new ErrorForm("Communication Broken", "There was an unexpected error communicating to your Fusion Brain. Please verify that your Fusion Brain's DC Power and USB cable are fully plugged in. If you can reproduce this error, please email me at FusionControlCentre@gmail.com")).ShowDialog(); } UInt32 sucUINT; sucUINT = FusionUSB.FUSB_Free(ref pFusionUSB); if (sucUINT == 0) { return sucUINT; } return sucUINT; } catch (Exception) { (new ErrorForm("Unknown Error", "There was an unexpected error. If you can reproduce this error, please email me at FusionControlCentre@gmail.com")).ShowDialog(); this.Close(); return (UInt32)42; } } #endregion */ #endregion #region Blink Timer public void Timer_Blink_Tick(object sender, EventArgs e) { try { if (isFirstTime) { LBL_BRAIN_ID.Text = "Starting transfer"; LBL_BRAIN_ID.Update(); } //if (_FB.Device == null) //{ // (new ErrorForm("Communication Broken [NULL Device]", "There was an unexpected error communicating to your Fusion Brain. Please verify that your Fusion Brain's DC Power and USB cable are fully plugged in. If you can reproduce this error, please email me at FusionControlCentre@gmail.com")).ShowDialog(); // this.Close(); //} PROGRESS_DoingSomething.Value = (PROGRESS_DoingSomething.Value + 1) % 100; DataToSend = new byte[64]; if (_FB.version == 4) { for (int i = 0; i < 32; i += 2) { int val1 = 0; int val2 = 0; if ((i + currentIteration) % 4 == 0) { val1 += 1; } DataToSend[i] = (byte)val1; DataToSend[i + 1] = (byte)val2; } currentIteration++; DataToSend[61] = (byte)2; if (flipper) { DataToSend[61] += 1; } flipper = !flipper; } else if (_FB.version == 3) { for (int i = 0; i < 12; i++) { int val = 252; if (flipper) { val += 2; } if ((i + currentIteration) % 4 == 0) { val += 1; } DataToSend[i] = (byte)val; } currentIteration++; DataToSend[61] = (byte)255; flipper = !flipper; } int successWrite = 0; int successRead = 0; try { FusionBrain_WinUSB.MainUSBClass.SendAndReceiveViaInterruptTransfers(ref DataToSend, _FB.raw_index, _FB.version); } catch (Exception com_error) { System.Windows.Forms.MessageBox.Show("TEMPORARY ERROR CATCH: FAILED USB COMMUNICATION FOR BRAIN[" + _FB.humanName + "]\r\n" + com_error.ToString()); } if (isFirstTime) { LBL_BRAIN_ID.Text = "Blinking Fusion Brain: " + _FB.humanName; LBL_BRAIN_ID.Update(); isFirstTime = false; } } catch (Exception) { (new ErrorForm("Unknown Error", "There was an unexpected error. If you can reproduce this error, please email me at FusionControlCentre@gmail.com")).ShowDialog(); this.Close(); } } #endregion /* #region Free the Device on Close private void AutoSearchBrainIdentifyForm_FormClosing(object sender, FormClosingEventArgs e) { try { Console.WriteLine(FreeTheDevice().ToString()); } catch (Exception e2) { (new ErrorForm("Unexpected Error", e2.ToString())).ShowDialog(); this.DialogResult = DialogResult.Cancel; this.Close(); } } #endregion */ } }