﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CzmCs;

namespace CapacitanceSingleMeasure
{
    public partial class CapacitanceSingleMeasure : Form
    {
        //----------------------------------------
        // Create instance Czm class
        //----------------------------------------
        Czm zm = new Czm();

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public short g_id;    // ID for device access

        public CapacitanceSingleMeasure()
        {
            InitializeComponent();
        }

        private void CapacitanceSingleMeasure_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Set the default button
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // Update the initial display of device name
            //----------------------------------------
            textBox_DeviceName.Text = "ZM000";
            //----------------------------------------
            // Create the combobox for channel No.
            //----------------------------------------
            comboBox_ChannelNumber.Items.Add("CH1");
            comboBox_ChannelNumber.Items.Add("CH2");
            //----------------------------------------
            // Change the selection of channel No. combobox to 0
            //----------------------------------------
            comboBox_ChannelNumber.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for frequency
            //----------------------------------------
            comboBox_Frequency.Items.Add("120 Hz");
            comboBox_Frequency.Items.Add("1 kHz");
            //----------------------------------------
            // Change the selection of frequency combobox to 0
            //----------------------------------------
            comboBox_Frequency.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for signal level
            //----------------------------------------
            comboBox_Level.Items.Add("0.5 V");
            comboBox_Level.Items.Add("1 V");
            //----------------------------------------
            // Change the selection of signal level combobox to 0
            //----------------------------------------
            comboBox_Level.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for measurement speed
            //----------------------------------------
            comboBox_Speed.Items.Add("FAST");
            comboBox_Speed.Items.Add("NORMAL");
            comboBox_Speed.Items.Add("SLOW");
            //----------------------------------------
            // Change the selection of measurement speed combobox to 0
            //----------------------------------------
            comboBox_Speed.SelectedIndex = 0;
            //----------------------------------------
            // Update the initial display of average count
            //----------------------------------------
            textBox_AverageCount.Text = "1";
            //----------------------------------------
            // Create the combobox for measurement range
            //----------------------------------------
            comboBox_Range.Items.Add("4 nF");
            comboBox_Range.Items.Add("40 nF");
            comboBox_Range.Items.Add("400 nF");
            comboBox_Range.Items.Add("4 uF");
            comboBox_Range.Items.Add("40 uF");
            comboBox_Range.Items.Add("400 uF");
            comboBox_Range.Items.Add("1.4 mF");
            //----------------------------------------
            // Change the selection of measurement range combobox to 0
            //----------------------------------------
            comboBox_Range.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for equivalent circuit mode
            //----------------------------------------
            comboBox_Circuit.Items.Add("AUTO");
            comboBox_Circuit.Items.Add("SERIAL");
            comboBox_Circuit.Items.Add("PARALLEL");
            //----------------------------------------
            // Change the selection of equivalent circuit mode combobox to 0
            //----------------------------------------
            comboBox_Circuit.SelectedIndex = 0;
            //----------------------------------------
            // Update the initial display of measurement start delay
            //----------------------------------------
            textBox_StartDelay.Text = "2";
            //----------------------------------------
            // Create the combobox for trigger synchronous output
            //----------------------------------------
            comboBox_TriggerSyncEnable.Items.Add("DISABLE");
            comboBox_TriggerSyncEnable.Items.Add("ENABLE");
            //----------------------------------------
            // Change the selection of trigger synchronous output combobox to 0
            //----------------------------------------
            comboBox_TriggerSyncEnable.SelectedIndex = 0;
            //----------------------------------------
            // Update the initial display of measurement signal output delay
            //----------------------------------------
            textBox_SignalOutputDelay.Text = "0";
            //----------------------------------------
            // Create the combobox for contact check
            //----------------------------------------
            comboBox_ContactCheck.Items.Add("DISABLE");
            comboBox_ContactCheck.Items.Add("BEFORE");
            comboBox_ContactCheck.Items.Add("AFTER");
            comboBox_ContactCheck.Items.Add("BOTH");
            //----------------------------------------
            // Change the selection of contact check combobox to 0
            //----------------------------------------
            comboBox_ContactCheck.SelectedIndex = 0;
        }

        //================================================================================
        // It is the process when the ZmInit button is clicked
        // Initialize the device
        //================================================================================
        private void button_Init_Click(object sender, EventArgs e)
        {
            string device_name;     // Device name
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Get device name from textbox
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // Device initialization
            //----------------------------------------
            ret = zm.Init(device_name, out g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.Init = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmResetDevice button is clicked
        // Reset the device
        //================================================================================
        private void button_ResetDevice_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Reset device
            //----------------------------------------
            ret = zm.ResetDevice(g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.ResetDevice = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSetFrequency button is clicked
        // Set the frequency
        //================================================================================
        private void button_SetFrequency_Click(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            int    ret;               // For return code
            ushort channel_number;    // Channel No.
            ushort frequency;         // Frequency

            //----------------------------------------
            // Get the channel number to set from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Get the frequency to set from combobox
            //----------------------------------------
            frequency = (ushort)comboBox_Frequency.SelectedIndex;
            //----------------------------------------
            // Set the frequency
            //----------------------------------------
            ret = zm.SetFrequency(g_id, channel_number, frequency);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetFrequency = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSetLevel button is clicked
        // Set the signal level
        //================================================================================
        private void button_SetLevel_Click(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            int    ret;               // For return code
            ushort channel_number;    // Channel No.
            ushort level;             // Signal level

            //----------------------------------------
            // Get the channel number to set from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Get the signal level to set from combobox
            //----------------------------------------
            level = (ushort)comboBox_Level.SelectedIndex;
            //----------------------------------------
            // Set the signal level
            //----------------------------------------
            ret = zm.SetLevel(g_id, channel_number, level);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetLevel = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSetSpeed button is clicked
        // Set the measurement speed and the average count
        //================================================================================
        private void button_SetSpeed_Click(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            int    ret;               // For return code
            ushort channel_number;    // Channel No.
            ushort speed;             // Measurement speed
            uint   average_count;     // Average count

            //----------------------------------------
            // Get the channel number to set from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Get the measurement speed to set from combobox
            //----------------------------------------
            speed = (ushort)comboBox_Speed.SelectedIndex;
            //----------------------------------------
            // Get the average count to set from textbox
            //----------------------------------------
            if (uint.TryParse(textBox_AverageCount.Text, out average_count) == false)
            {
                average_count = 1;
            }
            //----------------------------------------
            // Set the measurement speed and the average count
            //----------------------------------------
            ret = zm.SetSpeed(g_id, channel_number, speed, average_count);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetSpeed = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSetRange button is clicked
        // Set the measurement range
        //================================================================================
        private void button_SetRange_Click(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            int    ret;               // For return code
            ushort channel_number;    // Channel No.
            ushort range;             // Measurement range

            //----------------------------------------
            // Get the channel number to set from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Get the measurement range to set from combobox
            //----------------------------------------
            range = (ushort)(comboBox_Range.SelectedIndex + 3);
            //----------------------------------------
            // Set the measurement range
            //----------------------------------------
            ret = zm.SetRange(g_id, channel_number, range);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetRange = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSetCircuit button is clicked
        // Set the equivalent circuit mode
        //================================================================================
        private void button_SetCircuit_Click(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            int    ret;               // For return code
            ushort channel_number;    // Channel No.
            ushort circuit;           // Equivalent circuit mode

            //----------------------------------------
            // Get the channel number to set from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Get the equivalent circuit mode to set from combobox
            //----------------------------------------
            circuit = (ushort)comboBox_Circuit.SelectedIndex;
            //----------------------------------------
            // Set the equivalent circuit mode
            //----------------------------------------
            ret = zm.SetCircuit(g_id, channel_number, circuit);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetCircuit = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSetStartDelay button is clicked
        // Set the measurement start delay
        //================================================================================
        private void button_SetStartDelay_Click(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            int    ret;               // For return code
            ushort channel_number;    // Channel No.
            float  start_delay;       // Measurement start delay

            //----------------------------------------
            // Get the channel number to set from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Get the measurement start delay to set from textbox
            //----------------------------------------
            if (float.TryParse(textBox_StartDelay.Text, out start_delay) == false)
            {
                start_delay = 2;
            }
            //----------------------------------------
            // Set the measurement start delay
            //----------------------------------------
            ret = zm.SetStartDelay(g_id, channel_number, start_delay);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetStartDelay = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSetTriggerSyncEnable button is clicked
        // Set the trigger synchronous output
        //================================================================================
        private void button_SetTriggerSyncEnable_Click(object sender, EventArgs e)
        {
            string error_string;           // Error code string
            int    ret;                    // For return code
            ushort channel_number;         // Channel No.
            ushort trigger_sync_enable;    // Trigger synchronous output

            //----------------------------------------
            // Get the channel number to set from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Get the trigger synchronous output to set from combobox
            //----------------------------------------
            trigger_sync_enable = (ushort)comboBox_TriggerSyncEnable.SelectedIndex;
            //----------------------------------------
            // Set the trigger synchronous output
            //----------------------------------------
            ret = zm.SetTriggerSyncEnable(g_id, channel_number, trigger_sync_enable);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetTriggerSyncEnable = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSetSignalOutputDelay button is clicked
        // Set the measurement signal output delay
        //================================================================================
        private void button_SetSignalOutputDelay_Click(object sender, EventArgs e)
        {
            string error_string;          // Error code string
            int    ret;                   // For return code
            ushort channel_number;        // Channel No.
            float signal_output_delay;    // Measurement signal output delay

            //----------------------------------------
            // Get the channel number to set from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Get the measurement signal output delay to set from textbox
            //----------------------------------------
            if (float.TryParse(textBox_SignalOutputDelay.Text, out signal_output_delay) == false)
            {
                signal_output_delay = 0;
            }
            //----------------------------------------
            // Set the measurement signal output delay
            //----------------------------------------
            ret = zm.SetSignalOutputDelay(g_id, channel_number, signal_output_delay);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetSignalOutputDelay = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSetContactCheck button is clicked
        // Set the contact check
        //================================================================================
        private void button_SetContactCheck_Click(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            int    ret;               // For return code
            ushort channel_number;    // Channel No.
            ushort contact_check;     // Contack check

            //----------------------------------------
            // Get the channel number to set from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Get the contact check to set from combobox
            //----------------------------------------
            contact_check = (ushort)comboBox_ContactCheck.SelectedIndex;
            //----------------------------------------
            // Set the contact check
            //----------------------------------------
            ret = zm.SetContactCheck(g_id, channel_number, contact_check);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetContactCheck = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmSingleCMeasure button is clicked
        // Measure
        //================================================================================
        private void button_SingleCMeasure_Click(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            int    ret;               // For return code
            ushort channel_number;    // Channel No.
            float  capacitance;       // Capacitance
            float  dissipation;       // Dissipation factor
            uint   status;            // Status
            ushort contact_check;     // Contact check

            //----------------------------------------
            // Get the channel number to measure from combobox
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // Measure
            //----------------------------------------
            ret = zm.SingleCMeasure(g_id, channel_number, out capacitance, out dissipation, out status);
            //----------------------------------------
            // Update the display when the ZmSingleCMeasure function ends normally
            //----------------------------------------
            if (ret == (int)CzmConst.ZM_ERR_SUCCESS)
            {
                //----------------------------------------
                // Update the display of capacitance
                //----------------------------------------
                textBox_Capacitance.Text = string.Format("{0:e}", capacitance);
                //----------------------------------------
                // Update the display of dissipation factor
                //----------------------------------------
                textBox_Dissipation.Text = string.Format("{0:f}", dissipation);
                //----------------------------------------
                // Update the display of equivalent circuit mode
                //----------------------------------------
                if (((status >> 24) & 0x03) == (uint)CzmConst.ZM_CIRCUIT_SERIAL)
                {
                    //----------------------------------------
                    // Update the display to serial equivalent circuit
                    //----------------------------------------
                    textBox_Circuit.Text = "SERIAL";
                }
                else if (((status >> 24) & 0x03) == (uint)CzmConst.ZM_CIRCUIT_PARALLEL)
                {
                    //----------------------------------------
                    // Update the display to parallel equivalent circuit
                    //----------------------------------------
                    textBox_Circuit.Text = "PARALLEL";
                }
                //----------------------------------------
                // Update the display of contact check error
                // Update the display to '-' when contact check is disabled
                //----------------------------------------
                zm.GetContactCheck(g_id, channel_number, out contact_check);
                //----------------------------------------
                // The contact check is disabled so update all display to '-'
                //----------------------------------------
                if (contact_check == (ushort)CzmConst.ZM_CONTACTCHECK_DISABLE)
                {
                    textBox_BeforeL.Text = "-";
                    textBox_BeforeH.Text = "-";
                    textBox_AfterL.Text = "-";
                    textBox_AfterH.Text = "-";
                }
                //----------------------------------------
                // The contact check is 'BEFORE' so update 'after measurement' to '-'
                //----------------------------------------
                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 = "-";
                }
                //----------------------------------------
                // The contact check is 'AFTER' so update 'berore measurement' to '-'
                //----------------------------------------
                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);
                }
                //----------------------------------------
                // The contact check is 'BOTH' so update all display to status value
                //----------------------------------------
                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);
                }
            }
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SingleCMeasure = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ZmExit button is clicked
        // Perform the exit process of device
        //================================================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Exit process
            //----------------------------------------
            ret = zm.Exit(g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.Exit = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the Close button is clicked
        // Perform the exit process of device and terminate the application
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Exit process
            //----------------------------------------
            zm.Exit(g_id);
            //----------------------------------------
            // Close dialog
            //----------------------------------------
            this.Close();
        }
    }
}
