using CzmCs;

namespace CapacitanceSingleMeasure
{
    public partial class CapacitanceSingleMeasure : Form
    {
        //----------------------------------------
        // NXCzmCX^X쐬
        //----------------------------------------
        Czm zm = new Czm();

        //----------------------------------------
        // O[oϐ̐錾
        //----------------------------------------
        public short g_id;    // foCXANZXpID

        public CapacitanceSingleMeasure()
        {
            InitializeComponent();
        }

        private void CapacitanceSingleMeasure_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // ftHg{^ݒ
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // foCX̏\XV
            //----------------------------------------
            textBox_DeviceName.Text = "ZM000";
            //----------------------------------------
            // `lԍpR{{bNX̍쐬
            //----------------------------------------
            comboBox_ChannelNumber.Items.Add("CH1");
            comboBox_ChannelNumber.Items.Add("CH2");
            //----------------------------------------
            // `lԍpR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_ChannelNumber.SelectedIndex = 0;
            //----------------------------------------
            // gpR{{bNX̍쐬
            //----------------------------------------
            comboBox_Frequency.Items.Add("120 Hz");
            comboBox_Frequency.Items.Add("1 kHz");
            //----------------------------------------
            // gpR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_Frequency.SelectedIndex = 0;
            //----------------------------------------
            // MxpR{{bNX̍쐬
            //----------------------------------------
            comboBox_Level.Items.Add("0.5 V");
            comboBox_Level.Items.Add("1 V");
            //----------------------------------------
            // MxpR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_Level.SelectedIndex = 0;
            //----------------------------------------
            // Xs[hpR{{bNX̍쐬
            //----------------------------------------
            comboBox_Speed.Items.Add("FAST");
            comboBox_Speed.Items.Add("NORMAL");
            comboBox_Speed.Items.Add("SLOW");
            //----------------------------------------
            // Xs[hpR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_Speed.SelectedIndex = 0;
            //----------------------------------------
            // ω񐔂̏\XV
            //----------------------------------------
            textBox_AverageCount.Text = "1";
            //----------------------------------------
            // 背WpR{{bNX̍쐬
            //----------------------------------------
            comboBox_Range.Items.Add("4 nF");
            comboBox_Range.Items.Add("40 nF");
            comboBox_Range.Items.Add("400 nF");
            comboBox_Range.Items.Add("4 F");
            comboBox_Range.Items.Add("40 F");
            comboBox_Range.Items.Add("400 F");
            comboBox_Range.Items.Add("1.4 mF");
            //----------------------------------------
            // 背WpR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_Range.SelectedIndex = 0;
            //----------------------------------------
            // H[hpR{{bNX̍쐬
            //----------------------------------------
            comboBox_Circuit.Items.Add("AUTO");
            comboBox_Circuit.Items.Add("񓙉H");
            comboBox_Circuit.Items.Add("񓙉H");
            //----------------------------------------
            // H[hpR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_Circuit.SelectedIndex = 0;
            //----------------------------------------
            // JnfBC̏\XV
            //----------------------------------------
            textBox_StartDelay.Text = "2";
            //----------------------------------------
            // gKo͗pR{{bNX̍쐬
            //----------------------------------------
            comboBox_TriggerSyncEnable.Items.Add("");
            comboBox_TriggerSyncEnable.Items.Add("L");
            //----------------------------------------
            // gKo͗pR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_TriggerSyncEnable.SelectedIndex = 0;
            //----------------------------------------
            // Mo̓fBC̏\XV
            //----------------------------------------
            textBox_SignalOutputDelay.Text = "0";
            //----------------------------------------
            // R^Ng`FbNpR{{bNX̍쐬
            //----------------------------------------
            comboBox_ContactCheck.Items.Add("");
            comboBox_ContactCheck.Items.Add("O");
            comboBox_ContactCheck.Items.Add("");
            comboBox_ContactCheck.Items.Add("O");
            //----------------------------------------
            // R^Ng`FbNpR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_ContactCheck.SelectedIndex = 0;
        }

        //================================================================================
        // ZmInit{^NbNۂ̏ɂȂ܂
        // foCX̏s܂
        //================================================================================
        private void button_Init_Click(object sender, EventArgs e)
        {
            string device_name;     // foCX
            string error_string;    // G[R[h
            int    ret;             // ߂l擾p

            //----------------------------------------
            // foCXeLXg{bNX擾
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // foCX
            //----------------------------------------
            ret = zm.Init(device_name, out g_id);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.Init = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmResetDevice{^NbNۂ̏ɂȂ܂
        // foCX̃Zbgs܂
        //================================================================================
        private void button_ResetDevice_Click(object sender, EventArgs e)
        {
            string error_string;    // G[R[h
            int    ret;             // ߂l擾p

            //----------------------------------------
            // foCXZbg
            //----------------------------------------
            ret = zm.ResetDevice(g_id);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.ResetDevice = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSetFrequency{^NbNۂ̏ɂȂ܂
        // g̐ݒs܂
        //================================================================================
        private void button_SetFrequency_Click(object sender, EventArgs e)
        {
            string error_string;      // G[R[h
            int    ret;               // ߂l擾p
            ushort channel_number;    // `lԍ
            ushort frequency;         // g

            //----------------------------------------
            // ݒ肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // ݒ肷gR{{bNX擾
            //----------------------------------------
            frequency = (ushort)comboBox_Frequency.SelectedIndex;
            //----------------------------------------
            // g̐ݒ
            //----------------------------------------
            ret = zm.SetFrequency(g_id, channel_number, frequency);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetFrequency = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSetLevel{^NbNۂ̏ɂȂ܂
        // Mx̐ݒs܂
        //================================================================================
        private void button_SetLevel_Click(object sender, EventArgs e)
        {
            string error_string;      // G[R[h
            int    ret;               // ߂l擾p
            ushort channel_number;    // `lԍ
            ushort level;             // Mx

            //----------------------------------------
            // ݒ肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // ݒ肷MxR{{bNX擾
            //----------------------------------------
            level = (ushort)comboBox_Level.SelectedIndex;
            //----------------------------------------
            // Mx̐ݒ
            //----------------------------------------
            ret = zm.SetLevel(g_id, channel_number, level);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetLevel = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSetSpeed{^NbNۂ̏ɂȂ܂
        // Xs[hƕω񐔂̐ݒs܂
        //================================================================================
        private void button_SetSpeed_Click(object sender, EventArgs e)
        {
            string error_string;      // G[R[h
            int    ret;               // ߂l擾p
            ushort channel_number;    // `lԍ
            ushort speed;             // Xs[h
            uint   average_count;     // ω

            //----------------------------------------
            // ݒ肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // ݒ肷鑪Xs[hR{{bNX擾
            //----------------------------------------
            speed = (ushort)comboBox_Speed.SelectedIndex;
            //----------------------------------------
            // ݒ肷镽ω񐔂eLXg{bNX擾
            //----------------------------------------
            if (uint.TryParse(textBox_AverageCount.Text, out average_count) == false)
            {
                average_count = 1;
            }
            //----------------------------------------
            // Xs[hƕω񐔂̐ݒ
            //----------------------------------------
            ret = zm.SetSpeed(g_id, channel_number, speed, average_count);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetSpeed = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSetRange{^NbNۂ̏ɂȂ܂
        // 背W̐ݒs܂
        //================================================================================
        private void button_SetRange_Click(object sender, EventArgs e)
        {
            string error_string;      // G[R[h
            int    ret;               // ߂l擾p
            ushort channel_number;    // `lԍ
            ushort range;             // 背W

            //----------------------------------------
            // ݒ肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // ݒ肷鑪背WR{{bNX擾
            //----------------------------------------
            range = (ushort)(comboBox_Range.SelectedIndex + 3);
            //----------------------------------------
            // 背W̐ݒ
            //----------------------------------------
            ret = zm.SetRange(g_id, channel_number, range);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetRange = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSetCircuit{^NbNۂ̏ɂȂ܂
        // H[h̐ݒs܂
        //================================================================================
        private void button_SetCircuit_Click(object sender, EventArgs e)
        {
            string error_string;      // G[R[h
            int    ret;               // ߂l擾p
            ushort channel_number;    // `lԍ
            ushort circuit;           // H[h

            //----------------------------------------
            // ݒ肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // ݒ肷铙H[hR{{bNX擾
            //----------------------------------------
            circuit = (ushort)comboBox_Circuit.SelectedIndex;
            //----------------------------------------
            // H[h̐ݒ
            //----------------------------------------
            ret = zm.SetCircuit(g_id, channel_number, circuit);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetCircuit = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSetStartDelay{^NbNۂ̏ɂȂ܂
        // JnfBC̐ݒs܂
        //================================================================================
        private void button_SetStartDelay_Click(object sender, EventArgs e)
        {
            string error_string;      // G[R[h
            int    ret;               // ߂l擾p
            ushort channel_number;    // `lԍ
            float  start_delay;       // JnfBC

            //----------------------------------------
            // ݒ肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // ݒ肷鑪JnfBCeLXg{bNX擾
            //----------------------------------------
            if (float.TryParse(textBox_StartDelay.Text, out start_delay) == false)
            {
                start_delay = 2;
            }
            //----------------------------------------
            // JnfBC̐ݒ
            //----------------------------------------
            ret = zm.SetStartDelay(g_id, channel_number, start_delay);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetStartDelay = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSetTriggerSyncEnable{^NbNۂ̏ɂȂ܂
        // gKo͂̐ݒs܂
        //================================================================================
        private void button_SetTriggerSyncEnable_Click(object sender, EventArgs e)
        {
            string error_string;           // G[R[h
            int    ret;                    // ߂l擾p
            ushort channel_number;         // `lԍ
            ushort trigger_sync_enable;    // gKo

            //----------------------------------------
            // ݒ肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // ݒ肷gKo͂R{{bNX擾
            //----------------------------------------
            trigger_sync_enable = (ushort)comboBox_TriggerSyncEnable.SelectedIndex;
            //----------------------------------------
            // gKo͂̐ݒ
            //----------------------------------------
            ret = zm.SetTriggerSyncEnable(g_id, channel_number, trigger_sync_enable);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetTriggerSyncEnable = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSetSignalOutputDelay{^NbNۂ̏ɂȂ܂
        // Mo̓fBC̐ݒs܂
        //================================================================================
        private void button_SetSignalOutputDelay_Click(object sender, EventArgs e)
        {
            string error_string;          // G[R[h
            int    ret;                   // ߂l擾p
            ushort channel_number;        // `lԍ
            float signal_output_delay;    // Mo̓fBC

            //----------------------------------------
            // ݒ肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // ݒ肷鑪Mo̓fBCeLXg{bNX擾
            //----------------------------------------
            if (float.TryParse(textBox_SignalOutputDelay.Text, out signal_output_delay) == false)
            {
                signal_output_delay = 0;
            }
            //----------------------------------------
            // Mo̓fBC̐ݒ
            //----------------------------------------
            ret = zm.SetSignalOutputDelay(g_id, channel_number, signal_output_delay);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetSignalOutputDelay = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSetContactCheck{^NbNۂ̏ɂȂ܂
        // R^Ng`FbN̐ݒs܂
        //================================================================================
        private void button_SetContactCheck_Click(object sender, EventArgs e)
        {
            string error_string;      // G[R[h
            int    ret;               // ߂l擾p
            ushort channel_number;    // `lԍ
            ushort contact_check;     // R^Ng`FbN

            //----------------------------------------
            // ݒ肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // ݒ肷R^Ng`FbNR{{bNX擾
            //----------------------------------------
            contact_check = (ushort)comboBox_ContactCheck.SelectedIndex;
            //----------------------------------------
            // R^Ng`FbN̐ݒ
            //----------------------------------------
            ret = zm.SetContactCheck(g_id, channel_number, contact_check);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetContactCheck = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmSingleCMeasure{^NbNۂ̏ɂȂ܂
        // s܂
        //================================================================================
        private void button_SingleCMeasure_Click(object sender, EventArgs e)
        {
            string error_string;      // G[R[h
            int    ret;               // ߂l擾p
            ushort channel_number;    // `lԍ
            float  capacitance;       // Óde
            float  dissipation;       // W
            uint   status;            // Xe[^X
            ushort contact_check;     // R^Ng`FbN

            //----------------------------------------
            // 肷`lԍR{{bNX擾
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 
            //----------------------------------------
            ret = zm.SingleCMeasure(g_id, channel_number, out capacitance, out dissipation, out status);
            //----------------------------------------
            // ZmSingleCMeasure֐I\XV
            //----------------------------------------
            if (ret == (int)CzmConst.ZM_ERR_SUCCESS)
            {
                //----------------------------------------
                // Ódeʂ̕\XV
                //----------------------------------------
                textBox_Capacitance.Text = string.Format("{0:e}", capacitance);
                //----------------------------------------
                // W̕\XV
                //----------------------------------------
                textBox_Dissipation.Text = string.Format("{0:f}", dissipation);
                //----------------------------------------
                // H[h̕\XV
                //----------------------------------------
                if (((status >> 24) & 0x03) == (uint)CzmConst.ZM_CIRCUIT_SERIAL)
                {
                    //----------------------------------------
                    // 񓙉Hɕ\XV
                    //----------------------------------------
                    textBox_Circuit.Text = "񓙉H";
                }
                else if (((status >> 24) & 0x03) == (uint)CzmConst.ZM_CIRCUIT_PARALLEL)
                {
                    //----------------------------------------
                    // 񓙉Hɕ\XV
                    //----------------------------------------
                    textBox_Circuit.Text = "񓙉H";
                }
                //----------------------------------------
                // R^Ng`FbNG[\XV
                // R^Ng`FbN̉ӏ́u-vɍXV
                //----------------------------------------
                zm.GetContactCheck(g_id, channel_number, out contact_check);
                //----------------------------------------
                // R^Ng`FbNȂ̂őSāu-vɍXV
                //----------------------------------------
                if (contact_check == (ushort)CzmConst.ZM_CONTACTCHECK_DISABLE)
                {
                    textBox_BeforeL.Text = "-";
                    textBox_BeforeH.Text = "-";
                    textBox_AfterL.Text = "-";
                    textBox_AfterH.Text = "-";
                }
                //----------------------------------------
                // R^Ng`FbNOȂ̂ő́u-vɍXV
                //----------------------------------------
                else if (contact_check == (ushort)CzmConst.ZM_CONTACTCHECK_BEFORE)
                {
                    textBox_BeforeL.Text = string.Format("{0:d}", (status >> 16) & 0x01);
                    textBox_BeforeH.Text = string.Format("{0:d}", (status >> 17) & 0x01);
                    textBox_AfterL.Text = "-";
                    textBox_AfterH.Text = "-";
                }
                //----------------------------------------
                // R^Ng`FbNȂ̂őÓu-vɍXV
                //----------------------------------------
                else if (contact_check == (ushort)CzmConst.ZM_CONTACTCHECK_AFTER)
                {
                    textBox_BeforeL.Text = "-";
                    textBox_BeforeH.Text = "-";
                    textBox_AfterL.Text = string.Format("{0:d}", (status >> 18) & 0x01);
                    textBox_AfterH.Text = string.Format("{0:d}", (status >> 19) & 0x01);
                }
                //----------------------------------------
                // R^Ng`FbNOȂ̂őSăXe[^X̒lɍXV
                //----------------------------------------
                else if (contact_check == (ushort)CzmConst.ZM_CONTACTCHECK_BOTH)
                {
                    textBox_BeforeL.Text = string.Format("{0:d}", (status >> 16) & 0x01);
                    textBox_BeforeH.Text = string.Format("{0:d}", (status >> 17) & 0x01);
                    textBox_AfterL.Text = string.Format("{0:d}", (status >> 18) & 0x01);
                    textBox_AfterH.Text = string.Format("{0:d}", (status >> 19) & 0x01);
                }
            }
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SingleCMeasure = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // ZmExit{^NbNۂ̏ɂȂ܂
        // foCX̏Is܂
        //================================================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            string error_string;    // G[R[h
            int    ret;             // ߂l擾p

            //----------------------------------------
            // I̎s
            //----------------------------------------
            ret = zm.Exit(g_id);
            //----------------------------------------
            // G[擾
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // bZ[W\
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.Exit = " + ret.ToString() + "F" + error_string;
        }

        //================================================================================
        // NbNۂ̏ɂȂ܂
        // foCX̏IsAAvP[VI܂
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // I̎s
            //----------------------------------------
            zm.Exit(g_id);
            //----------------------------------------
            // _CAOI
            //----------------------------------------
            this.Close();
        }
    }
}
