using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace QC.MMI.MMI_PCBA_Tester_Control;

public class USB_IO_Expander
{
	public enum USB_C_AUDIO_IN
	{
		DM1,
		DM2,
		DP1,
		DP2
	}

	public enum mux
	{
		AUDIO_IN_SEL,
		AUDIO_OUT_SEL,
		AUDIO_IN_DISABLE,
		AUDIO_DISABLE
	}

	public enum ADC_VBUS
	{
		UFP_VBUS_A4,
		UFP_VBUS_A9,
		UFP_VBUS_B4,
		UFP_VBUS_B9,
		VCONN
	}

	public enum MUX_Control
	{
		RLY_TST_MIC4_IN,
		RLY_TST_MIC3_IN,
		RLY_TST_MIC2_IN,
		RLY_TST_MIC1_IN,
		RLY_EAR_OUT,
		RLY_FF_SPKR_OUT,
		RLY_HPH_R_OUT,
		RLY_HPH_L_OUT,
		RLY_SPKR3,
		RLY_SPKR2,
		RLY_SPKR1,
		RLY_MIC3_OUT,
		RLY_MIC2_OUT,
		RLY_MIC1_OUT,
		TP30,
		TP31,
		AUDIO_IN_DISABLE,
		AUDIO_DISABLE
	}

	public enum IndividualControl
	{
		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,
		BLUE_DET,
		GRN_DET,
		LIGHT_DET1,
		LIGHT_DET2,
		OTG_VBUS_STAT,
		PHOT_LED1,
		RED_DET,
		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,
		VIBE_DET,
		OMTP_CTIA_SELECT,
		HUB_RESET_N,
		NOT_USED
	}

	public enum SetValue
	{
		LOW,
		HIGH
	}

	public enum I2C_Control
	{
		CC1_EN,
		CC2_EN,
		VCONN_EN,
		VBUS_A4_EN,
		VBUS_A9_EN,
		VBUS_B4_EN,
		VBUS_B9_EN,
		TESTER_QUICK_CHRG_VBUS_EN,
		USBC_HPH_SELECT,
		SBU1_2_SELECT,
		HS_SBU_MIC_SELECT,
		UV_LED_CNTRL,
		RLY_TST_MIC5_IN,
		RLY_TST_MIC6_IN,
		EMPTY_SLOT_14,
		EMPTY_SLOT_15,
		USB2_DP1_DM1_EN_N,
		USB2_DP2_DM2_EN_N,
		CAMERA_LED_CNTRL,
		CCx_MUX_S0,
		CCx_MUX_S1,
		USB2_AUDIO_SEL,
		USB2_CTRL,
		USB2_HUB_QUICKCHRG_SEL,
		USB3_SS_SEL
	}

	public enum I2C_Mode
	{
		INITIAL_STATE,
		CC1_56K_PU,
		CC1_10K_PU,
		CC2_56K_PU,
		CC2_10K_PU,
		CC1_5K_PD,
		CC_AUDIO_1K_PD,
		CC_VCONN_EN,
		CC_VCONN_DIS,
		POWER_DUT,
		POWER_OFF_DUT,
		CONNECT_DUT_CC1_SS1,
		CONNECT_DUT_CC2_SS2,
		PARALLEL_CHARGING,
		UFP_VBUS,
		DMDP_1_SPEAKER_CONNECT,
		DMDP_2_SPEAKER_CONNECT,
		TEST_MIC_SBU_CONNECT,
		SBU_DIN_CONNECT,
		UV_LED_CNTRL_EN,
		UV_LED_CNTRL_DIS,
		CAMERA_LED_CNTRL_EN,
		CAMERA_LED_CNTRL_DIS
	}

	public enum LCD_Color
	{
		RED,
		GREEN,
		BLUE
	}

	private const double MIN_V = 3.4;

	private const double MAX_V = 4.55;

	private const int RESOLUTION = 3;

	private const string usbIODLLName = "FwFhid.dll";

	private const double current_resistor_value = 0.05;

	private const double LSB_Shunt_V = 0.01;

	private const double LSB_BUS_V = 0.004;

	public List<int> Res_vals_in_range = new List<int>();

	public List<double> Vbatt_out_range = new List<double>();

	private bool RGBSenseInit;

	public bool Vbatt_table_updated;

	public bool Vbatt_table_loaded;

	public string Vbatt_Cal_File = "c:\\temp\\TempVbatCalFile.txt";

	public int MuxRdDelay = 100;

	public int usb_hdl;

	public double USBIO_Vdd = 5.0;

	public bool dbgMode = true;

	public int No_Res_byte = 5;

	public bool USB_Connection_valid;

	public bool deviceFoundWithMatchingID;

	private bool IO_initialized;

	private bool I2C_1509A_Initialized;

	private bool I2C_1509B_Initialized;

	private bool I2C_1508_Initialized;

	private bool devices_Initializied;

	private bool ADC128D818_Initialized;

	private bool USBIO_Initialized;

	private double[] Compensation_T = new double[3];

	private double[] Compensation_P = new double[10];

	private bool compensationFactors;

	public bool isRevB = true;

	public Dictionary<string, string> USBIO_dictionary = new Dictionary<string, string>
	{
		{ "AUDIO_IN_RLY_A0", "9F 04 00 " },
		{ "AUDIO_IN_RLY_A1", "9F 04 01 " },
		{ "AUDIO_IN_RLY_A2", "9F 04 02 " },
		{ "AUDIO_RLY_EN", "9F 04 03 " },
		{ "AUDIO_RLY_EN_N", "9F 05 01 " },
		{ "AUDIO_OUT_RLY_A0", "9F 04 04 " },
		{ "AUDIO_OUT_RLY_A1", "9F 04 05 " },
		{ "AUDIO_OUT_RLY_A2", "9F 04 06 " },
		{ "TESTER_ID1", "9E 01 00 01 " },
		{ "TESTER_ID0", "9E 01 01 01 " },
		{ "RLY_SINE_OUT", "9F 01 02 " },
		{ "VIBE_DET", "96 03 00 00 " },
		{ "RLY_CTP", "9F 01 04 " },
		{ "RED_DET", "96 04 00 00 " },
		{ "LIGHT_DET1", "96 08 00 00 " },
		{ "LIGHT_DET2", "96 09 00 00 " },
		{ "RLY_OTG_EN", "9F 02 04 " },
		{ "RLY_HS_DETECT", "9F 02 05 " },
		{ "RLY_SOLENOID_VOL_UP", "9F 02 06 " },
		{ "RLY_SOLENOID_VOL_DN", "9F 02 07 " },
		{ "OTG_VBUS_STAT", "9E 03 00 01 " },
		{ "RLY_SOLENOID_PWRON", "9F 03 01 " },
		{ "RLY_POGO_AMP_N", "9F 03 02 " },
		{ "PHOT_LED1", "9F 03 06 " },
		{ "TESTER_ID2", "9E 03 07 01 " },
		{ "VBAT_EN_N", "9F 04 07 " },
		{ "GRN_DET", "96 05 00 00 " },
		{ "BLUE_DET", "96 06 00 00 " },
		{ "TONE_DET_N", "9E 05 02 01 " },
		{ "OMTP_CTIA_SELECT", "9F 01 05 " },
		{ "HUB_RESET_N", "9F 05 01 " }
	};

	public Dictionary<I2C_Mode, string> I2C_State_Dictionary = new Dictionary<I2C_Mode, string>
	{
		{
			I2C_Mode.INITIAL_STATE,
			"0000 0000 0000 0011    0000 0000 0000 0011"
		},
		{
			I2C_Mode.CC1_56K_PU,
			"2222 2222 2222 2210    2222 2222 2221 1222"
		},
		{
			I2C_Mode.CC1_10K_PU,
			"2222 2222 2222 2210    2222 2222 2221 0222"
		},
		{
			I2C_Mode.CC2_56K_PU,
			"2222 2222 2222 2201    2222 2222 2221 1222"
		},
		{
			I2C_Mode.CC2_10K_PU,
			"2222 2222 2222 2201    2222 2222 2221 0222"
		},
		{
			I2C_Mode.CC1_5K_PD,
			"0000 0000 0000 0010    0000 0001 0000 1011"
		},
		{
			I2C_Mode.CC_AUDIO_1K_PD,
			"2222 2222 2222 2000    2222 2222 2220 0222"
		},
		{
			I2C_Mode.CC_VCONN_EN,
			"0000 0000 0000 0110    2222 2222 2221 1222"
		},
		{
			I2C_Mode.CC_VCONN_DIS,
			"2222 2222 2222 2010    2222 2222 2221 1222"
		},
		{
			I2C_Mode.POWER_DUT,
			"2222 2222 1111 1222    2222 2222 2222 2222"
		},
		{
			I2C_Mode.POWER_OFF_DUT,
			"2222 2222 0000 0222    2222 2222 2222 2222"
		},
		{
			I2C_Mode.CONNECT_DUT_CC1_SS1,
			"2222 2222 1111 1010    2222 2221 2121 1222"
		},
		{
			I2C_Mode.CONNECT_DUT_CC2_SS2,
			"2222 2222 1111 1001    2222 2221 2121 1222"
		},
		{
			I2C_Mode.PARALLEL_CHARGING,
			"2222 2222 1111 1100    2222 2222 2221 1222"
		},
		{
			I2C_Mode.UFP_VBUS,
			"0000 0000 0000 0010    0000 0001 0000 1011"
		},
		{
			I2C_Mode.DMDP_1_SPEAKER_CONNECT,
			"2200 2221 2222 2222    2222 2222 2002 2210"
		},
		{
			I2C_Mode.DMDP_2_SPEAKER_CONNECT,
			"2200 2221 2222 2222    2222 2222 2002 2201"
		},
		{
			I2C_Mode.TEST_MIC_SBU_CONNECT,
			"2200 2102 2222 2222    2222 2222 2222 2222"
		},
		{
			I2C_Mode.SBU_DIN_CONNECT,
			"0000 0101 0000 0011    0000 0000 0000 0011"
		},
		{
			I2C_Mode.UV_LED_CNTRL_EN,
			"2222 1222 2222 2222    2222 2222 2222 2222"
		},
		{
			I2C_Mode.UV_LED_CNTRL_DIS,
			"2222 0222 2222 2222    2222 2222 2222 2222"
		},
		{
			I2C_Mode.CAMERA_LED_CNTRL_EN,
			"2222 2222 2222 2222    2222 2222 2222 2122"
		},
		{
			I2C_Mode.CAMERA_LED_CNTRL_DIS,
			"2222 2222 2222 2222    2222 2222 2222 2022"
		}
	};

	public string Response_Str = "";

	public string I2C_Address_VBAT_current_sensor_Write = "80";

	public string I2C_Address_VBAT_current_sensor_Read = "81";

	public string I2C_Address_PC_VBUS_current_sensor_Write = "82";

	public string I2C_Address_PC_VBUS_current_sensor_Read = "83";

	public string I2C_Address_KeyPress_Write = "40";

	public string I2C_Address_KeyBacklight_Write = "90";

	public string I2C_Address_KeyBacklight_Read = "A0 01 91 02";

	public string I2C_Init_100K_noSlewCtl = "93 01 3B 00";

	public string I2C_ShuntV_Reg_address = "01";

	public string I2C_BUSV_Reg_address = "02";

	public string I2C_Address_POT_Write = "54";

	public string I2C_Address_POT_Read = "55";

	public string I2C_Address_PressureSense_BMP285_Write = "A0 00 EC ";

	public string I2C_Address_PressureSense_BMP285_Read = "A0 01 ED ";

	public string I2C_Address_HumiditySense_SHTW2_Write = "A0 00 E0 ";

	public string I2C_Address_HumiditySense_SHTW2_Read = "A0 01 E1 ";

	public string I2C_Address_IOexpand_SX1508_Write = "A0 00 46 ";

	public string I2C_Address_IOexpand_SX1509_Write = "A0 00 7C ";

	public string I2C_Address_TI_ADC128D818_Write = "A0 00 6E ";

	public string I2C_Address_TI_ADC128D818_Read = "A0 01 6F ";

	public string I2C_Address_RGBSense_Read = "A0 01 53 ";

	public string I2C_Address_RGBSense_Write = "A0 00 52 ";

	public string I2C_Address_IOexpand_SX1509A_Write = "A0 00 7C ";

	public string I2C_Address_IOexpand_SX1509B_Write = "A0 00 E2 ";

	public string I2C_Address_IOexpand_SX1509A_Read = "A0 01 7D ";

	public string I2C_Address_IOexpand_SX1509B_Read = "A0 01 E3 ";

	private int fixtureID;

	public int FixtureID
	{
		get
		{
			return fixtureID;
		}
		set
		{
			fixtureID = value;
		}
	}

	public void CTP_touch()
	{
		if (!isUSB_Connected())
		{
			DebugMessage.Write("Debug", "No USB connected");
		}
		string writePortBitCMD = "9F 01 04 ";
		int millisecondsTimeout = 100;
		WriteBit(writePortBitCMD, SetValue.HIGH);
		Thread.Sleep(millisecondsTimeout);
		WriteBit(writePortBitCMD, SetValue.LOW);
	}

	private string HexStringToBinary(string hex)
	{
		if (hex.Length != 4)
		{
			DebugMessage.Write("Debug", "Enter an input of length 4");
		}
		string text = "";
		int num = Convert.ToInt32(hex, 16);
		text = Convert.ToString(num, 2);
		while (text.Length < 16)
		{
			text = "0" + text;
		}
		return text;
	}

	private string BinaryStringToHex(string binary)
	{
		if (binary.Length != 16)
		{
			DebugMessage.Write("Debug", "Enter an input of length 16");
		}
		string text = "";
		int num = Convert.ToInt32(binary, 2);
		text = Convert.ToString(num, 16).ToUpper();
		while (text.Length < 4)
		{
			text = "0" + text;
		}
		return text;
	}

	public bool SetVBAT_Voltage(double VBAT_V)
	{
		SetValue setBit = SetValue.HIGH;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.VBAT_EN_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!Vbatt_table_updated)
		{
			if (!VBAT_Current(out var _))
			{
				return false;
			}
			if (!Create_I2C_Res_VBATT_table())
			{
				return false;
			}
			Vbatt_table_updated = true;
			Vbatt_table_loaded = true;
		}
		if (!Vbatt_table_loaded)
		{
			load_table_fromFile(Vbatt_Cal_File);
			Vbatt_table_loaded = true;
		}
		int resInput = 0;
		if (!getNearestResistance(VBAT_V, ref resInput))
		{
			return false;
		}
		string i2C_cmd_str = "A0 00 54 02 00 " + resInput.ToString("X2");
		if (!SEND_I2C_cmd(i2C_cmd_str, ref Response_Str))
		{
			return false;
		}
		DebugMessage.Write("READING", "Successfully Set VBAT_Voltage to " + VBAT_V + "V.");
		return true;
	}

	public bool Set_VBAT_HI()
	{
		return SetVBAT_Voltage(4.55);
	}

	public bool Set_VBAT_LOW()
	{
		return SetVBAT_Voltage(3.4);
	}

	public bool SetVBATT_Delta_V(double delta_v)
	{
		double voltage_reading = 0.0;
		if (!VBAT_Voltage(out voltage_reading))
		{
			return false;
		}
		if (!SetVBAT_Voltage(voltage_reading + delta_v))
		{
			DebugMessage.Write("Debug", "Error -- Set_VBAT");
			return false;
		}
		return true;
	}

	public bool SetVBATT_Decrease_200mV()
	{
		return SetVBATT_Delta_V(-0.2);
	}

	public bool SetVBATT_Decrease_100mV()
	{
		return SetVBATT_Delta_V(-0.1);
	}

	public bool SetVBATT_Increase_200mV()
	{
		return SetVBATT_Delta_V(0.2);
	}

	public bool SetVBATT_Increase_100mV()
	{
		return SetVBATT_Delta_V(0.1);
	}

	private bool getNearestResistance(double Vbatt, ref int resInput)
	{
		int num = Res_vals_in_range.Count();
		resInput = Res_vals_in_range[0];
		double num2 = Math.Abs(Vbatt - Vbatt_out_range[0]);
		double num3 = num2;
		for (int i = 1; i < num; i++)
		{
			num3 = Math.Abs(Vbatt - Vbatt_out_range[i]);
			if (num3 < num2)
			{
				resInput = Res_vals_in_range[i];
				num2 = num3;
			}
		}
		return true;
	}

	private bool Create_I2C_Res_VBATT_table()
	{
		int num = 88;
		bool IO_OUT = false;
		SetValue setBit = SetValue.HIGH;
		if (!Individual_Control(IndividualControl.RLY_OTG_EN, ref setBit, ref IO_OUT))
		{
			return false;
		}
		string i2C_cmd_str = "A0 00 54 02 00 " + num.ToString("X2");
		if (!SEND_I2C_cmd(i2C_cmd_str, ref Response_Str))
		{
			return false;
		}
		if (!VBAT_Voltage(out var voltage_reading))
		{
			return false;
		}
		if (voltage_reading < 1.0)
		{
			DebugMessage.Write("Debug", "VBAT NOT ENABLED PROPERLY, CHECK SX1509");
			return false;
		}
		while (voltage_reading > 3.44)
		{
			int num2 = --num;
			i2C_cmd_str = "A0 00 54 02 00 " + num2.ToString("X2");
			if (!SEND_I2C_cmd(i2C_cmd_str, ref Response_Str))
			{
				return false;
			}
			if (!VBAT_Voltage(out voltage_reading))
			{
				return false;
			}
		}
		while (voltage_reading < 3.36)
		{
			int num3 = ++num;
			i2C_cmd_str = "A0 00 54 02 00 " + num3.ToString("X2");
			if (!SEND_I2C_cmd(i2C_cmd_str, ref Response_Str))
			{
				return false;
			}
			if (!VBAT_Voltage(out voltage_reading))
			{
				return false;
			}
		}
		for (int i = num + 3; i < 128; i += 3)
		{
			if (!(voltage_reading < 4.59))
			{
				break;
			}
			i2C_cmd_str = "A0 00 54 02 00 " + i.ToString("X2");
			if (!SEND_I2C_cmd(i2C_cmd_str, ref Response_Str))
			{
				return false;
			}
			if (!VBAT_Voltage(out var voltage_reading2))
			{
				return false;
			}
			double num4 = (voltage_reading2 - voltage_reading) / 3.0;
			for (int j = 0; j < 3; j++)
			{
				if (!(voltage_reading < 4.59))
				{
					break;
				}
				Res_vals_in_range.Add(j + i - 3);
				Vbatt_out_range.Add(Math.Round(voltage_reading, 3));
				voltage_reading += num4;
			}
			voltage_reading = voltage_reading2;
		}
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.RLY_OTG_EN, ref setBit, ref IO_OUT))
		{
			DebugMessage.Write("Debug", "Error in setting RLY_OTG_EN");
			return false;
		}
		save_table_ToFile(Vbatt_Cal_File);
		return true;
	}

	public bool DUTOutputTesterSpeakerConnect(USB_C_AUDIO_IN input, int speaker)
	{
		if (speaker < 1 || speaker > 4)
		{
			DebugMessage.Write("Debug", "Please enter a valid number for speaker, 1-4");
			return false;
		}
		SetValue setBit = SetValue.HIGH;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.AUDIO_RLY_EN, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!Individual_Control(IndividualControl.RLY_POGO_AMP_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (input == USB_C_AUDIO_IN.DM1 || input == USB_C_AUDIO_IN.DP1)
		{
			if (isRevB)
			{
				if (!I2C_Control_Output(I2C_Mode.DMDP_1_SPEAKER_CONNECT))
				{
					return false;
				}
			}
			else if (!SetIOexpand_SX1509("06", "FF"))
			{
				return false;
			}
		}
		else if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Mode.DMDP_2_SPEAKER_CONNECT))
			{
				return false;
			}
		}
		else if (!SetIOexpand_SX1509("07", "FF"))
		{
			return false;
		}
		if (input == USB_C_AUDIO_IN.DM1 || input == USB_C_AUDIO_IN.DM2)
		{
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_HPH_L_OUT))
			{
				return false;
			}
		}
		else if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_HPH_R_OUT))
		{
			return false;
		}
		switch (speaker)
		{
		case 1:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_SPKR1))
			{
				return false;
			}
			break;
		case 2:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_SPKR2))
			{
				return false;
			}
			break;
		case 3:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_SPKR3))
			{
				return false;
			}
			break;
		}
		return true;
	}

	public bool Tester_Mic_SBU_Connect(int micNumber, int SBUNumber)
	{
		if (micNumber < 1 || micNumber > 6)
		{
			DebugMessage.Write("Debug", "Please enter a valid number for micNumber, 1-6");
			return false;
		}
		if (SBUNumber != 1 && SBUNumber != 2)
		{
			DebugMessage.Write("Debug", "Please enter a valid number for SBUNumber, 1 or 2");
			return false;
		}
		SetValue setBit = SetValue.LOW;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!Individual_Control(IndividualControl.RLY_POGO_AMP_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_MIC2_OUT))
		{
			return false;
		}
		if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Mode.TEST_MIC_SBU_CONNECT))
			{
				return false;
			}
		}
		else
		{
			SetIOexpand_SX1509("10", "FF");
		}
		switch (micNumber)
		{
		case 1:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC1_IN))
			{
				return false;
			}
			break;
		case 2:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC2_IN))
			{
				return false;
			}
			break;
		case 3:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC3_IN))
			{
				return false;
			}
			break;
		case 4:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC4_IN))
			{
				return false;
			}
			break;
		case 5:
			if (isRevB)
			{
				if (!Set_Mux_Select(MuxRdDelay, MUX_Control.AUDIO_IN_DISABLE))
				{
					return false;
				}
				if (!I2C_Control_Output(I2C_Control.RLY_TST_MIC5_IN, SetValue.HIGH))
				{
					return false;
				}
			}
			else
			{
				if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC4_IN))
				{
					return false;
				}
				SetIOexpand_SX1509("50", "FF");
			}
			break;
		case 6:
			if (isRevB)
			{
				if (!Set_Mux_Select(MuxRdDelay, MUX_Control.AUDIO_IN_DISABLE))
				{
					return false;
				}
				if (!I2C_Control_Output(I2C_Control.RLY_TST_MIC6_IN, SetValue.HIGH))
				{
					return false;
				}
			}
			else
			{
				if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC4_IN))
				{
					return false;
				}
				SetIOexpand_SX1509("90", "FF");
			}
			break;
		default:
			DebugMessage.Write("Debug", "Enter a valid test mic number");
			return false;
		}
		if (isRevB)
		{
			if (SBUNumber == 2)
			{
				if (!I2C_Control_Output(I2C_Control.SBU1_2_SELECT, SetValue.HIGH))
				{
					return false;
				}
				if (!I2C_Control_Output(I2C_Control.SBU1_2_SELECT, SetValue.LOW))
				{
					return false;
				}
			}
		}
		else if (SBUNumber == 2)
		{
			SetSX1509Bit(11, 1);
		}
		return true;
	}

	public bool SetSX1509Bit(int IO_location, int input)
	{
		if (isRevB)
		{
			DebugMessage.Write("Debug", "This function is obsolete, use I2C_Control_Output() instead");
			return false;
		}
		if (input > 1 || input < 0 || IO_location < 0 || IO_location > 15)
		{
			DebugMessage.Write("Debug", "Please enter a valid input.");
			return false;
		}
		I2C_Initialize_SX1509A();
		string i2C_cmd_str = "A0 00 7C 01 10";
		string text = "";
		string text2 = "";
		string text3 = "";
		string text4 = "";
		if (!SEND_I2C_cmd(i2C_cmd_str, ref Response_Str))
		{
			return false;
		}
		i2C_cmd_str = "A0 01 7D 01";
		if (!SEND_I2C_cmd(i2C_cmd_str, ref Response_Str))
		{
			return false;
		}
		text3 += Response_Str.Substring(6, 2);
		i2C_cmd_str = "A0 00 7C 01 11";
		if (!SEND_I2C_cmd(i2C_cmd_str, ref Response_Str))
		{
			return false;
		}
		i2C_cmd_str = "A0 01 7D 01";
		if (!SEND_I2C_cmd(i2C_cmd_str, ref Response_Str))
		{
			return false;
		}
		text3 += Response_Str.Substring(6, 2);
		text4 = HexStringToBinary(text3);
		text2 += text4.Substring(0, 15 - IO_location);
		text2 += input;
		if (IO_location != 0)
		{
			text2 += text4.Substring(16 - IO_location);
		}
		text = BinaryStringToHex(text2);
		SetIOexpand_SX1509(text.Substring(0, 2), text.Substring(2));
		return true;
	}

	private bool SPKR_EAR_IN_CONNECT(int speaker_num)
	{
		SetValue setBit = SetValue.LOW;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!I2C_Control_Output(I2C_Mode.INITIAL_STATE))
		{
			return false;
		}
		if (speaker_num < 1 || speaker_num > 3)
		{
			DebugMessage.Write("Debug", "Enter a valid speaker number, 1 - 3");
			return false;
		}
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.TONE_DET_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (setBit != SetValue.HIGH)
		{
			DebugMessage.Write("Debug", "Unexpected Tone Detector Reading, LOW when no input");
			return false;
		}
		switch (speaker_num)
		{
		case 1:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_SPKR1))
			{
				return false;
			}
			break;
		case 2:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_SPKR2))
			{
				return false;
			}
			break;
		case 3:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_SPKR3))
			{
				return false;
			}
			break;
		default:
			DebugMessage.Write("Debug", "Enter a valid speaker number");
			return false;
		}
		if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_EAR_OUT))
		{
			return false;
		}
		setBit = SetValue.HIGH;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!Individual_Control(IndividualControl.RLY_POGO_AMP_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		setBit = SetValue.LOW;
		IO_OUT = false;
		if (!Individual_Control(IndividualControl.TONE_DET_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (setBit != 0)
		{
			DebugMessage.Write("Debug", "Unexpected Tone Detector Reading, HIGH with input, expected low. Try tuning the variable resistor R255.");
			return false;
		}
		return true;
	}

	public bool FVT_SPK1_EAR_IN()
	{
		return SPKR_EAR_IN_CONNECT(1);
	}

	public bool FVT_SPK2_EAR_IN()
	{
		return SPKR_EAR_IN_CONNECT(2);
	}

	public bool FVT_SPK3_EAR_IN()
	{
		return SPKR_EAR_IN_CONNECT(3);
	}

	private bool Mic_Out_Mic_In_Connect(int mic_out, int mic_in)
	{
		if (mic_in < 1 || mic_in > 6)
		{
			DebugMessage.Write("Debug", "Enter a valid number for mic_in, 1-6");
			return false;
		}
		if (mic_out != 1 && mic_out != 3)
		{
			DebugMessage.Write("Debug", "Enter a valid number for mic_out, 1 or 3");
			return false;
		}
		SetValue setBit = SetValue.LOW;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!Individual_Control(IndividualControl.TONE_DET_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (setBit != SetValue.HIGH)
		{
			DebugMessage.Write("Debug", "Unexpected Tone Detector Reading, LOW when no input");
			return false;
		}
		switch (mic_out)
		{
		case 1:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_MIC1_OUT))
			{
				return false;
			}
			break;
		case 3:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_MIC3_OUT))
			{
				return false;
			}
			break;
		default:
			DebugMessage.Write("Debug", "Enter a valid value for mic_out");
			return false;
		}
		switch (mic_in)
		{
		case 1:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC1_IN))
			{
				return false;
			}
			break;
		case 2:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC2_IN))
			{
				return false;
			}
			break;
		case 3:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC3_IN))
			{
				return false;
			}
			break;
		case 4:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_TST_MIC4_IN))
			{
				return false;
			}
			break;
		case 5:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.AUDIO_IN_DISABLE))
			{
				return false;
			}
			if (isRevB)
			{
				if (!I2C_Control_Output(I2C_Control.RLY_TST_MIC5_IN, SetValue.HIGH))
				{
					return false;
				}
				if (!I2C_Control_Output(I2C_Control.RLY_TST_MIC6_IN, SetValue.LOW))
				{
					return false;
				}
			}
			else
			{
				if (!SetSX1509Bit(15, 0))
				{
					return false;
				}
				if (!SetSX1509Bit(14, 1))
				{
					return false;
				}
			}
			break;
		case 6:
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.AUDIO_IN_DISABLE))
			{
				return false;
			}
			if (isRevB)
			{
				if (!I2C_Control_Output(I2C_Control.RLY_TST_MIC5_IN, SetValue.LOW))
				{
					return false;
				}
				if (!I2C_Control_Output(I2C_Control.RLY_TST_MIC6_IN, SetValue.HIGH))
				{
					return false;
				}
			}
			else
			{
				if (!SetSX1509Bit(15, 1))
				{
					return false;
				}
				if (!SetSX1509Bit(14, 0))
				{
					return false;
				}
			}
			break;
		default:
			DebugMessage.Write("Debug", "Enter a valid value for mic_in");
			return false;
		}
		setBit = SetValue.HIGH;
		IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.RLY_POGO_AMP_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!Individual_Control(IndividualControl.TONE_DET_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (setBit != 0)
		{
			DebugMessage.Write("Debug", "Retry for Unexpected Tone Detector Reading, HIGH with input, expected low.");
			Thread.Sleep(2000);
			if (!Individual_Control(IndividualControl.TONE_DET_N, ref setBit, ref IO_OUT))
			{
				return false;
			}
			if (setBit != 0)
			{
				DebugMessage.Write("Debug", "Unexpected Tone Detector Reading, HIGH with input, expected low. Try tuning the variable resistor R255.");
				return false;
			}
		}
		return true;
	}

	public bool FVT_MIC1OUT_MIC1()
	{
		return Mic_Out_Mic_In_Connect(1, 1);
	}

	public bool FVT_MIC1OUT_MIC2()
	{
		return Mic_Out_Mic_In_Connect(1, 2);
	}

	public bool FVT_MIC3OUT_MIC3()
	{
		return Mic_Out_Mic_In_Connect(3, 3);
	}

	public bool FVT_MIC3OUT_MIC4()
	{
		return Mic_Out_Mic_In_Connect(3, 4);
	}

	public bool FVT_MIC1OUT_MIC5()
	{
		return Mic_Out_Mic_In_Connect(1, 5);
	}

	public bool FVT_MIC3OUT_MIC6()
	{
		return Mic_Out_Mic_In_Connect(3, 6);
	}

	private bool SBU_DIN_Connect(int SBU_num, char D_name, int D_num)
	{
		D_name = char.ToLower(D_name);
		if (SBU_num != 1 && SBU_num != 2)
		{
			DebugMessage.Write("Debug", "Enter a valid number for SBU_num, 1 or 2");
			return false;
		}
		if (!D_name.Equals('m') && !D_name.Equals('p'))
		{
			DebugMessage.Write("Debug", "Enter a valid letter for D_name, 'M' or 'P'");
			return false;
		}
		if (D_num != 1 && D_num != 2)
		{
			DebugMessage.Write("Debug", "Enter a valid number for D_num, 1 or 2");
			return false;
		}
		SetValue setBit = SetValue.LOW;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!Individual_Control(IndividualControl.TONE_DET_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (setBit != SetValue.HIGH)
		{
			DebugMessage.Write("Debug", "Unexpected Tone Detector Reading, LOW when no input");
			return false;
		}
		if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_MIC2_OUT))
		{
			return false;
		}
		if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Mode.SBU_DIN_CONNECT))
			{
				return false;
			}
			if (SBU_num == 0)
			{
				if (!I2C_Control_Output(I2C_Control.SBU1_2_SELECT, SetValue.LOW))
				{
					return false;
				}
			}
			else if (!I2C_Control_Output(I2C_Control.SBU1_2_SELECT, SetValue.HIGH))
			{
				return false;
			}
			if (D_num == 0)
			{
				if (!I2C_Control_Output(I2C_Control.USB2_DP1_DM1_EN_N, SetValue.LOW))
				{
					return false;
				}
			}
			else if (!I2C_Control_Output(I2C_Control.USB2_DP2_DM2_EN_N, SetValue.LOW))
			{
				return false;
			}
		}
		else
		{
			if (!SetIOexpand_SX1509("16", "FF"))
			{
				return false;
			}
			if (!SetSX1509Bit(11, SBU_num - 1))
			{
				return false;
			}
			if (!SetSX1509Bit(8, D_num - 1))
			{
				return false;
			}
		}
		switch (D_name)
		{
		case 'm':
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_HPH_L_OUT))
			{
				return false;
			}
			break;
		case 'p':
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_HPH_R_OUT))
			{
				return false;
			}
			break;
		default:
			return false;
		}
		setBit = SetValue.LOW;
		IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_POGO_AMP_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		setBit = SetValue.HIGH;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		Thread.Sleep(220);
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.TONE_DET_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (setBit != 0)
		{
			DebugMessage.Write("Debug", "Unexpected Tone Detector Reading, HIGH with input, expected low. Try tuning the variable resistor R255.");
			return false;
		}
		return true;
	}

	public bool FVT_SBU1OUT_DM1()
	{
		return SBU_DIN_Connect(1, 'm', 1);
	}

	public bool FVT_SBU1OUT_DP1()
	{
		return SBU_DIN_Connect(1, 'p', 1);
	}

	public bool FVT_SBU2OUT_DM2()
	{
		return SBU_DIN_Connect(2, 'm', 2);
	}

	public bool FVT_SBU2OUT_DP2()
	{
		return SBU_DIN_Connect(2, 'p', 2);
	}

	private bool HSMIC_HSSPK_Connect(char side)
	{
		side = char.ToLower(side);
		if (!side.Equals('l') && !side.Equals('r'))
		{
			DebugMessage.Write("Debug", "Enter a valid input for side, l or r");
			return false;
		}
		SetValue setBit = SetValue.LOW;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!Individual_Control(IndividualControl.TONE_DET_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (setBit != SetValue.HIGH)
		{
			DebugMessage.Write("Debug", "Unexpected Tone Detector Reading, LOW when no input");
			return false;
		}
		if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_MIC2_OUT))
		{
			return false;
		}
		switch (side)
		{
		case 'l':
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_HPH_L_OUT))
			{
				return false;
			}
			break;
		case 'r':
			if (!Set_Mux_Select(MuxRdDelay, MUX_Control.RLY_HPH_R_OUT))
			{
				return false;
			}
			break;
		default:
			return false;
		}
		if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Control.HS_SBU_MIC_SELECT, SetValue.LOW))
			{
				return false;
			}
			if (!I2C_Control_Output(I2C_Control.USBC_HPH_SELECT, SetValue.LOW))
			{
				return false;
			}
		}
		else if (!SetIOexpand_SX1509("00", "FF"))
		{
			return false;
		}
		setBit = SetValue.LOW;
		IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_POGO_AMP_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		setBit = SetValue.HIGH;
		if (!Individual_Control(IndividualControl.RLY_SINE_OUT, ref setBit, ref IO_OUT))
		{
			return false;
		}
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.TONE_DET_N, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (setBit != 0)
		{
			DebugMessage.Write("Debug", "Unexpected Tone Detector Reading, HIGH with input, expected low. Try tuning the variable resistor R255.");
			return false;
		}
		return true;
	}

	public bool FVT_HSMIC_HSSPK_L()
	{
		return HSMIC_HSSPK_Connect('l');
	}

	public bool FVT_HSMIC_HSSPK_R()
	{
		return HSMIC_HSSPK_Connect('r');
	}

	public bool FVT_VOL_UP_VIBE()
	{
		SetValue setBit = SetValue.LOW;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_SOLENOID_VOL_UP, ref setBit, ref IO_OUT))
		{
			return false;
		}
		double analog_Voltage_VIB_DETECT = Get_Analog_Voltage_VIB_DETECT();
		if (Math.Abs(analog_Voltage_VIB_DETECT - 0.0) > 0.5)
		{
			DebugMessage.Write("Debug", "Unexpected VIBE detector reading, HIGH when no input");
			return false;
		}
		setBit = SetValue.HIGH;
		if (!Individual_Control(IndividualControl.RLY_SOLENOID_VOL_UP, ref setBit, ref IO_OUT))
		{
			return false;
		}
		analog_Voltage_VIB_DETECT = Get_Analog_Voltage_VIB_DETECT();
		if (Math.Abs(analog_Voltage_VIB_DETECT - 5.0) > 0.5)
		{
			DebugMessage.Write("Debug", "Unexpected VIBE detector reading, low with input");
			return false;
		}
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.RLY_SOLENOID_VOL_UP, ref setBit, ref IO_OUT))
		{
			return false;
		}
		return true;
	}

	public bool FVT_LIGHT1_VOL_DOWN()
	{
		SetValue setBit = SetValue.LOW;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_SOLENOID_VOL_DN, ref setBit, ref IO_OUT))
		{
			return false;
		}
		double analog_Voltage_FLASHLIGHT_Sensor = Get_Analog_Voltage_FLASHLIGHT_Sensor();
		if (analog_Voltage_FLASHLIGHT_Sensor < 2.95)
		{
			DebugMessage.Write("Debug", "Unexpected LIGHT detector reading, HIGH when no input");
			return false;
		}
		setBit = SetValue.HIGH;
		if (!Individual_Control(IndividualControl.RLY_SOLENOID_VOL_DN, ref setBit, ref IO_OUT))
		{
			return false;
		}
		analog_Voltage_FLASHLIGHT_Sensor = Get_Analog_Voltage_FLASHLIGHT_Sensor();
		if (Math.Abs(analog_Voltage_FLASHLIGHT_Sensor - 0.0) > 0.5)
		{
			DebugMessage.Write("Debug", "Unexpected LIGHT detector reading, low with input");
			return false;
		}
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.RLY_SOLENOID_VOL_DN, ref setBit, ref IO_OUT))
		{
			return false;
		}
		return true;
	}

	public bool FVT_LIGHT2_PWRON()
	{
		SetValue setBit = SetValue.LOW;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_SOLENOID_PWRON, ref setBit, ref IO_OUT))
		{
			return false;
		}
		double analog_voltage_Indicator_LED_light_Sensor = Get_analog_voltage_Indicator_LED_light_Sensor();
		if (analog_voltage_Indicator_LED_light_Sensor < 2.95)
		{
			DebugMessage.Write("Debug", "Unexpected LIGHT detector reading, LOW when no input");
			return false;
		}
		setBit = SetValue.HIGH;
		if (!Individual_Control(IndividualControl.RLY_SOLENOID_PWRON, ref setBit, ref IO_OUT))
		{
			return false;
		}
		analog_voltage_Indicator_LED_light_Sensor = Get_analog_voltage_Indicator_LED_light_Sensor();
		if (Math.Abs(analog_voltage_Indicator_LED_light_Sensor - 0.0) > 0.5)
		{
			DebugMessage.Write("Debug", "Unexpected LIGHT detector reading, low with input");
			return false;
		}
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.RLY_SOLENOID_PWRON, ref setBit, ref IO_OUT))
		{
			return false;
		}
		return true;
	}

	public bool FVT_OTG_VBUS()
	{
		SetValue setBit = SetValue.LOW;
		bool IO_OUT = false;
		if (!Individual_Control(IndividualControl.RLY_OTG_EN, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!PC_VBUS_Voltage(out var voltage_reading))
		{
			return false;
		}
		if (Math.Abs(voltage_reading - 5.0) > 0.5)
		{
			DebugMessage.Write("Debug", "Unexpected LIGHT detector reading, LOW when no input");
			return false;
		}
		setBit = SetValue.HIGH;
		if (!Individual_Control(IndividualControl.RLY_OTG_EN, ref setBit, ref IO_OUT))
		{
			return false;
		}
		if (!PC_VBUS_Voltage(out voltage_reading))
		{
			return false;
		}
		if (voltage_reading >= 2.0)
		{
			DebugMessage.Write("Debug", "Unexpected LIGHT detector reading, low with input");
			return false;
		}
		setBit = SetValue.LOW;
		if (!Individual_Control(IndividualControl.RLY_OTG_EN, ref setBit, ref IO_OUT))
		{
			return false;
		}
		return true;
	}

	public bool FVT_CC1PU_VBUS_A4()
	{
		if (isRevB)
		{
			if (!I2C_Control_Output("0802", "001B"))
			{
				return false;
			}
		}
		else
		{
			if (!SetIOexpand_SX1509("20", "02"))
			{
				return false;
			}
			if (!SetIOexpand_SX1508("08"))
			{
				return false;
			}
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_A4, out var val))
		{
			return false;
		}
		double num = 100.0 * Math.Abs(val - 0.766) / 0.766;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 0.766 + " Measured: " + val);
			return false;
		}
		if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Control.CCx_MUX_S0, SetValue.LOW))
			{
				return false;
			}
		}
		else if (!SetIOexpand_SX1508("00"))
		{
			return false;
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_A4, out val))
		{
			return false;
		}
		num = 100.0 * Math.Abs(val - 2.52) / 2.52;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 2.52 + " Measured: " + val);
			return false;
		}
		return true;
	}

	public bool FVT_CC2PD_VBUS_B4()
	{
		if (isRevB)
		{
			if (!I2C_Control_Output("00A1", "000B"))
			{
				return false;
			}
		}
		else
		{
			if (!SetIOexpand_SX1509("00", "A1"))
			{
				return false;
			}
			if (!SetIOexpand_SX1508("08"))
			{
				return false;
			}
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_A9, out var val))
		{
			DebugMessage.Write("Debug", "Error reading bus voltage from ADC");
			return false;
		}
		double num = 100.0 * Math.Abs(val - 1.689) / 1.689;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 1.689 + " Measured: " + val);
			return false;
		}
		if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Control.CCx_MUX_S0, SetValue.LOW))
			{
				return false;
			}
		}
		else if (!SetIOexpand_SX1508("00"))
		{
			return false;
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_A9, out val))
		{
			return false;
		}
		num = 100.0 * Math.Abs(val - 0.455) / 0.455;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 0.455 + " Measured: " + val);
			return false;
		}
		return true;
	}

	public bool FVT_CC1PD_VBUS_B9()
	{
		if (isRevB)
		{
			if (!I2C_Control_Output("00C2", "000B"))
			{
				return false;
			}
		}
		else
		{
			if (!SetIOexpand_SX1509("00", "C0"))
			{
				return false;
			}
			if (!SetIOexpand_SX1508("08"))
			{
				return false;
			}
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_A9, out var val))
		{
			DebugMessage.Write("Debug", "Error reading bus voltage from ADC");
			return false;
		}
		double num = 100.0 * Math.Abs(val - 1.689) / 1.689;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 1.689 + " Measured: " + val);
			return false;
		}
		if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Control.CCx_MUX_S0, SetValue.LOW))
			{
				return false;
			}
		}
		else if (!SetIOexpand_SX1508("00"))
		{
			return false;
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_A9, out val))
		{
			return false;
		}
		num = 100.0 * Math.Abs(val - 0.455) / 0.455;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 0.455 + " Measured: " + val);
			return false;
		}
		return true;
	}

	public bool FVT_VBUSA4_CAMERA_LED()
	{
		if (isRevB)
		{
			if (!I2C_Control_Output("008B", "0003"))
			{
				return false;
			}
		}
		else
		{
			if (!SetIOexpand_SX1509("00", "8B"))
			{
				return false;
			}
			if (!SetIOexpand_SX1508("00"))
			{
				return false;
			}
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_B9, out var val))
		{
			return false;
		}
		double num = 100.0 * Math.Abs(val - 5.0) / 5.0;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 5.0 + " Measured: " + val);
			return false;
		}
		if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Control.CAMERA_LED_CNTRL, SetValue.HIGH))
			{
				return false;
			}
		}
		else if (!SetIOexpand_SX1508("04"))
		{
			return false;
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_B9, out val))
		{
			return false;
		}
		num = 100.0 * Math.Abs(val - 0.0787) / 0.0787;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 0.0787 + " Measured: " + val);
			return false;
		}
		return true;
	}

	public bool FVT_VBUSA9_UV_LED()
	{
		if (isRevB)
		{
			if (!I2C_Control_Output("0093", "0003"))
			{
				return false;
			}
		}
		else
		{
			if (!SetIOexpand_SX1509("00", "93"))
			{
				return false;
			}
			if (!SetIOexpand_SX1508("00"))
			{
				return false;
			}
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_B4, out var val))
		{
			return false;
		}
		double num = 100.0 * Math.Abs(val - 5.0) / 5.0;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 5.0 + " Measured: " + val);
			return false;
		}
		if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Control.UV_LED_CNTRL, SetValue.HIGH))
			{
				return false;
			}
		}
		else if (!SetIOexpand_SX1509("20", "93"))
		{
			return false;
		}
		if (!Get_ADC128D818value(ADC_VBUS.UFP_VBUS_B4, out val))
		{
			DebugMessage.Write("Debug", "Error reading bus voltage from ADC");
			return false;
		}
		num = 100.0 * Math.Abs(val - 0.0787) / 0.0787;
		if (num > 5.0)
		{
			DebugMessage.Write("Debug", "ERROR: VBUS value produced an unexpected output. Expected: " + 0.0787 + " Measured: " + val);
			return false;
		}
		return true;
	}

	public bool EnableParallelCharging()
	{
		if (isRevB)
		{
			if (!I2C_Control_Output(I2C_Mode.POWER_OFF_DUT))
			{
				return false;
			}
			if (!I2C_Control_Output(I2C_Mode.PARALLEL_CHARGING))
			{
				return false;
			}
			if (!I2C_Control_Output(I2C_Mode.POWER_DUT))
			{
				return false;
			}
		}
		else
		{
			SetSX1509Bit(7, 0);
			if (!SetIOexpand_SX1509("00", "FE") || !SetIOexpand_SX1508("10"))
			{
				return false;
			}
			SetSX1509Bit(7, 1);
		}
		Set_VBAT_LOW();
		if (!VBAT_Voltage(out var _))
		{
			return false;
		}
		return true;
	}

	public bool I2C_Read_RGB(LCD_Color color, ref double color_value)
	{
		double num = -1.0;
		color_value = -1.0;
		string I2C_response_str = "";
		string text = "01 B4";
		string text2;
		switch (color)
		{
		case LCD_Color.RED:
			text2 = "01 B6";
			break;
		case LCD_Color.GREEN:
			text2 = "01 B8";
			break;
		case LCD_Color.BLUE:
			text2 = "01 BA";
			break;
		default:
			DebugMessage.Write("Debug", "Invalid color input to I2C_READ_RGB");
			return false;
		}
		if (!RGBSenseInit)
		{
			if (!SEND_I2C_cmd(I2C_Address_RGBSense_Write + "01 92", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_RGBSense_Read + "01", ref I2C_response_str))
			{
				return false;
			}
			if (!I2C_response_str.Substring(6, 2).Equals("14"))
			{
				DebugMessage.Write("Debug", "Incorrect Device ID Read");
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_RGBSense_Write + "02 80 03", ref I2C_response_str))
			{
				return false;
			}
			RGBSenseInit = true;
		}
		int num2 = 0;
		while (num <= 0.0 && num2 < 5)
		{
			if (num2 > 0)
			{
				Thread.Sleep(200);
			}
			if (!SEND_I2C_cmd(I2C_Address_RGBSense_Write + text, ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_RGBSense_Read + "02", ref I2C_response_str))
			{
				return false;
			}
			I2C_response_str = I2C_response_str.Substring(9, 2) + I2C_response_str.Substring(6, 2);
			num = int.Parse(I2C_response_str, NumberStyles.HexNumber);
			num2++;
		}
		if (num2 == 5)
		{
			DebugMessage.Write("Error", "RGB Sensor encountered an error, check connections");
			return false;
		}
		num2 = 0;
		while (color_value == -1.0 && num2 < 5)
		{
			if (num2 > 0)
			{
				Thread.Sleep(200);
			}
			if (!SEND_I2C_cmd(I2C_Address_RGBSense_Write + text2, ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_RGBSense_Read + "02", ref I2C_response_str))
			{
				return false;
			}
			I2C_response_str = I2C_response_str.Substring(9, 2) + I2C_response_str.Substring(6, 2);
			color_value = int.Parse(I2C_response_str, NumberStyles.HexNumber);
			num2++;
		}
		if (num2 == 5)
		{
			DebugMessage.Write("Error", "RGB Sensor encountered an error, check connections");
			return false;
		}
		color_value = Math.Round(100.0 * (color_value / num), 2);
		DebugMessage.Write(color.ToString(), " " + color_value + "%");
		return true;
	}

	public bool Open_MMI_Applicaiton()
	{
		Process process = new Process();
		process.StartInfo.UseShellExecute = false;
		process.StartInfo.FileName = "adb.exe";
		process.StartInfo.CreateNoWindow = true;
		process.StartInfo.Arguments = "shell input keyevent 82";
		process.Start();
		process.Start();
		process.StartInfo.Arguments = "shell am start -n com.qualcomm.qti.qmmi/com.qualcomm.qti.qmmi.framework.MainActivity";
		process.Start();
		for (int i = 0; i < 10; i++)
		{
			if (process.HasExited)
			{
				break;
			}
			Thread.Sleep(50);
			if (i == 9)
			{
				DebugMessage.Write("Debug", "Error opening MMI application");
				return false;
			}
		}
		if (process.ExitCode == 0)
		{
			return true;
		}
		DebugMessage.Write("Debug", "Error opening the QMMI Application, trying again.");
		return false;
	}

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#1", ExactSpelling = true, SetLastError = true)]
	public static extern int FwFhidCountDevices(int PID, int Version);

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#2", ExactSpelling = true, SetLastError = true)]
	public static extern int FwFhidGetIndexDevType(int Index, int Pid, int Version);

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#3", ExactSpelling = true, SetLastError = true)]
	public static extern string FwFhidGetInfoString(int Index, int InfoIdx, int PID, int version);

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#4", ExactSpelling = true, SetLastError = true)]
	public static extern byte FwFhidGetInfoStringCh(byte ChNum, int Index, int InfoIdx, int PID, int Version);

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#5", ExactSpelling = true, SetLastError = true)]
	public static extern int FwFhidConnect(int Index, ref int PIDfound, int PIDsearch, int Version);

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#6", ExactSpelling = true, SetLastError = true)]
	public static extern int FwFhidRead(int Handle, int nBytes);

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#7", ExactSpelling = true, SetLastError = true)]
	public static extern int FwFhidWrite(int Handle, int nBytes);

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#8", ExactSpelling = true, SetLastError = true)]
	public static extern int FwFhidDisconnect(int Handle);

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#9", ExactSpelling = true, SetLastError = true)]
	public static extern int FwFhidGetArrayVal(int Handle, int Index);

	[DllImport("FwFhid.dll", CharSet = CharSet.Ansi, EntryPoint = "#10", ExactSpelling = true, SetLastError = true)]
	public static extern int FwFhidSetArrayVal(int Handle, int Index, int newVal);

	[DllImport("kernel32.dll")]
	private static extern void QueryPerformanceCounter(ref long ticks);

	public bool getInfoStr_Vbatt(int start_indest, int lenth, ref double[] info_array, double A = 2.5, double B = 100.0)
	{
		try
		{
			for (int i = start_indest; i < start_indest + lenth; i++)
			{
				info_array[i - start_indest] = A + (double)FwFhidGetArrayVal(usb_hdl, i) / B;
			}
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Error", "Getting Info String (" + ex.Message + ")\n");
			return false;
		}
		DebugMessage.Write("InfoStr", info_array.ToString() + ")\n");
		return true;
	}

	public bool getInfoStr(int start_indest, int lenth, ref int[] info_array)
	{
		try
		{
			for (int i = start_indest; i < start_indest + lenth; i++)
			{
				info_array[i - start_indest] = FwFhidGetArrayVal(usb_hdl, i);
			}
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Error", "Getting Info String (" + ex.Message + ")\n");
			return false;
		}
		DebugMessage.Write("InfoStr", info_array.ToString() + ")\n");
		return true;
	}

	public bool setInfoStr_Vbatt(int start_indest, int lenth, ref double[] info_array, double A = 2.5, double B = 100.0)
	{
		try
		{
			for (int i = start_indest; i < start_indest + lenth; i++)
			{
				int newVal = (int)(info_array[i - start_indest] - A) * 100;
				FwFhidSetArrayVal(usb_hdl, i, newVal);
			}
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Error", "Setting Info String (" + ex.Message + ")\n");
			return false;
		}
		return true;
	}

	public bool setInfoStr(int start_indest, int lenth, ref int[] info_array)
	{
		try
		{
			for (int i = start_indest; i < start_indest + lenth; i++)
			{
				FwFhidSetArrayVal(usb_hdl, i, info_array[i - start_indest]);
			}
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Error", "Setting Info String (" + ex.Message + ")\n");
			return false;
		}
		return true;
	}

	public bool load_table_fromFile(string fileName)
	{
		Res_vals_in_range.Clear();
		Vbatt_out_range.Clear();
		try
		{
			if (!File.Exists(fileName))
			{
				DebugMessage.Write("Error", "File not Exist (" + fileName + ")\n");
				return false;
			}
			string[] array = File.ReadAllLines(fileName);
			string[] array2 = array[0].Split(',');
			string[] array3 = array[1].Split(',');
			int num = array2.Count();
			if (num != array3.Count())
			{
				DebugMessage.Write("Debug", "Mismatch in dimensions of Res and Vbatt arrays");
				return false;
			}
			for (int i = 0; i < num; i++)
			{
				int.TryParse(array2[i], out var result);
				double.TryParse(array3[i], out var result2);
				Res_vals_in_range.Add(result);
				Vbatt_out_range.Add(result2);
			}
			File.Delete(fileName);
			return true;
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Error", "Loading Vbatt Cal Table from File(" + fileName + ") Error info:" + ex.Message + ")\n");
			return false;
		}
	}

	public bool save_table_ToFile(string fileName)
	{
		try
		{
			if (File.Exists(fileName))
			{
				File.Delete(fileName);
			}
			string text = string.Join(",", Res_vals_in_range);
			string text2 = string.Join(",", Vbatt_out_range);
			string[] contents = new string[2] { text, text2 };
			File.WriteAllLines(fileName, contents);
			return true;
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Error", "Loading Vbatt Cal Table from File(" + fileName + ") Error info:" + ex.Message + ")\n");
			return false;
		}
	}

	public bool USB_Connect(int fixtureTesterID, out string USB_Name)
	{
		int PIDfound = 0;
		USB_Name = "";
		FixtureID = fixtureTesterID;
		deviceFoundWithMatchingID = false;
		try
		{
			FwFhidDisconnect(usb_hdl);
			int num = FwFhidCountDevices(-1, -1);
			for (int i = 0; i < num; i++)
			{
				usb_hdl = FwFhidConnect(i, ref PIDfound, -1, -1);
				if (usb_hdl != -1)
				{
					int uSBTesterID = GetUSBTesterID();
					if (uSBTesterID == FixtureID)
					{
						deviceFoundWithMatchingID = true;
						break;
					}
				}
			}
			if (!deviceFoundWithMatchingID)
			{
				DebugMessage.Write("Debug", "Fixture ID found=" + usb_hdl + " - not matching with FixtureID setting [" + FixtureID + "]");
				throw new Exception("Failed - please check if USB is connected and ID is correct");
			}
			DebugMessage.Write("Debug", "USB found =" + usb_hdl + ": PID = " + PIDfound);
			char c = ' ';
			int num2 = 0;
			while (c != 0)
			{
				USB_Name += (c = (char)FwFhidGetInfoStringCh((byte)num2, 0, 1, -1, -1));
				num2++;
			}
			USB_Name = USB_Name.Replace("\0", "");
			if (USB_Name.StartsWith("expandIO-USB"))
			{
				DebugMessage.Write("Debug", "USB DIO Connected =" + USB_Name.StartsWith("expandIO-USB"));
				USB_Connection_valid = true;
				return true;
			}
			DebugMessage.Write("Debug", "No USB DIO found ");
			return false;
		}
		catch (Exception ex)
		{
			throw ex;
		}
	}

	public bool USB_DisConnect()
	{
		try
		{
			if (FwFhidDisconnect(usb_hdl) == 1)
			{
				DebugMessage.Write("Debug", "USB DIO Disconnected");
				return true;
			}
			DebugMessage.Write("Debug", "failed to disconnect DIO USB");
			return false;
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Debug", "failed to disconnect to DIO USB " + ex);
			return false;
		}
	}

	public bool IO_Inited()
	{
		return IO_initialized;
	}

	public string get_CMD_str(string p)
	{
		if (USBIO_dictionary.ContainsKey(p))
		{
			return USBIO_dictionary[p];
		}
		DebugMessage.Write("Error", "no command for " + p + ";");
		return "";
	}

	public int GetUSBTesterID()
	{
		bool IO_OUT = true;
		StringBuilder stringBuilder = new StringBuilder();
		SetValue setBit = SetValue.HIGH;
		Individual_Control(IndividualControl.TESTER_ID2, ref setBit, ref IO_OUT);
		if (setBit == SetValue.HIGH)
		{
			stringBuilder.Append("1");
		}
		else
		{
			stringBuilder.Append("0");
		}
		Individual_Control(IndividualControl.TESTER_ID1, ref setBit, ref IO_OUT);
		if (setBit == SetValue.HIGH)
		{
			stringBuilder.Append("1");
		}
		else
		{
			stringBuilder.Append("0");
		}
		Individual_Control(IndividualControl.TESTER_ID0, ref setBit, ref IO_OUT);
		if (setBit == SetValue.HIGH)
		{
			stringBuilder.Append("1");
		}
		else
		{
			stringBuilder.Append("0");
		}
		return Convert.ToInt16(stringBuilder.ToString(), 2);
	}

	private void MUX_Control_CMD(mux muxTocontrol, SetValue A0, SetValue A1, SetValue A2)
	{
		string txtWrite = "";
		string txtWrite2 = "";
		string txtWrite3 = "";
		string txtWrite4 = "";
		string text = "";
		string text2 = get_CMD_str("AUDIO_IN_RLY_A0");
		string text3 = get_CMD_str("AUDIO_IN_RLY_A1");
		string text4 = get_CMD_str("AUDIO_IN_RLY_A2");
		string text5 = get_CMD_str("AUDIO_RLY_EN");
		string text6 = "";
		string text7 = get_CMD_str("AUDIO_OUT_RLY_A0");
		string text8 = get_CMD_str("AUDIO_OUT_RLY_A1");
		string text9 = get_CMD_str("AUDIO_OUT_RLY_A2");
		if (isRevB)
		{
			text6 = get_CMD_str("AUDIO_RLY_EN_N");
			text = text6 + "00";
		}
		switch (muxTocontrol)
		{
		case mux.AUDIO_IN_SEL:
		{
			int num4 = (int)A0;
			txtWrite = text2 + num4.ToString("X2");
			int num5 = (int)A1;
			txtWrite2 = text3 + num5.ToString("X2");
			int num6 = (int)A2;
			txtWrite3 = text4 + num6.ToString("X2");
			txtWrite4 = text5 + "01";
			if (isRevB)
			{
				text = get_CMD_str("AUDIO_RLY_EN_N") + "00";
				Send_USBIO_CMD(text, ref Response_Str);
			}
			break;
		}
		case mux.AUDIO_OUT_SEL:
		{
			int num = (int)A0;
			txtWrite = text7 + num.ToString("X2");
			int num2 = (int)A1;
			txtWrite2 = text8 + num2.ToString("X2");
			int num3 = (int)A2;
			txtWrite3 = text9 + num3.ToString("X2");
			txtWrite4 = text5 + "01";
			break;
		}
		case mux.AUDIO_IN_DISABLE:
			txtWrite4 = text6 + "01";
			text = text5 + "01";
			Send_USBIO_CMD(text, ref Response_Str);
			break;
		case mux.AUDIO_DISABLE:
			txtWrite4 = text5 + "00";
			break;
		}
		if (muxTocontrol != mux.AUDIO_IN_DISABLE && muxTocontrol != mux.AUDIO_DISABLE)
		{
			Send_USBIO_CMD(txtWrite, ref Response_Str);
			Send_USBIO_CMD(txtWrite2, ref Response_Str);
			Send_USBIO_CMD(txtWrite3, ref Response_Str);
		}
		Send_USBIO_CMD(txtWrite4, ref Response_Str);
	}

	public bool Set_Mux_Select(int MuxRdDelay, MUX_Control m_ctl)
	{
		Response_Str = "";
		try
		{
			if (!isUSB_Connected())
			{
				DebugMessage.Write("Debug", "No USB connected");
				return false;
			}
			switch (m_ctl)
			{
			case MUX_Control.AUDIO_IN_DISABLE:
				MUX_Control_CMD(mux.AUDIO_IN_DISABLE, SetValue.LOW, SetValue.LOW, SetValue.LOW);
				break;
			case MUX_Control.AUDIO_DISABLE:
				MUX_Control_CMD(mux.AUDIO_DISABLE, SetValue.LOW, SetValue.LOW, SetValue.LOW);
				break;
			case MUX_Control.RLY_TST_MIC4_IN:
				MUX_Control_CMD(mux.AUDIO_IN_SEL, SetValue.LOW, SetValue.LOW, SetValue.LOW);
				break;
			case MUX_Control.RLY_TST_MIC3_IN:
				MUX_Control_CMD(mux.AUDIO_IN_SEL, SetValue.HIGH, SetValue.LOW, SetValue.LOW);
				break;
			case MUX_Control.RLY_TST_MIC2_IN:
				MUX_Control_CMD(mux.AUDIO_IN_SEL, SetValue.LOW, SetValue.HIGH, SetValue.LOW);
				break;
			case MUX_Control.RLY_TST_MIC1_IN:
				MUX_Control_CMD(mux.AUDIO_IN_SEL, SetValue.HIGH, SetValue.HIGH, SetValue.LOW);
				break;
			case MUX_Control.RLY_EAR_OUT:
				MUX_Control_CMD(mux.AUDIO_IN_SEL, SetValue.LOW, SetValue.LOW, SetValue.HIGH);
				break;
			case MUX_Control.RLY_FF_SPKR_OUT:
				MUX_Control_CMD(mux.AUDIO_IN_SEL, SetValue.HIGH, SetValue.LOW, SetValue.HIGH);
				break;
			case MUX_Control.RLY_HPH_R_OUT:
				MUX_Control_CMD(mux.AUDIO_IN_SEL, SetValue.LOW, SetValue.HIGH, SetValue.HIGH);
				break;
			case MUX_Control.RLY_HPH_L_OUT:
				MUX_Control_CMD(mux.AUDIO_IN_SEL, SetValue.HIGH, SetValue.HIGH, SetValue.HIGH);
				break;
			case MUX_Control.RLY_SPKR3:
				MUX_Control_CMD(mux.AUDIO_OUT_SEL, SetValue.LOW, SetValue.LOW, SetValue.LOW);
				break;
			case MUX_Control.RLY_SPKR2:
				MUX_Control_CMD(mux.AUDIO_OUT_SEL, SetValue.HIGH, SetValue.LOW, SetValue.LOW);
				break;
			case MUX_Control.RLY_SPKR1:
				MUX_Control_CMD(mux.AUDIO_OUT_SEL, SetValue.LOW, SetValue.HIGH, SetValue.LOW);
				break;
			case MUX_Control.RLY_MIC3_OUT:
				MUX_Control_CMD(mux.AUDIO_OUT_SEL, SetValue.HIGH, SetValue.HIGH, SetValue.LOW);
				break;
			case MUX_Control.RLY_MIC2_OUT:
				MUX_Control_CMD(mux.AUDIO_OUT_SEL, SetValue.LOW, SetValue.LOW, SetValue.HIGH);
				break;
			case MUX_Control.RLY_MIC1_OUT:
				MUX_Control_CMD(mux.AUDIO_OUT_SEL, SetValue.HIGH, SetValue.LOW, SetValue.HIGH);
				break;
			case MUX_Control.TP30:
				MUX_Control_CMD(mux.AUDIO_OUT_SEL, SetValue.LOW, SetValue.HIGH, SetValue.HIGH);
				break;
			case MUX_Control.TP31:
				MUX_Control_CMD(mux.AUDIO_OUT_SEL, SetValue.HIGH, SetValue.HIGH, SetValue.HIGH);
				break;
			}
			Thread.Sleep(MuxRdDelay);
			return true;
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Debug", "failed to Set Mux " + m_ctl.ToString() + ex);
			return false;
		}
	}

	public void directWrite(string strToWrite)
	{
		Send_USBIO_CMD(strToWrite, ref Response_Str);
	}

	public bool Individual_Control(IndividualControl i_ctl, ref SetValue setBit, ref bool IO_OUT)
	{
		Response_Str = "";
		try
		{
			if (!i_ctl.ToString().StartsWith("TESTER_ID") && !isUSB_Connected())
			{
				DebugMessage.Write("Debug", "No USB connected");
				return false;
			}
			string text = get_CMD_str(i_ctl.ToString());
			if (text == "")
			{
				return false;
			}
			if (text.StartsWith("9F"))
			{
				IO_OUT = true;
				if (!WriteBit(text, setBit))
				{
					return false;
				}
			}
			else
			{
				if (!text.StartsWith("9E"))
				{
					DebugMessage.Write("Debug", "Wrong single control command");
					return false;
				}
				IO_OUT = false;
				if (!ReadBit(text, ref setBit))
				{
					return false;
				}
			}
			return true;
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Debug", "failed single control command" + i_ctl.ToString() + ex);
			return false;
		}
	}

	public double Get_analog_voltage_LCD_light_Sensor()
	{
		if (!isUSB_Connected())
		{
			DebugMessage.Write("Debug", "No USB connected");
			return -999.99;
		}
		double AN_Voltage = -999.99;
		if (!ReadAnalogVoltage("96 06 00 00", ref AN_Voltage, USBIO_Vdd))
		{
			DebugMessage.Write("Error", "Get_analog_voltage_LCD_light_Sensor");
			return -999.99;
		}
		DebugMessage.Write("Debug", "Get_analog_voltage_LCD_light_Sensor=" + AN_Voltage + "(Volt)");
		return AN_Voltage;
	}

	public double Get_Analog_Voltage_LCD_Light_Sensor(LCD_Color color)
	{
		if (!isUSB_Connected())
		{
			DebugMessage.Write("Debug", "No USB connected");
			return -999.99;
		}
		double AN_Voltage = -999.99;
		switch (color)
		{
		case LCD_Color.RED:
			if (!ReadAnalogVoltage("96 04 00 00", ref AN_Voltage, USBIO_Vdd))
			{
				DebugMessage.Write("Error", "Get_analog_voltage_LCD_light_Sensor");
				return -999.99;
			}
			break;
		case LCD_Color.GREEN:
			if (!ReadAnalogVoltage("96 05 00 00", ref AN_Voltage, USBIO_Vdd))
			{
				DebugMessage.Write("Error", "Get_analog_voltage_LCD_light_Sensor");
				return -999.99;
			}
			break;
		case LCD_Color.BLUE:
			if (!ReadAnalogVoltage("96 06 00 00", ref AN_Voltage, USBIO_Vdd))
			{
				DebugMessage.Write("Error", "Get_analog_voltage_LCD_light_Sensor");
				return -999.99;
			}
			break;
		}
		DebugMessage.Write("Debug", "Get_analog_voltage_LCD_light_Sensor=" + AN_Voltage + "(Volt)");
		return AN_Voltage;
	}

	public double Get_Analog_Voltage_VIB_DETECT()
	{
		if (!isUSB_Connected())
		{
			DebugMessage.Write("Debug", "No USB connected");
			return -999.99;
		}
		double AN_Voltage = -999.99;
		if (!ReadAnalogVoltage("96 03 00 00", ref AN_Voltage, USBIO_Vdd))
		{
			DebugMessage.Write("Error", "Get_analog_voltage_LCD_light_Sensor");
			return -999.99;
		}
		return AN_Voltage;
	}

	public double Get_analog_voltage_Indicator_LED_light_Sensor()
	{
		if (!isUSB_Connected())
		{
			DebugMessage.Write("Debug", "No USB connected");
			return -999.99;
		}
		double AN_Voltage = -999.99;
		if (!ReadAnalogVoltage("96 09 00 00", ref AN_Voltage, USBIO_Vdd))
		{
			DebugMessage.Write("Error", "Get_analog_voltage_LCD_light_Sensor");
			return -999.99;
		}
		DebugMessage.Write("Debug", "Get_analog_voltage_Indicator_LED_light_Sensor=" + AN_Voltage + "(Volt)");
		return AN_Voltage;
	}

	public double Get_Analog_Voltage_FLASHLIGHT_Sensor()
	{
		if (!isUSB_Connected())
		{
			DebugMessage.Write("Debug", "No USB connected");
			return -999.99;
		}
		double AN_Voltage = -999.99;
		if (!ReadAnalogVoltage("96 08 00 00", ref AN_Voltage, USBIO_Vdd))
		{
			DebugMessage.Write("Error", "Get_analog_voltage_LCD_light_Sensor");
			return -999.99;
		}
		DebugMessage.Write("Debug", "Get_analog_voltage_Indicator_LED_light_Sensor=" + AN_Voltage + "(Volt)");
		return AN_Voltage;
	}

	public bool Set_Audio_CC1_CC2()
	{
		try
		{
			return I2C_Control_Output(I2C_Mode.CC_AUDIO_1K_PD);
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Debug", "failed to Set_Audio_CC1_CC2 " + ex);
			return false;
		}
	}

	public bool Set_SBU_Path(int path)
	{
		bool flag = false;
		try
		{
			flag = I2C_Control_Output(I2C_Mode.SBU_DIN_CONNECT);
			if (path == 0)
			{
				return flag & I2C_Control_Output(I2C_Control.SBU1_2_SELECT, SetValue.LOW);
			}
			return flag & I2C_Control_Output(I2C_Control.SBU1_2_SELECT, SetValue.HIGH);
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Debug", "failed to Set_SBU_Path " + ex);
			return false;
		}
	}

	public bool Enable_USB2_DPDM_Audio_Paths()
	{
		try
		{
			return I2C_Control_Output(I2C_Control.USB2_DP1_DM1_EN_N, SetValue.LOW) && I2C_Control_Output(I2C_Control.USB2_DP2_DM2_EN_N, SetValue.LOW) && I2C_Control_Output(I2C_Control.USB2_AUDIO_SEL, SetValue.LOW);
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Debug", "failed to Enable_USB2_DPDM_Audio_Paths " + ex);
			return false;
		}
	}

	public bool Set_HeadSet_Audio_Paths()
	{
		try
		{
			return I2C_Control_Output(I2C_Control.USBC_HPH_SELECT, SetValue.LOW) && I2C_Control_Output(I2C_Control.HS_SBU_MIC_SELECT, SetValue.HIGH);
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Debug", "failed to Set_HeadSet_Audio_Paths " + ex);
			return false;
		}
	}

	public bool VBAT_Current(out double current_reading)
	{
		return I2C_Measure_V1(out current_reading, I2C_Address_VBAT_current_sensor_Read, I2C_Address_VBAT_current_sensor_Write, measure_current: true);
	}

	public bool VBAT_Voltage(out double voltage_reading)
	{
		return I2C_Measure_V1(out voltage_reading, I2C_Address_VBAT_current_sensor_Read, I2C_Address_VBAT_current_sensor_Write, measure_current: false);
	}

	public bool PC_VBUS_Current(out double current_reading)
	{
		return I2C_Measure_V1(out current_reading, I2C_Address_PC_VBUS_current_sensor_Read, I2C_Address_PC_VBUS_current_sensor_Write, measure_current: true);
	}

	public bool PC_VBUS_Voltage(out double voltage_reading)
	{
		return I2C_Measure_V1(out voltage_reading, I2C_Address_PC_VBUS_current_sensor_Read, I2C_Address_PC_VBUS_current_sensor_Write, measure_current: false);
	}

	public bool PressKeys()
	{
		return I2C_PressAllKeys();
	}

	public bool SetKeyBacklight(int ioIndex = 0)
	{
		return I2C_Key_Backlight(ioIndex);
	}

	public bool ReadKeyBacklight(out string readVal)
	{
		return I2C_Read_KeyBacklight(out readVal);
	}

	public bool ReadPressureSensor_BMP285(out string readVal, out double temperature, out double pressure)
	{
		return I2C_Read_PressureSensor_BMP285(out readVal, out temperature, out pressure);
	}

	public bool ReadHumiditySensor_SHTW2(out string readVal, out double percentH)
	{
		return I2C_Read_HumiditySensor_SHTW2(out readVal, out percentH);
	}

	public bool ReadTemperatureSensor_SHTW2(out string readVal, out double TCelcius)
	{
		return I2C_Read_TemperatureSensor_SHTW2(out readVal, out TCelcius);
	}

	public bool SetIOexpand_SX1508(string val)
	{
		return I2C_Set_IOexpand_SX1508(val);
	}

	public bool SetIOexpand_SX1509(string valMSB, string valLSB)
	{
		return I2C_Set_IOexpand_SX1509(valMSB, valLSB);
	}

	public bool Get_ADC128D818value(ADC_VBUS reg, out double val)
	{
		return I2C_Get_ADC128D818value(reg, out val);
	}

	public bool Set_UFP_CC_PD_resistors()
	{
		return I2C_Control_Output(I2C_Mode.CC1_5K_PD);
	}

	public bool Power_OFF_DUT(int sleep = 500)
	{
		if (I2C_Control_Output(I2C_Mode.POWER_OFF_DUT))
		{
			Thread.Sleep(sleep);
			return true;
		}
		return false;
	}

	public bool Power_ON_DUT(int sleep = 5000)
	{
		if (I2C_Control_Output(I2C_Mode.POWER_DUT))
		{
			Thread.Sleep(sleep);
			return true;
		}
		return false;
	}

	public bool Set_UFPVBUS(out double A4, out double A9, out double B4, out double B9, bool reset = false)
	{
		A4 = (A9 = (B4 = (B9 = -1.0)));
		if (isRevB)
		{
			if (reset)
			{
				if (!I2C_Control_Output(I2C_Mode.POWER_OFF_DUT))
				{
					return false;
				}
				Thread.Sleep(1000);
			}
			if (!I2C_Control_Output(I2C_Mode.UFP_VBUS))
			{
				return false;
			}
			Thread.Sleep(500);
			if (reset)
			{
				if (!I2C_Control_Output(I2C_Mode.POWER_DUT))
				{
					return false;
				}
				Thread.Sleep(5000);
			}
		}
		else
		{
			SetSX1509Bit(7, 0);
			if (!SetIOexpand_SX1509("00", "00") || !SetIOexpand_SX1508("18"))
			{
				return false;
			}
			SetSX1509Bit(7, 1);
		}
		Get_ADC128D818value(ADC_VBUS.UFP_VBUS_A4, out A4);
		Get_ADC128D818value(ADC_VBUS.UFP_VBUS_A9, out A9);
		Get_ADC128D818value(ADC_VBUS.UFP_VBUS_B4, out B4);
		Get_ADC128D818value(ADC_VBUS.UFP_VBUS_B9, out B9);
		DebugMessage.Write("Debug", "VBUS A4 Measured: " + A4);
		DebugMessage.Write("Debug", "VBUS A9 Measured: " + A9);
		DebugMessage.Write("Debug", "VBUS B4 Measured: " + B4);
		DebugMessage.Write("Debug", "VBUS B9 Measured: " + B9);
		return true;
	}

	public bool Set_VCONN(out double vconn, bool reset = false)
	{
		vconn = -1.0;
		if (reset)
		{
			if (!I2C_Control_Output(I2C_Mode.POWER_OFF_DUT))
			{
				return false;
			}
			Thread.Sleep(1000);
		}
		if (!I2C_Control_Output(I2C_Mode.CC_VCONN_EN))
		{
			return false;
		}
		if (reset)
		{
			if (!I2C_Control_Output(I2C_Mode.POWER_DUT))
			{
				return false;
			}
			Thread.Sleep(5000);
		}
		Get_ADC128D818value(ADC_VBUS.VCONN, out vconn);
		DebugMessage.Write("Debug", "VBUS A4 Measured: " + vconn);
		if (!I2C_Control_Output(I2C_Mode.CC_VCONN_DIS))
		{
			return false;
		}
		return true;
	}

	public bool Set_UV_LED(bool enable)
	{
		if (enable)
		{
			if (!I2C_Control_Output(I2C_Mode.UV_LED_CNTRL_EN))
			{
				return false;
			}
		}
		else if (!I2C_Control_Output(I2C_Mode.UV_LED_CNTRL_DIS))
		{
			return false;
		}
		return true;
	}

	public bool Set_CAMERA_LED(bool enable)
	{
		if (enable)
		{
			if (!I2C_Control_Output(I2C_Mode.CAMERA_LED_CNTRL_EN))
			{
				return false;
			}
		}
		else if (!I2C_Control_Output(I2C_Mode.CAMERA_LED_CNTRL_DIS))
		{
			return false;
		}
		return true;
	}

	private int SUBSTR2ushort(string response_string, int index_byte, int number_of_byte)
	{
		string text = "";
		for (int i = 0; i < number_of_byte; i++)
		{
			text += response_string.Substring(3 * (index_byte + i), 2);
		}
		return ushort.Parse(text, NumberStyles.HexNumber);
	}

	private bool SEND_I2C_cmd(string I2C_cmd_str, ref string I2C_response_str)
	{
		if (!isUSB_Connected())
		{
			DebugMessage.Write("Debug", "No USB connected");
			return false;
		}
		Send_USBIO_CMD(I2C_cmd_str, ref Response_Str);
		if (I2C_cmd_str.StartsWith("A0"))
		{
			I2C_response_str = Response_Str;
			int num = Convert.ToInt32(Response_Str.Substring(3, 2));
			I2C_error_handling("I2C_cmd:" + I2C_cmd_str, num);
			if (num == 0)
			{
				return true;
			}
			return false;
		}
		return true;
	}

	private short SUBSTR2short(string response_string, int index_byte, int number_of_byte)
	{
		string text = "";
		for (int i = 0; i < number_of_byte; i++)
		{
			text += response_string.Substring(3 * (index_byte + i), 2);
		}
		return short.Parse(text, NumberStyles.HexNumber);
	}

	private bool I2C_Read(string I2C_cmd_str, out int I2C_Read_value)
	{
		I2C_Read_value = 0;
		if (SEND_I2C_cmd(I2C_cmd_str, ref Response_Str))
		{
			I2C_Read_value = SUBSTR2short(Response_Str, 2, 2);
			return true;
		}
		return false;
	}

	private string I2C_error_handling(string error_source, int I2C_Response_code)
	{
		switch (I2C_Response_code)
		{
		case 0:
			DebugMessage.Write("Debug", "I2C resoponse: " + error_source + ": Success");
			return "Success";
		case 1:
			DebugMessage.Write("Debug", "I2C resoponse: " + error_source + ": Bus Collision");
			return "Bus Collision";
		case 2:
			DebugMessage.Write("Debug", "I2C resoponse: " + error_source + ": Write Collision");
			return "Write Collision";
		case 3:
			DebugMessage.Write("Debug", "I2C resoponse: " + error_source + ": No Acknowledge Recieved");
			return "No Acknowledge Recieved";
		default:
			DebugMessage.Write("Debug", error_source + ":  Error");
			return " unknown error";
		}
	}

	private bool I2C_Measure_V1(out double reading_out, string I2C_address_read, string I2C_address_write, bool measure_current)
	{
		int num = 10;
		bool flag = true;
		string i2C_cmd_str = "A0 02 " + I2C_address_write + " 01 02 01";
		string text = "mA";
		string text2 = "current measurement";
		int[] array = new int[num];
		int num2 = 0;
		string I2C_response_str = "";
		reading_out = 0.0;
		if (!measure_current)
		{
			i2C_cmd_str = "A0 02 " + I2C_address_write + " 01 02 02";
			text = "V";
			text2 = "Voltage measurement";
		}
		try
		{
			if (!SEND_I2C_cmd(I2C_Init_100K_noSlewCtl, ref I2C_response_str))
			{
				return false;
			}
			for (int i = 0; i < num; i++)
			{
				if (!SEND_I2C_cmd(i2C_cmd_str, ref I2C_response_str))
				{
					return false;
				}
				array[i] = SUBSTR2short(Response_Str, 2, 2);
				if (!measure_current)
				{
					array[i] = SUBSTR2ushort(Response_Str, 2, 2) / 8;
				}
				DebugMessage.Write("Debug", "I2C shunt_V_samples (" + i + ") = " + array[i]);
			}
			if (flag)
			{
				int num3 = array.Min();
				int num4 = array.Max();
				if (num3 != num4)
				{
					int num5 = 0;
					int num6 = 0;
					for (int j = 0; j < num; j++)
					{
						if (array[j] != num3 && array[j] != num4)
						{
							num6 += array[j];
							num5++;
						}
					}
					if (num5 > 0)
					{
						reading_out = (double)(num6 / num5) / 0.05 * 0.01;
						if (!measure_current)
						{
							reading_out = (double)(num6 / num5) * 0.004;
						}
						DebugMessage.Write("Debug", "I2C " + text2 + " read success. (Removed " + (num - num5) + " extrem values out of " + num + " reading=" + reading_out + " " + text);
						return true;
					}
					reading_out = array.Average() / 0.05 * 0.01;
					if (!measure_current)
					{
						reading_out = array.Average() * 0.004;
					}
					DebugMessage.Write("Debug", "I2C " + text2 + "success: Reading=" + reading_out + " " + text);
					return true;
				}
				num2 = num3;
				reading_out = (double)num2 / 0.05 * 0.01;
				if (!measure_current)
				{
					reading_out = (double)num2 * 0.004;
				}
				DebugMessage.Write("Debug", "I2C " + text2 + "success: (all same) Reading=" + reading_out + " " + text);
				return true;
			}
			reading_out = array.Average() / 0.05 * 0.01;
			if (!measure_current)
			{
				reading_out = array.Average() * 0.004;
			}
			DebugMessage.Write("Debug", "I2C " + text2 + "success: Reading=" + reading_out + " " + text);
			return true;
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Debug", "failed to get I2C " + text2 + ex);
			return false;
		}
	}

	private bool I2C_PressAllKeys()
	{
		string I2C_response_str = "";
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 07 00", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(50);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 11", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 EE", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 EC", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 EA", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 00", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 02", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 04", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 06", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 08", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 20", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 22", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 24", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 26", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 28", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 40", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 42", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 44", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 46", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 48", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 60", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 62", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(100);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 64", ref I2C_response_str))
		{
			return false;
		}
		Thread.Sleep(200);
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyPress_Write + " 02 08 11", ref I2C_response_str))
		{
			return false;
		}
		return true;
	}

	private bool I2C_Key_Backlight(int ioIndex = 0)
	{
		if (ioIndex > 7 || ioIndex < 0)
		{
			return false;
		}
		string text = "";
		ioIndex += 8;
		text = ioIndex.ToString("X");
		string I2C_response_str = "";
		if (!SEND_I2C_cmd("A0 00 " + I2C_Address_KeyBacklight_Write + " 01 " + text + "C", ref I2C_response_str))
		{
			return false;
		}
		return true;
	}

	private bool I2C_Read_KeyBacklight(out string readValue)
	{
		string I2C_response_str = "";
		readValue = "";
		try
		{
			if (!SEND_I2C_cmd(I2C_Address_KeyBacklight_Read, ref I2C_response_str))
			{
				return false;
			}
			readValue = I2C_response_str;
			return true;
		}
		catch
		{
			return false;
		}
	}

	private bool LoadCompensationParamsFromPressureSensor()
	{
		string I2C_response_str = "";
		if (compensationFactors)
		{
			return true;
		}
		try
		{
			if (!SEND_I2C_cmd(I2C_Init_100K_noSlewCtl, ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Write + "01 D0", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str))
			{
				return false;
			}
			if (!I2C_response_str.Substring(6, 2).Equals("58"))
			{
				DebugMessage.Write("Debug", "Returned Wrong Product ID");
				return false;
			}
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Write + "02 F4 B7", ref I2C_response_str);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Write + "02 F5 28", ref I2C_response_str);
			string i2C_cmd_str;
			string text;
			short num3;
			for (int i = 0; i < 3; i++)
			{
				text = "";
				int num = 137 + i * 2;
				int num2 = num - 1;
				i2C_cmd_str = I2C_Address_PressureSense_BMP285_Write + "01 " + num.ToString("X2");
				string i2C_cmd_str2 = I2C_Address_PressureSense_BMP285_Write + "01 " + num2.ToString("X2");
				SEND_I2C_cmd(i2C_cmd_str, ref I2C_response_str);
				SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
				text += I2C_response_str.Substring(6, 2);
				SEND_I2C_cmd(i2C_cmd_str2, ref I2C_response_str);
				SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
				text += I2C_response_str.Substring(6, 2);
				num3 = Convert.ToInt16(text, 16);
				Compensation_T[i] = num3;
				if (i == 0)
				{
					int num4 = int.Parse(text, NumberStyles.HexNumber);
					Compensation_T[i] = num4;
				}
			}
			for (int j = 0; j < 9; j++)
			{
				text = "";
				int num = 143 + j * 2;
				int num2 = num - 1;
				i2C_cmd_str = I2C_Address_PressureSense_BMP285_Write + "01 " + num.ToString("X2");
				string i2C_cmd_str2 = I2C_Address_PressureSense_BMP285_Write + "01 " + num2.ToString("X2");
				SEND_I2C_cmd(i2C_cmd_str, ref I2C_response_str);
				SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
				text += I2C_response_str.Substring(6, 2);
				SEND_I2C_cmd(i2C_cmd_str2, ref I2C_response_str);
				SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
				text += I2C_response_str.Substring(6, 2);
				num3 = Convert.ToInt16(text, 16);
				Compensation_P[j] = num3;
				if (j == 0)
				{
					int num4 = int.Parse(text, NumberStyles.HexNumber);
					Compensation_P[j] = num4;
				}
			}
			i2C_cmd_str = I2C_Address_PressureSense_BMP285_Write + "01 A0";
			SEND_I2C_cmd(i2C_cmd_str, ref I2C_response_str);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
			text = I2C_response_str.Substring(6, 2);
			if (int.Parse(text.Substring(0, 1), NumberStyles.HexNumber) >= 8)
			{
				text = "FF" + text;
			}
			num3 = Convert.ToInt16(text, 16);
			Compensation_P[9] = num3;
		}
		catch
		{
			return false;
		}
		compensationFactors = true;
		return true;
	}

	private bool I2C_Read_PressureSensor_BMP285(out string readValue, out double temperature, out double pressure)
	{
		string I2C_response_str = "";
		readValue = "";
		string text = "";
		string text2 = "";
		temperature = (pressure = 0.0);
		try
		{
			if (!LoadCompensationParamsFromPressureSensor())
			{
				return false;
			}
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Write + "01 F7", ref I2C_response_str);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
			text2 += I2C_response_str.Substring(6, 2);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Write + "01 F8", ref I2C_response_str);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
			text2 += I2C_response_str.Substring(6, 2);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Write + "01 F9", ref I2C_response_str);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
			text2 += I2C_response_str.Substring(6, 1);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Write + "01 FA", ref I2C_response_str);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
			text += I2C_response_str.Substring(6, 2);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Write + "01 FB", ref I2C_response_str);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
			text += I2C_response_str.Substring(6, 2);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Write + "01 FC", ref I2C_response_str);
			SEND_I2C_cmd(I2C_Address_PressureSense_BMP285_Read + "01", ref I2C_response_str);
			text += I2C_response_str.Substring(6, 1);
			double num = int.Parse(text, NumberStyles.HexNumber);
			double num2 = int.Parse(text2, NumberStyles.HexNumber);
			double num3 = (num / 16384.0 - Compensation_T[0] / 1024.0) * Compensation_T[1];
			double num4 = (num / 131072.0 - Compensation_T[0] / 8192.0) * (num / 131072.0 - Compensation_T[0] / 8192.0) * Compensation_T[2];
			double num5 = (num3 + num4) / 5120.0;
			num3 = num5 * 2560.0 - 64000.0;
			num4 = num3 * num3 * Compensation_P[5] / 32768.0;
			num4 += num3 * Compensation_P[4] * 2.0;
			num4 = num4 / 4.0 + Compensation_P[3] * 65536.0;
			num3 = (Compensation_P[2] * num3 * num3 / 524288.0 + Compensation_P[1] * num3) / 524288.0;
			num3 = (1.0 + num3 / 32768.0) * Compensation_P[0];
			if (num3 == 0.0)
			{
				return false;
			}
			double num6 = 1048576.0 - num2;
			num6 = (num6 - num4 / 4096.0) * 6250.0 / num3;
			num3 = Compensation_P[8] * num6 * num6 / 2147483648.0;
			num4 = num6 * Compensation_P[7] / 32768.0;
			double num7 = num6 / 256.0;
			double num8 = num7 * num7 * num7 * Compensation_P[9] / 131072.0;
			num6 += (num3 + num4 + num8 + Compensation_P[6]) / 16.0;
			temperature = num5;
			pressure = num6;
			return true;
		}
		catch
		{
			return false;
		}
	}

	private bool I2C_Read_HumiditySensor_SHTW2(out string readValue, out double percentH)
	{
		string I2C_response_str = "";
		readValue = "";
		percentH = 0.0;
		try
		{
			if (!SEND_I2C_cmd(I2C_Init_100K_noSlewCtl, ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_HumiditySense_SHTW2_Write + "02 EF C8", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_HumiditySense_SHTW2_Read + "02", ref I2C_response_str))
			{
				return false;
			}
			if (!I2C_response_str.Substring(10, 1).Equals("7"))
			{
				I2C_response_str = "Incorrect product ID SHTW2 humidity sensor";
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_HumiditySense_SHTW2_Write + "02 5C 24", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_HumiditySense_SHTW2_Read + "03", ref I2C_response_str))
			{
				return false;
			}
			readValue = I2C_response_str.Substring(6, 5);
			readValue = readValue.Replace(" ", string.Empty);
			int num = int.Parse(readValue, NumberStyles.HexNumber);
			percentH = Math.Round(100.0 * ((double)num / 65536.0), 2);
			return true;
		}
		catch
		{
			return false;
		}
	}

	private bool I2C_Read_TemperatureSensor_SHTW2(out string readValue, out double TCelcius)
	{
		string I2C_response_str = "";
		readValue = "";
		TCelcius = 0.0;
		try
		{
			if (!SEND_I2C_cmd(I2C_Init_100K_noSlewCtl, ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_HumiditySense_SHTW2_Write + "02 EF C8", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_HumiditySense_SHTW2_Read + "02", ref I2C_response_str))
			{
				return false;
			}
			if (!I2C_response_str.Substring(10, 1).Equals("7"))
			{
				I2C_response_str = "Incorrect product ID SHTW2 humidity sensor";
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_HumiditySense_SHTW2_Write + "02 5C 24", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_HumiditySense_SHTW2_Read + "06", ref I2C_response_str))
			{
				return false;
			}
			readValue = I2C_response_str.Substring(15, 5);
			readValue = readValue.Replace(" ", string.Empty);
			int num = int.Parse(readValue, NumberStyles.HexNumber);
			TCelcius = Math.Round(175.0 * ((double)num / 65536.0) - 45.0, 2);
			return true;
		}
		catch
		{
			return false;
		}
	}

	private bool I2C_Set_IOexpand_SX1508(string IOvalue)
	{
		string I2C_response_str = "";
		if (!I2C_1508_Initialized && !I2C_Initialize_SX1508())
		{
			return false;
		}
		try
		{
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1508_Write + "02 08 " + IOvalue, ref I2C_response_str))
			{
				return false;
			}
			return true;
		}
		catch
		{
			return false;
		}
	}

	private bool I2C_Set_IOexpand_SX1509(string IOvalueMSB, string IOvalueLSB)
	{
		string I2C_response_str = "";
		if (!I2C_1509A_Initialized && !I2C_Initialize_SX1509A())
		{
			return false;
		}
		try
		{
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "02 10 " + IOvalueMSB, ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "02 11 " + IOvalueLSB, ref I2C_response_str))
			{
				return false;
			}
		}
		catch
		{
			return false;
		}
		return true;
	}

	private bool I2C_Initialize_SX1509A()
	{
		string I2C_response_str = "";
		if (I2C_1509A_Initialized)
		{
			return true;
		}
		if (!USBIO_Initialized)
		{
			Initialize_USBIO();
		}
		try
		{
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "02 0E 00", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "02 0F 00", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "02 06 FF", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "02 07 FF", ref I2C_response_str))
			{
				return false;
			}
		}
		catch
		{
			return false;
		}
		DebugMessage.Write("Debug", "Successfully initialized SX1509A");
		I2C_1509A_Initialized = true;
		return true;
	}

	private bool I2C_Initialize_SX1509B()
	{
		string I2C_response_str = "";
		if (I2C_1509B_Initialized)
		{
			return true;
		}
		if (!USBIO_Initialized)
		{
			Initialize_USBIO();
		}
		try
		{
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Write + "02 0E 00", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Write + "02 0F 00", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Write + "02 06 FF", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Write + "02 07 FF", ref I2C_response_str))
			{
				return false;
			}
		}
		catch
		{
			return false;
		}
		DebugMessage.Write("Debug", "Successfully initialized SX1509A");
		I2C_1509B_Initialized = true;
		return true;
	}

	private bool I2C_Initialize_SX1508()
	{
		string I2C_response_str = "";
		if (I2C_1508_Initialized)
		{
			return true;
		}
		if (!USBIO_Initialized)
		{
			Initialize_USBIO();
		}
		try
		{
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1508_Write + "02 07 00", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1508_Write + "02 03 FF", ref I2C_response_str))
			{
				return false;
			}
		}
		catch
		{
			return false;
		}
		I2C_1508_Initialized = true;
		DebugMessage.Write("Debug", "Initialized SX1508 Successfully");
		return true;
	}

	public bool InitializeDevices()
	{
		if (devices_Initializied)
		{
			return true;
		}
		if (!Initialize_USBIO())
		{
			return false;
		}
		if (!Initialize_ADC128D818())
		{
			return false;
		}
		if (isRevB)
		{
			if (!I2C_Initialize_SX1509A())
			{
				return false;
			}
			if (!I2C_Initialize_SX1509B())
			{
				return false;
			}
		}
		else
		{
			if (!I2C_Initialize_SX1508())
			{
				return false;
			}
			if (!I2C_Initialize_SX1509A())
			{
				return false;
			}
		}
		devices_Initializied = true;
		if (isRevB)
		{
			if (!SEND_I2C_cmd("A0 00 54 02 00 64", ref Response_Str))
			{
				return false;
			}
			if (!I2C_Control_Output(I2C_Mode.INITIAL_STATE))
			{
				return false;
			}
		}
		else
		{
			if (!SEND_I2C_cmd("A0 00 54 02 00 5A", ref Response_Str))
			{
				return false;
			}
			if (!SetIOexpand_SX1508("00"))
			{
				return false;
			}
			if (!SetIOexpand_SX1509("00", "03"))
			{
				return false;
			}
		}
		DebugMessage.Write("Debug", "Successfully initialized SX1508, SX1509, USBIO, and 128D818");
		return true;
	}

	private bool Initialize_USBIO()
	{
		string I2C_response_str = "";
		if (USBIO_Initialized)
		{
			return true;
		}
		if (!SEND_I2C_cmd(I2C_Init_100K_noSlewCtl, ref I2C_response_str))
		{
			DebugMessage.Write("Debug", "Failed to initialize USBIO");
			return false;
		}
		USBIO_Initialized = true;
		return true;
	}

	private bool Initialize_ADC128D818()
	{
		string I2C_response_str = "";
		if (ADC128D818_Initialized)
		{
			return true;
		}
		try
		{
			if (!SEND_I2C_cmd(I2C_Init_100K_noSlewCtl, ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Write + "01 3E", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Read + "01", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Write + "01 3F", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Read + "01", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Write + "01 0C", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Read + "01", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Write + "02 0B 03", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Write + "02 07 01", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Write + "02 08 E0", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Write + "02 03 FF", ref I2C_response_str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Write + "02 00 01", ref I2C_response_str))
			{
				return false;
			}
		}
		catch
		{
			DebugMessage.Write("Debug", "Failed to initialize ADC");
			return false;
		}
		ADC128D818_Initialized = true;
		return true;
	}

	private bool I2C_Get_ADC128D818value(ADC_VBUS reg, out double readValue)
	{
		string I2C_response_str = "";
		string text = "";
		readValue = 0.0;
		int num = 5;
		switch (reg)
		{
		case ADC_VBUS.UFP_VBUS_A4:
			text = "20";
			break;
		case ADC_VBUS.UFP_VBUS_A9:
			text = "21";
			break;
		case ADC_VBUS.UFP_VBUS_B4:
			text = "22";
			break;
		case ADC_VBUS.UFP_VBUS_B9:
			text = "23";
			break;
		case ADC_VBUS.VCONN:
			text = "24";
			break;
		}
		if (!ADC128D818_Initialized && !Initialize_ADC128D818())
		{
			return false;
		}
		if (string.IsNullOrWhiteSpace(text))
		{
			DebugMessage.Write("Debug", "Error selecting adc register");
			return false;
		}
		try
		{
			Thread.Sleep(20);
			string text2 = "";
			for (int i = 0; i < num; i++)
			{
				if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Write + "01 " + text, ref I2C_response_str))
				{
					return false;
				}
				if (!SEND_I2C_cmd(I2C_Address_TI_ADC128D818_Read + "03", ref I2C_response_str))
				{
					return false;
				}
				text2 = I2C_response_str.Substring(6, 4);
				text2 = text2.Replace(" ", string.Empty);
				readValue += (double)int.Parse(text2, NumberStyles.HexNumber) * 5.0 / 4096.0;
			}
			readValue = Math.Round(readValue / (double)num, 2);
			return true;
		}
		catch
		{
			DebugMessage.Write("Debug", "Error getting ADC value");
			return false;
		}
	}

	public bool ReadAnalogVoltage(string AN_Read_CMD, ref double AN_Voltage, double Vdd)
	{
		if (!isUSB_Connected())
		{
			DebugMessage.Write("Debug", "No USB connected");
			return false;
		}
		if (AN_Read_CMD.Substring(0, 2) != "96")
		{
			DebugMessage.Write("Debug", "Please use correct analog read command '96'");
			return false;
		}
		Send_USBIO_CMD(AN_Read_CMD, ref Response_Str);
		if (Response_Str != "" && Convert.ToInt32(Response_Str.Substring(3, 2)) < 16)
		{
			AN_Voltage = Math.Round((double)SUBSTR2short(Response_Str, 2, 2) / 1023.0 * Vdd, 2);
			DebugMessage.Write("Debug", "Read analog voltage successfuly");
			return true;
		}
		DebugMessage.Write("Debug", "Read analog voltage -- Error");
		return false;
	}

	private bool ReadBit(string ReadPortBitCMD, ref SetValue bit_status)
	{
		if (ReadPortBitCMD.Substring(0, 2) != "9E")
		{
			DebugMessage.Write("Debug", "Please use correct port bit read command '9E'");
			return false;
		}
		Send_USBIO_CMD(ReadPortBitCMD + "00", ref Response_Str);
		if (Response_Str != "")
		{
			switch (Convert.ToInt32(Response_Str.Substring(9, 2)))
			{
			case 0:
				bit_status = SetValue.LOW;
				return true;
			case 1:
				bit_status = SetValue.HIGH;
				return true;
			default:
				DebugMessage.Write("Debug", "ReadBit return Invalid bit status -- Error");
				return false;
			}
		}
		DebugMessage.Write("Debug", "ReadBit return Invalid bit status -- Error");
		return false;
	}

	private bool WriteBit(string WritePortBitCMD, SetValue bit_value)
	{
		if (WritePortBitCMD.Substring(0, 2) != "9F")
		{
			DebugMessage.Write("Debug", "Please use correct port bit write command '9F'");
			return false;
		}
		string Res_str = "";
		string text = WritePortBitCMD;
		int num = (int)bit_value;
		WritePortBitCMD = text + num.ToString("X2");
		Send_USBIO_CMD(WritePortBitCMD, ref Res_str);
		if (Res_str != "" && Res_str.Substring(0, 11) == WritePortBitCMD)
		{
			return true;
		}
		DebugMessage.Write("Debug", "WriteBit Error");
		return false;
	}

	private void Send_USBIO_CMD(string txtWrite, ref string Res_str)
	{
		string text = "";
		Res_str = "";
		txtWrite += " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
		DebugMessage.Write("Debug", "writing = " + txtWrite.Substring(0, No_Res_byte * 3 - 1));
		for (int i = 0; i < 64; i++)
		{
			text = txtWrite.Substring(3 * i, 2);
			int newVal = int.Parse(text, NumberStyles.AllowHexSpecifier);
			FwFhidSetArrayVal(usb_hdl, i, newVal);
		}
		FwFhidWrite(usb_hdl, 64);
		Thread.Sleep(10);
		FwFhidRead(usb_hdl, 64);
		Thread.Sleep(10);
		for (int j = 0; j < 64; j++)
		{
			Res_str = Res_str + string.Format(FwFhidGetArrayVal(usb_hdl, j).ToString("X2")) + " ";
		}
		DebugMessage.Write("Debug", "Response : " + Res_str.Substring(0, No_Res_byte * 3 - 1));
	}

	private bool isUSB_Connected()
	{
		bool flag = false;
		string txtWrite = "94 00 00 00";
		string Res_str = "";
		if (USB_Connection_valid)
		{
			Send_USBIO_CMD(txtWrite, ref Res_str);
		}
		else
		{
			Res_str = "11 11 11 11 11 11 11 11";
		}
		if (Res_str.Substring(0, 11) != "94 45 00 08")
		{
			if (!USB_Connect(fixtureID, out var _))
			{
				throw new Exception("Failed to connect to fixture USB");
			}
			return true;
		}
		return true;
	}

	public void IO_init()
	{
		string[] array = new string[31]
		{
			"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_OUT_RLY_A0", "AUDIO_OUT_RLY_A1", "AUDIO_OUT_RLY_A2", "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",
			"AUDIO_RLY_EN_N"
		};
		try
		{
			bool flag = true;
			bool flag2 = false;
			SetValue setValue = SetValue.LOW;
			string[] array2 = array;
			foreach (string text in array2)
			{
				string text2 = text;
				flag = true;
				flag2 = false;
				if (text2.StartsWith("AN_"))
				{
					text2 = text2.Substring(3);
					flag2 = true;
				}
				if (!isRevB)
				{
					switch (text)
					{
					case "OMTP_CTIA_SELECT":
					case "HUB_RESET_N":
					case "AUDIO_RLY_EN_N":
						flag2 = true;
						break;
					}
				}
				IndividualControl individualControl = IndividualControl.NOT_USED;
				string[] names = Enum.GetNames(typeof(IndividualControl));
				for (int j = 0; j < names.Count(); j++)
				{
					if (names[j] == text2)
					{
						individualControl = (IndividualControl)j;
						break;
					}
				}
				if (!flag2)
				{
					setValue = SetValue.LOW;
					if (!Individual_Control(individualControl, ref setValue, ref flag))
					{
						DebugMessage.Write("Debug", Environment.NewLine + "Error -- Sending Signle control: " + individualControl);
						continue;
					}
					DebugMessage.Write("Debug", Environment.NewLine + "Send Signle control successuly: " + individualControl);
					if (!flag)
					{
						DebugMessage.Write("Debug", string.Concat(Environment.NewLine, individualControl, " : ", setValue));
					}
					continue;
				}
				string text3 = get_CMD_str(individualControl.ToString());
				text3 = text3.TrimEnd();
				double AN_Voltage = 0.0;
				if (!ReadAnalogVoltage(text3, ref AN_Voltage, USBIO_Vdd))
				{
					DebugMessage.Write("Debug", Environment.NewLine + "Error -- Sending Signle control: Analog " + individualControl);
					continue;
				}
				DebugMessage.Write("Debug", Environment.NewLine + "Send Signle control successuly: Analog" + individualControl);
				DebugMessage.Write("Debug", string.Concat(Environment.NewLine, individualControl, " AN_Voltage : ", AN_Voltage, "(Volt)"));
			}
			if (PC_VBUS_Voltage(out var voltage_reading))
			{
				DebugMessage.Write("Debug", Environment.NewLine + "Get PC_VBUS_Voltage successuly, reading=" + Math.Round(voltage_reading, 2));
			}
			else
			{
				DebugMessage.Write("Debug", Environment.NewLine + "Error in Getting PC_VBUS_Voltage");
			}
			IO_initialized = true;
		}
		catch (Exception ex)
		{
			DebugMessage.Write("Error", Environment.NewLine + "Error Initlilizing USB IO" + ex.Message);
		}
	}

	private bool I2C_Control_Output(string regA, string regB)
	{
		InitializeDevices();
		string text = "";
		string text2 = "";
		string text3 = "";
		string text4 = "";
		string text5 = "";
		string text6 = "";
		string text7 = "";
		string text8 = "";
		regA = regA.Replace(" ", "");
		regB = regB.Replace(" ", "");
		int num = regA.Count();
		if (num != 16 && num != 4 && num != 0)
		{
			DebugMessage.Write("Error", "Invalid Input Arguments for I2C_Control_Output reg A, make sure input length is 16, 4, or 0 characters");
			return false;
		}
		if (num == 4)
		{
			regA = HexStringToBinary(regA);
		}
		num = regB.Count();
		if (num != 16 && num != 4 && num != 0)
		{
			DebugMessage.Write("Error", "Invalid Input Arguments for I2C_Control_Output reg B, make sure input length is 16, 4, or 0 characters");
			return false;
		}
		if (num == 4)
		{
			regB = HexStringToBinary(regB);
		}
		bool flag = !regA.Equals("2222222222222222") && !string.IsNullOrWhiteSpace(regA);
		bool flag2 = !regB.Equals("2222222222222222") && !string.IsNullOrWhiteSpace(regB);
		if (regA.Contains('2') && flag)
		{
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "01 10", ref Response_Str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Read + "01", ref Response_Str))
			{
				return false;
			}
			text3 += Response_Str.Substring(6, 2);
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "01 11", ref Response_Str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Read + "01", ref Response_Str))
			{
				return false;
			}
			text3 += Response_Str.Substring(6, 2);
			text = HexStringToBinary(text3);
		}
		if (regB.Contains('2') && flag2)
		{
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Write + "01 10", ref Response_Str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Read + "01", ref Response_Str))
			{
				return false;
			}
			text4 += Response_Str.Substring(6, 2);
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Write + "01 11", ref Response_Str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Read + "01", ref Response_Str))
			{
				return false;
			}
			text4 += Response_Str.Substring(6, 2);
			text2 = HexStringToBinary(text4);
		}
		if (flag)
		{
			for (int i = 0; i < 16; i++)
			{
				string text9 = regA.Substring(i, 1);
				text5 = ((!text9.Equals("2")) ? (text5 + text9) : (text5 + text.Substring(i, 1)));
			}
			text7 = BinaryStringToHex(text5);
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "02 10 " + text7.Substring(0, 2), ref Response_Str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509A_Write + "02 11 " + text7.Substring(2, 2), ref Response_Str))
			{
				return false;
			}
		}
		if (flag2)
		{
			for (int j = 0; j < 16; j++)
			{
				string text10 = regB.Substring(j, 1);
				text6 = ((!text10.Equals("2")) ? (text6 + text10) : (text6 + text2.Substring(j, 1)));
			}
			text8 = BinaryStringToHex(text6);
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Write + "02 10 " + text8.Substring(0, 2), ref Response_Str))
			{
				return false;
			}
			if (!SEND_I2C_cmd(I2C_Address_IOexpand_SX1509B_Write + "02 11 " + text8.Substring(2, 2), ref Response_Str))
			{
				return false;
			}
		}
		return true;
	}

	public bool I2C_Control_Output(I2C_Mode mode)
	{
		string text = I2C_State_Dictionary[mode];
		text = text.Replace(" ", "");
		string regA = text.Substring(0, 16);
		string regB = text.Substring(16);
		return I2C_Control_Output(regA, regB);
	}

	public bool I2C_Control_Output(I2C_Control reg, SetValue power)
	{
		string text = "2222222222222222";
		string text2 = "";
		bool flag = true;
		int num = (int)reg;
		if (num > 15)
		{
			num %= 16;
			flag = false;
		}
		text2 += text.Substring(0, 15 - num);
		text2 += (int)power;
		text2 += text.Substring(0, num);
		if (flag)
		{
			return I2C_Control_Output(text2, "");
		}
		return I2C_Control_Output("", text2);
	}
}
