using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using QC.MMI.MMI_PCBA_Tester_Control;

namespace FixtureControlUtility;

public class Form1 : Form
{
	public int controlType;

	public int setValue = 1;

	public string control_item = "";

	public int MuxRdDelay = 100;

	public USB_IO_Expander myIO = new USB_IO_Expander();

	public bool completedSuccess = true;

	private double colorVal;

	public string[] single_ctl_list = new string[32]
	{
		"AN_BLUE_DET", "AN_GRN_DET", "AN_RED_DET", "AN_LIGHT_DET1", "AN_LIGHT_DET2", "AN_VIBE_DET", "AUDIO_IN_RLY_A0", "AUDIO_IN_RLY_A1", "AUDIO_IN_RLY_A2", "AUDIO_RLY_EN",
		"AUDIO_RLY_EN_N", "AUDIO_OUT_RLY_A0", "AUDIO_OUT_RLY_A1", "AUDIO_OUT_RLY_A2", "CTP_touch", "OTG_VBUS_STAT", "PHOT_LED1", "RLY_CTP", "RLY_HS_DETECT", "RLY_OTG_EN",
		"RLY_POGO_AMP_N", "RLY_SINE_OUT", "RLY_SOLENOID_PWRON", "RLY_SOLENOID_VOL_DN", "RLY_SOLENOID_VOL_UP", "TESTER_ID0", "TESTER_ID1", "TESTER_ID2", "TONE_DET_N", "VBAT_EN_N",
		"OMTP_CTIA_SELECT", "HUB_RESET_N"
	};

	public string[] Mux_ctl_list = new string[16]
	{
		"RLY_EAR_OUT", "RLY_FF_SPKR_OUT", "RLY_HPH_L_OUT", "RLY_HPH_R_OUT", "RLY_MIC1_OUT", "RLY_MIC2_OUT", "RLY_MIC3_OUT", "RLY_SPKR1", "RLY_SPKR2", "RLY_SPKR3",
		"RLY_TST_MIC1_IN", "RLY_TST_MIC2_IN", "RLY_TST_MIC3_IN", "RLY_TST_MIC4_IN", "TP30", "TP31"
	};

	public string[] I2C_ctl_list = new string[46]
	{
		"Initialize_Board", "PwrPhone_CC1_SS1", "PwrPhone_CC2_SS2", "Open_MMI", "PwrPhoneOFF", "PwrPhoneON", "Get_PC_VBUS_Current (mA)", "Get_PC_VBUS_Voltage (V)", "Get_VBAT_Current (mA)", "Get_VBAT_Voltage (V)",
		"Set_VBAT_LOW", "Set_VBAT_HI", "SetVBATT_Increase_100mV", "SetVBATT_Increase_200mV", "SetVBATT_Decrease_100mV", "SetVBATT_Decrease_200mV", "PressKeys", "SetKeyBacklight", "ReadKeyBacklight", "ReadPressureSensor_BMP285",
		"ReadHumiditySensor_SHTW2", "ReadTemperatureSensor_SHTW2", "Get_ADC128D818value_A4", "Get_ADC128D818value_A9", "Get_ADC128D818value_B4", "Get_ADC128D818value_B9", "Get_ADC128D818value_VCONN", "Set_CC1_56k_High", "Set_CC1_10k_High", "Set_CC2_56k_High",
		"Set_CC2_10k_High", "Set_Audio_CC1_CC2", "Set_5k_CC1_PD", "Enable_Parallel_Charging", "Set_UFP_VBUS", "I2C_CONTROL_TEST", "I2C_Read_RED", "I2C_Read_GREEN", "I2C_Read_BLUE", "SET_MIC5_OUT",
		"SET_MIC6_OUT", "Set_VCONN", "UV_LED_EN", "UV_LED_DIS", "CAMERA_LED_EN", "CAMERA_LED_DIS"
	};

	public string[] Self_Tests = new string[25]
	{
		"Test_All", "FVT_SPK1_EAR_IN", "FVT_SPK2_EAR_IN", "FVT_SPK3_EAR_IN", "FVT_MIC1OUT_MIC1", "FVT_MIC1OUT_MIC2", "FVT_MIC3OUT_MIC3", "FVT_MIC3OUT_MIC4", "FVT_MIC1OUT_MIC5", "FVT_MIC3OUT_MIC6",
		"FVT_SBU1OUT_DP1", "FVT_SBU1OUT_DM1", "FVT_SBU2OUT_DP2", "FVT_SBU2OUT_DM2", "FVT_HSMIC_HSSPK_L", "FVT_HSMIC_HSSPK_R", "FVT_VOL_UP_VIBE", "FVT_LIGHT1_VOL_DOWN", "FVT_LIGHT2_PWRON", "FVT_OTG_VBUS",
		"FVT_CC1PU_VBUS_A4", "FVT_CC2PD_VBUS_B4", "FVT_CC1PD_VBUS_B9", "FVT_VBUSA4_CAMERA_LED", "FVT_VBUSA9_UV_LED"
	};

	private IContainer components;

	private TextBox textBoxLog;

	private GroupBox groupBox1;

	private RadioButton radioButton3;

	private RadioButton radioButton2;

	private RadioButton radioButton1;

	private GroupBox groupBox2;

	private RadioButton radioButton5;

	private RadioButton radioButton4;

	private RadioButton radioButton6;

	private ComboBox comboBox1;

	private Button button1;

	private TextBox textBox2;

	private Button button2;

	private GroupBox groupBox3;

	private Label label1;

	private TextBox textBox3;

	private Button btnClear;

	private Button btnSaveAs;

	public Form1()
	{
		InitializeComponent();
		updateDropList(0);
	}

	private void RadioButton1CheckedChanged(object sender, EventArgs e)
	{
		controlType = 0;
		updateDropList(0);
	}

	private void RadioButton2CheckedChanged(object sender, EventArgs e)
	{
		controlType = 1;
		updateDropList(1);
	}

	private void RadioButton3CheckedChanged(object sender, EventArgs e)
	{
		controlType = 2;
		updateDropList(2);
	}

	private void RadioButton6CheckedChanged(object sender, EventArgs e)
	{
		controlType = 3;
		updateDropList(3);
	}

	private void TextBox1TextChanged(object sender, EventArgs e)
	{
		textBoxLog.SelectionStart = textBoxLog.TextLength;
		textBoxLog.ScrollToCaret();
	}

	private void Button2Click(object sender, EventArgs e)
	{
		myIO.directWrite(textBox2.Text);
		TextBox textBox = textBoxLog;
		string text = textBox.Text;
		textBox.Text = text + Environment.NewLine + "-----" + Environment.NewLine + myIO.Response_Str;
	}

	private void TextBox3TextChanged(object sender, EventArgs e)
	{
	}

	private void updateDropList(int ctl_type)
	{
		switch (ctl_type)
		{
		case 0:
			comboBox1.Items.Clear();
			comboBox1.Items.AddRange(single_ctl_list);
			comboBox1.SelectedItem = single_ctl_list[10];
			groupBox2.Enabled = true;
			break;
		case 1:
			comboBox1.Items.Clear();
			comboBox1.Items.AddRange(Mux_ctl_list);
			comboBox1.SelectedItem = Mux_ctl_list[0];
			groupBox2.Enabled = true;
			break;
		case 2:
			comboBox1.Items.Clear();
			comboBox1.Items.AddRange(I2C_ctl_list);
			comboBox1.SelectedItem = I2C_ctl_list[0];
			groupBox2.Enabled = false;
			break;
		case 3:
			comboBox1.Items.Clear();
			comboBox1.Items.AddRange(Self_Tests);
			comboBox1.SelectedItem = Self_Tests[0];
			groupBox2.Enabled = false;
			break;
		default:
			textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "Error control type " + ctl_type;
			groupBox2.Enabled = true;
			break;
		}
	}

	private void RadioButton4CheckedChanged(object sender, EventArgs e)
	{
		setValue = 1;
	}

	private void RadioButton5CheckedChanged(object sender, EventArgs e)
	{
		setValue = 0;
	}

	private void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
	{
		control_item = comboBox1.SelectedItem.ToString();
	}

	private void Button1Click(object sender, EventArgs e)
	{
		try
		{
			int num = int.Parse(this.textBox3.Text);
			if (num > 7 || num < 0)
			{
				TextBox textBox = textBoxLog;
				string text = textBox.Text;
				textBox.Text = text + Environment.NewLine + "Fixture ID should be between 0 to 7" + Environment.NewLine + myIO.Response_Str;
				DebugMessage.Write("FixtureID Invalid", "Please set valid Fixture ID (0 to 7)");
				return;
			}
		}
		catch
		{
			DebugMessage.Write("FixtureID Invalid", "Please set valid Fixture ID (0 to 7)");
			return;
		}
		bool IO_OUT = true;
		bool flag = false;
		if (control_item.StartsWith("AN_"))
		{
			control_item = control_item.Substring(3);
			flag = true;
		}
		switch (controlType)
		{
		case 0:
		{
			if (control_item == "CTP_touch")
			{
				myIO.CTP_touch();
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "CTP touch - with 100ms between on and off=";
				break;
			}
			USB_IO_Expander.IndividualControl individualControl = USB_IO_Expander.IndividualControl.NOT_USED;
			USB_IO_Expander.SetValue setBit = USB_IO_Expander.SetValue.LOW;
			if (setValue == 1)
			{
				setBit = USB_IO_Expander.SetValue.HIGH;
			}
			string[] names = Enum.GetNames(typeof(USB_IO_Expander.IndividualControl));
			for (int i = 0; i < names.Count(); i++)
			{
				if (names[i] == control_item)
				{
					individualControl = (USB_IO_Expander.IndividualControl)i;
					break;
				}
			}
			if (!flag)
			{
				if (!myIO.Individual_Control(individualControl, ref setBit, ref IO_OUT))
				{
					textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "Error -- Sending Signal control: " + individualControl;
					break;
				}
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "Send Signal control successuly: " + individualControl;
				if (!IO_OUT)
				{
					textBoxLog.Text = string.Concat(DebugMessage.str_debug_msg, Environment.NewLine, individualControl, " : ", setBit);
				}
				break;
			}
			string text2 = myIO.get_CMD_str(individualControl.ToString());
			text2 = text2.TrimEnd();
			double AN_Voltage = 0.0;
			if (!myIO.ReadAnalogVoltage(text2, ref AN_Voltage, myIO.USBIO_Vdd))
			{
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "Error -- Sending Signal control: Analog " + individualControl;
			}
			else
			{
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "Send Signal control successuly: Analog" + individualControl;
				textBoxLog.Text = string.Concat(DebugMessage.str_debug_msg, Environment.NewLine, individualControl, " AN_Voltage : ", AN_Voltage, "(Volt)");
			}
			control_item = "AN_" + control_item;
			break;
		}
		case 1:
		{
			USB_IO_Expander.MUX_Control mUX_Control = USB_IO_Expander.MUX_Control.TP30;
			string[] names2 = Enum.GetNames(typeof(USB_IO_Expander.MUX_Control));
			for (int j = 0; j < names2.Count(); j++)
			{
				if (names2[j] == control_item)
				{
					mUX_Control = (USB_IO_Expander.MUX_Control)j;
					break;
				}
			}
			if (!myIO.Set_Mux_Select(MuxRdDelay, mUX_Control))
			{
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "Error -- Sending MUX control: " + mUX_Control;
			}
			else
			{
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "Send MUX control successuly: " + mUX_Control;
			}
			break;
		}
		case 2:
		{
			double current_reading;
			double val;
			switch (control_item)
			{
			case "Initialize_Board":
				completedSuccess = myIO.InitializeDevices();
				break;
			case "PwrPhone_CC1_SS1":
				if (myIO.isRevB)
				{
					completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.CONNECT_DUT_CC1_SS1);
				}
				else
				{
					completedSuccess = myIO.SetIOexpand_SX1509("00", "FA") && myIO.SetIOexpand_SX1508("18");
				}
				break;
			case "PwrPhone_CC2_SS2":
				completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.CONNECT_DUT_CC2_SS2);
				break;
			case "Set_VCONN":
				completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.CC_VCONN_EN);
				break;
			case "PwrPhoneOFF":
				completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.POWER_OFF_DUT);
				break;
			case "PwrPhoneON":
				completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.POWER_DUT);
				break;
			case "Open_MMI":
				completedSuccess = myIO.Open_MMI_Applicaiton();
				break;
			case "Get_VBAT_Current (mA)":
				completedSuccess = myIO.VBAT_Current(out current_reading);
				break;
			case "Get_PC_VBUS_Current (mA)":
				completedSuccess = myIO.PC_VBUS_Current(out current_reading);
				break;
			case "Get_VBAT_Voltage (V)":
				completedSuccess = myIO.VBAT_Voltage(out current_reading);
				break;
			case "Get_PC_VBUS_Voltage (V)":
				completedSuccess = myIO.PC_VBUS_Voltage(out current_reading);
				break;
			case "Set_VBAT_LOW":
				completedSuccess = myIO.Set_VBAT_LOW();
				myIO.VBAT_Voltage(out current_reading);
				break;
			case "Set_VBAT_HI":
				completedSuccess = myIO.Set_VBAT_HI();
				myIO.VBAT_Voltage(out current_reading);
				break;
			case "SetVBATT_Increase_100mV":
				completedSuccess = myIO.SetVBATT_Increase_100mV();
				myIO.VBAT_Voltage(out current_reading);
				break;
			case "SetVBATT_Increase_200mV":
				completedSuccess = myIO.SetVBATT_Increase_200mV();
				myIO.VBAT_Voltage(out current_reading);
				break;
			case "SetVBATT_Decrease_100mV":
				completedSuccess = myIO.SetVBATT_Decrease_100mV();
				myIO.VBAT_Voltage(out current_reading);
				break;
			case "SetVBATT_Decrease_200mV":
				completedSuccess = myIO.SetVBATT_Decrease_200mV();
				myIO.VBAT_Voltage(out current_reading);
				break;
			case "PressKeys":
				completedSuccess = myIO.PressKeys();
				break;
			case "SetKeyBacklight":
				completedSuccess = myIO.SetKeyBacklight();
				break;
			case "ReadKeyBacklight":
			{
				string readVal4 = "";
				completedSuccess = myIO.ReadKeyBacklight(out readVal4);
				break;
			}
			case "ReadPressureSensor_BMP285":
			{
				string readVal3 = "";
				completedSuccess = myIO.ReadPressureSensor_BMP285(out readVal3, out var temperature, out var pressure);
				DebugMessage.Write("READING", Math.Round(temperature, 2) + " degrees Celsius, and " + Environment.NewLine + Math.Round(pressure, 2) + "Pa");
				break;
			}
			case "ReadHumiditySensor_SHTW2":
			{
				string readVal2 = "";
				double percentH = 0.0;
				completedSuccess = myIO.ReadHumiditySensor_SHTW2(out readVal2, out percentH);
				DebugMessage.Write("READING", percentH + "%RH");
				break;
			}
			case "ReadTemperatureSensor_SHTW2":
			{
				string readVal = "";
				double TCelcius = 0.0;
				completedSuccess = myIO.ReadTemperatureSensor_SHTW2(out readVal, out TCelcius);
				DebugMessage.Write("READING", TCelcius + " degrees celcius");
				break;
			}
			case "Set_CC1_56k_High":
				if (myIO.isRevB)
				{
					completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.CC1_56K_PU);
				}
				else
				{
					completedSuccess = myIO.SetIOexpand_SX1509("00", "FE") && myIO.SetIOexpand_SX1508("18");
				}
				break;
			case "Set_CC1_10k_High":
				if (myIO.isRevB)
				{
					completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.CC1_10K_PU);
				}
				else
				{
					completedSuccess = myIO.SetIOexpand_SX1509("00", "FE") && myIO.SetIOexpand_SX1508("10");
				}
				break;
			case "Set_CC2_56k_High":
				completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.CC2_56K_PU);
				break;
			case "Set_CC2_10k_High":
				completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.CC2_10K_PU);
				break;
			case "Set_Audio_CC1_CC2":
				if (myIO.isRevB)
				{
					completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.CC_AUDIO_1K_PD);
				}
				else
				{
					completedSuccess = myIO.SetIOexpand_SX1509("00", "F8") && myIO.SetIOexpand_SX1508("10");
				}
				break;
			case "Set_5k_CC1_PD":
				if (myIO.isRevB)
				{
					completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Mode.CC1_5K_PD);
				}
				else
				{
					completedSuccess = myIO.SetIOexpand_SX1509("00", "F8") && myIO.SetIOexpand_SX1508("1C");
				}
				break;
			case "Get_ADC128D818value_A4":
				completedSuccess = myIO.Get_ADC128D818value(USB_IO_Expander.ADC_VBUS.UFP_VBUS_A4, out val);
				DebugMessage.Write("READING", "A4: " + val + "V");
				break;
			case "Get_ADC128D818value_A9":
				completedSuccess = myIO.Get_ADC128D818value(USB_IO_Expander.ADC_VBUS.UFP_VBUS_A9, out val);
				DebugMessage.Write("READING", "A9: " + val + "V");
				break;
			case "Get_ADC128D818value_B4":
				completedSuccess = myIO.Get_ADC128D818value(USB_IO_Expander.ADC_VBUS.UFP_VBUS_B4, out val);
				DebugMessage.Write("READING", "B4: " + val + "V");
				break;
			case "Get_ADC128D818value_B9":
				completedSuccess = myIO.Get_ADC128D818value(USB_IO_Expander.ADC_VBUS.UFP_VBUS_B9, out val);
				DebugMessage.Write("READING", "B9: " + val + "V");
				break;
			case "Get_ADC128D818value_VCONN":
				completedSuccess = myIO.Get_ADC128D818value(USB_IO_Expander.ADC_VBUS.VCONN, out val);
				DebugMessage.Write("READING", "VCONN: " + val + "V");
				break;
			case "Enable_Parallel_Charging":
				completedSuccess = myIO.EnableParallelCharging();
				break;
			case "Set_UFP_VBUS":
			{
				completedSuccess = myIO.Set_UFPVBUS(out var _, out var _, out var _, out var _);
				break;
			}
			case "I2C_CONTROL_TEST":
				if (myIO.isRevB)
				{
					completedSuccess = myIO.I2C_Read_RGB(USB_IO_Expander.LCD_Color.BLUE, ref colorVal);
				}
				else
				{
					completedSuccess = myIO.I2C_Read_RGB(USB_IO_Expander.LCD_Color.BLUE, ref colorVal);
				}
				break;
			case "I2C_Read_RED":
				if (myIO.isRevB)
				{
					completedSuccess = myIO.I2C_Read_RGB(USB_IO_Expander.LCD_Color.RED, ref colorVal);
				}
				else
				{
					completedSuccess = myIO.I2C_Read_RGB(USB_IO_Expander.LCD_Color.RED, ref colorVal);
				}
				break;
			case "I2C_Read_GREEN":
				if (myIO.isRevB)
				{
					completedSuccess = myIO.I2C_Read_RGB(USB_IO_Expander.LCD_Color.GREEN, ref colorVal);
				}
				else
				{
					completedSuccess = myIO.I2C_Read_RGB(USB_IO_Expander.LCD_Color.GREEN, ref colorVal);
				}
				break;
			case "I2C_Read_BLUE":
				if (myIO.isRevB)
				{
					completedSuccess = myIO.I2C_Read_RGB(USB_IO_Expander.LCD_Color.BLUE, ref colorVal);
				}
				else
				{
					completedSuccess = myIO.I2C_Read_RGB(USB_IO_Expander.LCD_Color.BLUE, ref colorVal);
				}
				break;
			case "SET_MIC5_OUT":
				completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Control.RLY_TST_MIC5_IN, USB_IO_Expander.SetValue.HIGH) && myIO.I2C_Control_Output(USB_IO_Expander.I2C_Control.RLY_TST_MIC6_IN, USB_IO_Expander.SetValue.LOW);
				break;
			case "SET_MIC6_OUT":
				completedSuccess = myIO.I2C_Control_Output(USB_IO_Expander.I2C_Control.RLY_TST_MIC5_IN, USB_IO_Expander.SetValue.LOW) && myIO.I2C_Control_Output(USB_IO_Expander.I2C_Control.RLY_TST_MIC6_IN, USB_IO_Expander.SetValue.HIGH);
				break;
			case "UV_LED_EN":
				completedSuccess = myIO.Set_UV_LED(enable: true);
				break;
			case "UV_LED_DIS":
				completedSuccess = myIO.Set_UV_LED(enable: false);
				break;
			case "CAMERA_LED_EN":
				completedSuccess = myIO.Set_CAMERA_LED(enable: true);
				break;
			case "CAMERA_LED_DIS":
				completedSuccess = myIO.Set_CAMERA_LED(enable: false);
				break;
			default:
			{
				DebugMessage.Write("GUI Error", "No such a I2C command" + control_item);
				TextBox textBox3 = textBoxLog;
				textBox3.Text = textBox3.Text + Environment.NewLine + "GUI Error:No such a I2C command" + control_item;
				break;
			}
			}
			if (completedSuccess)
			{
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + control_item + " completed successuly";
			}
			else
			{
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "Error " + control_item;
			}
			break;
		}
		case 3:
			switch (control_item)
			{
			case "Test_All":
				completedSuccess = SelfTestAll();
				break;
			case "FVT_SPK1_EAR_IN":
				completedSuccess = myIO.FVT_SPK1_EAR_IN();
				break;
			case "FVT_SPK2_EAR_IN":
				completedSuccess = myIO.FVT_SPK2_EAR_IN();
				break;
			case "FVT_SPK3_EAR_IN":
				completedSuccess = myIO.FVT_SPK3_EAR_IN();
				break;
			case "FVT_MIC1OUT_MIC1":
				completedSuccess = myIO.FVT_MIC1OUT_MIC1();
				break;
			case "FVT_MIC1OUT_MIC2":
				completedSuccess = myIO.FVT_MIC1OUT_MIC2();
				break;
			case "FVT_MIC3OUT_MIC3":
				completedSuccess = myIO.FVT_MIC3OUT_MIC3();
				break;
			case "FVT_MIC3OUT_MIC4":
				completedSuccess = myIO.FVT_MIC3OUT_MIC4();
				break;
			case "FVT_MIC1OUT_MIC5":
				completedSuccess = myIO.FVT_MIC1OUT_MIC5();
				break;
			case "FVT_MIC3OUT_MIC6":
				completedSuccess = myIO.FVT_MIC3OUT_MIC6();
				break;
			case "FVT_SBU1OUT_DP1":
				completedSuccess = myIO.FVT_SBU1OUT_DP1();
				break;
			case "FVT_SBU1OUT_DM1":
				completedSuccess = myIO.FVT_SBU1OUT_DM1();
				break;
			case "FVT_SBU2OUT_DP2":
				completedSuccess = myIO.FVT_SBU2OUT_DP2();
				break;
			case "FVT_SBU2OUT_DM2":
				completedSuccess = myIO.FVT_SBU2OUT_DM2();
				break;
			case "FVT_HSMIC_HSSPK_L":
				completedSuccess = myIO.FVT_HSMIC_HSSPK_L();
				break;
			case "FVT_HSMIC_HSSPK_R":
				completedSuccess = myIO.FVT_HSMIC_HSSPK_R();
				break;
			case "FVT_VOL_UP_VIBE":
				completedSuccess = myIO.FVT_VOL_UP_VIBE();
				break;
			case "FVT_LIGHT1_VOL_DOWN":
				completedSuccess = myIO.FVT_LIGHT1_VOL_DOWN();
				break;
			case "FVT_LIGHT2_PWRON":
				completedSuccess = myIO.FVT_LIGHT2_PWRON();
				break;
			case "FVT_OTG_VBUS":
				completedSuccess = myIO.FVT_OTG_VBUS();
				break;
			case "FVT_CC1PU_VBUS_A4":
				completedSuccess = myIO.FVT_CC1PU_VBUS_A4();
				break;
			case "FVT_CC2PD_VBUS_B4":
				completedSuccess = myIO.FVT_CC2PD_VBUS_B4();
				break;
			case "FVT_CC1PD_VBUS_B9":
				completedSuccess = myIO.FVT_CC1PD_VBUS_B9();
				break;
			case "FVT_VBUSA4_CAMERA_LED":
				completedSuccess = myIO.FVT_VBUSA4_CAMERA_LED();
				break;
			case "FVT_VBUSA9_UV_LED":
				completedSuccess = myIO.FVT_VBUSA9_UV_LED();
				break;
			default:
			{
				DebugMessage.Write("GUI Error", "No such a I2C command" + control_item);
				TextBox textBox2 = textBoxLog;
				textBox2.Text = textBox2.Text + Environment.NewLine + "GUI Error:No such a I2C command" + control_item;
				break;
			}
			}
			if (completedSuccess)
			{
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + control_item + " completed successuly";
			}
			else
			{
				textBoxLog.Text = DebugMessage.str_debug_msg + Environment.NewLine + "Error " + control_item;
			}
			break;
		}
	}

	private void textBox3_Leave(object sender, EventArgs e)
	{
		try
		{
			int num = int.Parse(textBox3.Text);
			if (num > 7 || num < 0)
			{
				TextBox textBox = textBoxLog;
				string text = textBox.Text;
				textBox.Text = text + Environment.NewLine + "Fixture ID should be between 0 to 7" + Environment.NewLine + myIO.Response_Str;
				DebugMessage.Write("FixtureID Invalid", "Please set valid Fixture ID (0 to 7)");
			}
			else if (myIO.FixtureID != num)
			{
				myIO.usb_hdl = 999;
				myIO.FixtureID = num;
			}
		}
		catch
		{
			DebugMessage.Write("FixtureID Invalid", "Please set valid Fixture ID (0 to 7)");
		}
	}

	private bool SelfTestAll()
	{
		string text = "";
		bool result = true;
		string[] self_Tests = Self_Tests;
		foreach (string text2 in self_Tests)
		{
			if (!text2.Equals("Test_All") && !text2.Equals("UV_LED_TEST"))
			{
				if ((bool)typeof(USB_IO_Expander).GetMethod(text2).Invoke(myIO, null))
				{
					text = text + text2 + ": PASS" + Environment.NewLine;
					continue;
				}
				text = text + text2 + ": FAIL" + Environment.NewLine;
				result = false;
			}
		}
		DebugMessage.Write("Self Tests", Environment.NewLine + text);
		return result;
	}

	private void btnClear_Click(object sender, EventArgs e)
	{
		textBoxLog.Clear();
	}

	private void btnSaveAs_Click(object sender, EventArgs e)
	{
		string text = "C:\\log.txt";
		using SaveFileDialog saveFileDialog = new SaveFileDialog();
		saveFileDialog.Title = "Save file as Diag";
		saveFileDialog.InitialDirectory = Path.GetDirectoryName(text);
		saveFileDialog.FileName = text;
		saveFileDialog.Filter = "txt|*.txt";
		if (saveFileDialog.ShowDialog() == DialogResult.OK)
		{
			text = saveFileDialog.FileName;
			File.WriteAllText(text, textBoxLog.Text);
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.textBoxLog = new System.Windows.Forms.TextBox();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.radioButton3 = new System.Windows.Forms.RadioButton();
		this.radioButton2 = new System.Windows.Forms.RadioButton();
		this.radioButton1 = new System.Windows.Forms.RadioButton();
		this.radioButton6 = new System.Windows.Forms.RadioButton();
		this.groupBox2 = new System.Windows.Forms.GroupBox();
		this.radioButton5 = new System.Windows.Forms.RadioButton();
		this.radioButton4 = new System.Windows.Forms.RadioButton();
		this.comboBox1 = new System.Windows.Forms.ComboBox();
		this.button1 = new System.Windows.Forms.Button();
		this.textBox2 = new System.Windows.Forms.TextBox();
		this.button2 = new System.Windows.Forms.Button();
		this.groupBox3 = new System.Windows.Forms.GroupBox();
		this.label1 = new System.Windows.Forms.Label();
		this.textBox3 = new System.Windows.Forms.TextBox();
		this.btnClear = new System.Windows.Forms.Button();
		this.btnSaveAs = new System.Windows.Forms.Button();
		this.groupBox1.SuspendLayout();
		this.groupBox2.SuspendLayout();
		this.groupBox3.SuspendLayout();
		base.SuspendLayout();
		this.textBoxLog.AllowDrop = true;
		this.textBoxLog.BackColor = System.Drawing.SystemColors.Info;
		this.textBoxLog.Location = new System.Drawing.Point(493, 11);
		this.textBoxLog.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.textBoxLog.Multiline = true;
		this.textBoxLog.Name = "textBoxLog";
		this.textBoxLog.ReadOnly = true;
		this.textBoxLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
		this.textBoxLog.Size = new System.Drawing.Size(477, 411);
		this.textBoxLog.TabIndex = 0;
		this.textBoxLog.TextChanged += new System.EventHandler(TextBox1TextChanged);
		this.groupBox1.Controls.Add(this.radioButton3);
		this.groupBox1.Controls.Add(this.radioButton2);
		this.groupBox1.Controls.Add(this.radioButton1);
		this.groupBox1.Controls.Add(this.radioButton6);
		this.groupBox1.Location = new System.Drawing.Point(12, 98);
		this.groupBox1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Padding = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.groupBox1.Size = new System.Drawing.Size(452, 65);
		this.groupBox1.TabIndex = 1;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "Comtrol Type";
		this.radioButton3.AutoSize = true;
		this.radioButton3.Location = new System.Drawing.Point(239, 29);
		this.radioButton3.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.radioButton3.Name = "radioButton3";
		this.radioButton3.Size = new System.Drawing.Size(73, 21);
		this.radioButton3.TabIndex = 0;
		this.radioButton3.Text = "I2C_Ctl";
		this.radioButton3.UseVisualStyleBackColor = true;
		this.radioButton3.CheckedChanged += new System.EventHandler(RadioButton3CheckedChanged);
		this.radioButton2.AutoSize = true;
		this.radioButton2.Location = new System.Drawing.Point(142, 30);
		this.radioButton2.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.radioButton2.Name = "radioButton2";
		this.radioButton2.Size = new System.Drawing.Size(74, 21);
		this.radioButton2.TabIndex = 0;
		this.radioButton2.Text = "Mux Ctl";
		this.radioButton2.UseVisualStyleBackColor = true;
		this.radioButton2.CheckedChanged += new System.EventHandler(RadioButton2CheckedChanged);
		this.radioButton1.AutoSize = true;
		this.radioButton1.Checked = true;
		this.radioButton1.Location = new System.Drawing.Point(31, 30);
		this.radioButton1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.radioButton1.Name = "radioButton1";
		this.radioButton1.Size = new System.Drawing.Size(88, 21);
		this.radioButton1.TabIndex = 0;
		this.radioButton1.TabStop = true;
		this.radioButton1.Text = "Single Ctl";
		this.radioButton1.UseVisualStyleBackColor = true;
		this.radioButton1.CheckedChanged += new System.EventHandler(RadioButton1CheckedChanged);
		this.radioButton6.AutoSize = true;
		this.radioButton6.Location = new System.Drawing.Point(335, 30);
		this.radioButton6.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.radioButton6.Name = "radioButton6";
		this.radioButton6.Size = new System.Drawing.Size(96, 21);
		this.radioButton6.TabIndex = 0;
		this.radioButton6.Text = "Self_Tests";
		this.radioButton6.UseVisualStyleBackColor = true;
		this.radioButton6.CheckedChanged += new System.EventHandler(RadioButton6CheckedChanged);
		this.groupBox2.Controls.Add(this.radioButton5);
		this.groupBox2.Controls.Add(this.radioButton4);
		this.groupBox2.Location = new System.Drawing.Point(12, 182);
		this.groupBox2.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.groupBox2.Name = "groupBox2";
		this.groupBox2.Padding = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.groupBox2.Size = new System.Drawing.Size(452, 63);
		this.groupBox2.TabIndex = 2;
		this.groupBox2.TabStop = false;
		this.groupBox2.Text = "Set/Reset";
		this.radioButton5.AutoSize = true;
		this.radioButton5.Location = new System.Drawing.Point(231, 29);
		this.radioButton5.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.radioButton5.Name = "radioButton5";
		this.radioButton5.Size = new System.Drawing.Size(146, 21);
		this.radioButton5.TabIndex = 0;
		this.radioButton5.Text = "Reset (=LOW=\"0\")";
		this.radioButton5.UseVisualStyleBackColor = true;
		this.radioButton5.CheckedChanged += new System.EventHandler(RadioButton5CheckedChanged);
		this.radioButton4.AutoSize = true;
		this.radioButton4.Checked = true;
		this.radioButton4.Location = new System.Drawing.Point(31, 29);
		this.radioButton4.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.radioButton4.Name = "radioButton4";
		this.radioButton4.Size = new System.Drawing.Size(132, 21);
		this.radioButton4.TabIndex = 0;
		this.radioButton4.TabStop = true;
		this.radioButton4.Text = "Set (=HIGH=\"1\")";
		this.radioButton4.UseVisualStyleBackColor = true;
		this.radioButton4.CheckedChanged += new System.EventHandler(RadioButton4CheckedChanged);
		this.comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
		this.comboBox1.FormattingEnabled = true;
		this.comboBox1.Items.AddRange(new object[21]
		{
			"RLY_SOLENOID_PWRON", "RLY_POGO_AMP_N", "RLY_SINE_OUT", "RLY_CTP", "VIBE_DET", "RED_DET", "RLY_VBATT_HI_LO", "RLY_VBATT_MARGIN", "RLY_OTG_EN", "RLY_HS_DETECT",
			"RLY_SOLENOID_VOL_UP", "RLY_SOLENOID_VOL_DN", "OTG_VBUS_STAT", "LIGHT_DET1", "LIGHT_DET2", "PHOT_LED1", "TP26", "AUDIO_RLY_EN", "GRN_DET", "BLUE_DET",
			"TONE_DET_N"
		});
		this.comboBox1.Location = new System.Drawing.Point(81, 276);
		this.comboBox1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.comboBox1.Name = "comboBox1";
		this.comboBox1.Size = new System.Drawing.Size(383, 24);
		this.comboBox1.TabIndex = 4;
		this.comboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1SelectedIndexChanged);
		this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.button1.Location = new System.Drawing.Point(12, 264);
		this.button1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.button1.Name = "button1";
		this.button1.Size = new System.Drawing.Size(64, 44);
		this.button1.TabIndex = 5;
		this.button1.Text = "Send CMD";
		this.button1.UseVisualStyleBackColor = true;
		this.button1.Click += new System.EventHandler(Button1Click);
		this.textBox2.Location = new System.Drawing.Point(69, 46);
		this.textBox2.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.textBox2.Name = "textBox2";
		this.textBox2.Size = new System.Drawing.Size(364, 22);
		this.textBox2.TabIndex = 6;
		this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8f, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, 0);
		this.button2.ForeColor = System.Drawing.SystemColors.ControlText;
		this.button2.Location = new System.Drawing.Point(0, 33);
		this.button2.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.button2.Name = "button2";
		this.button2.Size = new System.Drawing.Size(68, 46);
		this.button2.TabIndex = 7;
		this.button2.Text = "Direct CMD";
		this.button2.UseVisualStyleBackColor = true;
		this.button2.Click += new System.EventHandler(Button2Click);
		this.groupBox3.Controls.Add(this.button2);
		this.groupBox3.Controls.Add(this.textBox2);
		this.groupBox3.ForeColor = System.Drawing.SystemColors.Desktop;
		this.groupBox3.Location = new System.Drawing.Point(12, 328);
		this.groupBox3.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.groupBox3.Name = "groupBox3";
		this.groupBox3.Padding = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.groupBox3.Size = new System.Drawing.Size(452, 94);
		this.groupBox3.TabIndex = 8;
		this.groupBox3.TabStop = false;
		this.groupBox3.Text = "Custom Command";
		this.label1.AutoSize = true;
		this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.label1.ForeColor = System.Drawing.SystemColors.Desktop;
		this.label1.Location = new System.Drawing.Point(8, 31);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(77, 17);
		this.label1.TabIndex = 9;
		this.label1.Text = "Fixture ID";
		this.textBox3.ForeColor = System.Drawing.SystemColors.HotTrack;
		this.textBox3.Location = new System.Drawing.Point(96, 28);
		this.textBox3.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		this.textBox3.Name = "textBox3";
		this.textBox3.Size = new System.Drawing.Size(293, 22);
		this.textBox3.TabIndex = 10;
		this.textBox3.Text = "0";
		this.textBox3.TextChanged += new System.EventHandler(TextBox3TextChanged);
		this.textBox3.Leave += new System.EventHandler(textBox3_Leave);
		this.btnClear.Location = new System.Drawing.Point(654, 434);
		this.btnClear.Name = "btnClear";
		this.btnClear.Size = new System.Drawing.Size(108, 44);
		this.btnClear.TabIndex = 11;
		this.btnClear.Text = "Clear";
		this.btnClear.UseVisualStyleBackColor = true;
		this.btnClear.Click += new System.EventHandler(btnClear_Click);
		this.btnSaveAs.Location = new System.Drawing.Point(831, 434);
		this.btnSaveAs.Name = "btnSaveAs";
		this.btnSaveAs.Size = new System.Drawing.Size(108, 44);
		this.btnSaveAs.TabIndex = 11;
		this.btnSaveAs.Text = "Save As";
		this.btnSaveAs.UseVisualStyleBackColor = true;
		this.btnSaveAs.Click += new System.EventHandler(btnSaveAs_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 16f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(976, 493);
		base.Controls.Add(this.btnSaveAs);
		base.Controls.Add(this.btnClear);
		base.Controls.Add(this.textBoxLog);
		base.Controls.Add(this.textBox3);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.groupBox3);
		base.Controls.Add(this.button1);
		base.Controls.Add(this.comboBox1);
		base.Controls.Add(this.groupBox2);
		base.Controls.Add(this.groupBox1);
		base.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "Form1";
		this.Text = "FixtureControlUtility";
		this.groupBox1.ResumeLayout(false);
		this.groupBox1.PerformLayout();
		this.groupBox2.ResumeLayout(false);
		this.groupBox2.PerformLayout();
		this.groupBox3.ResumeLayout(false);
		this.groupBox3.PerformLayout();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
