﻿using CdioCs;

namespace DiCounter
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Create instance Cdio class
        /// </summary>
        Cdio dio = new Cdio();

        //==========================================
        // Global variable
        //==========================================
        public              short dev_id;            // Device ID
        public short        count_ch_num;            // Number of counter channels in the device
        public static short MAX_COUNT_CH = 16;       // Max number of channels
        public static short DISP_MAX_COUNT_CH = 8;   // Max number of channels

        private System.Windows.Forms.ComboBox[] m_CountEdge         = new System.Windows.Forms.ComboBox[MAX_COUNT_CH];  // Count edge control
        private System.Windows.Forms.TextBox[]  m_CompareCountValue = new System.Windows.Forms.TextBox[MAX_COUNT_CH];   // Comparison count value control
        private System.Windows.Forms.TextBox[]  m_PresetValue       = new System.Windows.Forms.TextBox[MAX_COUNT_CH];   // Preset value control
        private System.Windows.Forms.TextBox[]  m_CountValue        = new System.Windows.Forms.TextBox[MAX_COUNT_CH];   // Count value control
        private System.Windows.Forms.TextBox[]  m_CountStatus       = new System.Windows.Forms.TextBox[MAX_COUNT_CH];   // Count status control

        public Form1()
        {
            InitializeComponent();

            //-----------------------------------
            //  Control array settings
            //-----------------------------------
            //-----------------------------------
            // Count edge control
            //-----------------------------------
            m_CountEdge[0] = comboBox_CountEdge0;
            m_CountEdge[1] = comboBox_CountEdge1;
            m_CountEdge[2] = comboBox_CountEdge2;
            m_CountEdge[3] = comboBox_CountEdge3;
            m_CountEdge[4] = comboBox_CountEdge4;
            m_CountEdge[5] = comboBox_CountEdge5;
            m_CountEdge[6] = comboBox_CountEdge6;
            m_CountEdge[7] = comboBox_CountEdge7;
            //-----------------------------------
            // Comparison count value control
            //-----------------------------------
            m_CompareCountValue[0] = textBox_CompareCountValue0;
            m_CompareCountValue[1] = textBox_CompareCountValue1;
            m_CompareCountValue[2] = textBox_CompareCountValue2;
            m_CompareCountValue[3] = textBox_CompareCountValue3;
            m_CompareCountValue[4] = textBox_CompareCountValue4;
            m_CompareCountValue[5] = textBox_CompareCountValue5;
            m_CompareCountValue[6] = textBox_CompareCountValue6;
            m_CompareCountValue[7] = textBox_CompareCountValue7;
            //-----------------------------------
            // Preset value control
            //-----------------------------------
            m_PresetValue[0] = textBox_PresetValue0;
            m_PresetValue[1] = textBox_PresetValue1;
            m_PresetValue[2] = textBox_PresetValue2;
            m_PresetValue[3] = textBox_PresetValue3;
            m_PresetValue[4] = textBox_PresetValue4;
            m_PresetValue[5] = textBox_PresetValue5;
            m_PresetValue[6] = textBox_PresetValue6;
            m_PresetValue[7] = textBox_PresetValue7;
            //-----------------------------------
            // Count value control
            //-----------------------------------
            m_CountValue[0] = textBox_CountValue0;
            m_CountValue[1] = textBox_CountValue1;
            m_CountValue[2] = textBox_CountValue2;
            m_CountValue[3] = textBox_CountValue3;
            m_CountValue[4] = textBox_CountValue4;
            m_CountValue[5] = textBox_CountValue5;
            m_CountValue[6] = textBox_CountValue6;
            m_CountValue[7] = textBox_CountValue7;
            //-----------------------------------
            // Count status control
            //-----------------------------------
            m_CountStatus[0] = textBox_CountStatus0;
            m_CountStatus[1] = textBox_CountStatus1;
            m_CountStatus[2] = textBox_CountStatus2;
            m_CountStatus[3] = textBox_CountStatus3;
            m_CountStatus[4] = textBox_CountStatus4;
            m_CountStatus[5] = textBox_CountStatus5;
            m_CountStatus[6] = textBox_CountStatus6;
            m_CountStatus[7] = textBox_CountStatus7;
        }

        //=========================================================
        // Diaglog initialization
        //=========================================================
        private void DiCounter_Load(object sender, EventArgs e)
        {
            //-----------------------------------
            //  Screen display initialization
            //-----------------------------------
            short   ch_no;
            string  cstr;

            //-----------------------------------
            // Device name
            //-----------------------------------
            textBox_DeviceName.Text = "DIO000";
            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < DISP_MAX_COUNT_CH; ch_no++){
                //-----------------------------------
                // Count edge control
                //-----------------------------------
                m_CountEdge[ch_no].Items.Add("Rising");
                m_CountEdge[ch_no].Items.Add("Falling");
                m_CountEdge[ch_no].Items.Add("Both");
                m_CountEdge[ch_no].SelectedIndex = 0;
                //-----------------------------------
                // Comparison count value control
                //-----------------------------------
                cstr = string.Format("{0:d}", ((ch_no + 1) * 100));
                m_CompareCountValue[ch_no].Text = cstr;
                //-----------------------------------
                // Preset value control
                //-----------------------------------
                cstr = string.Format("{0:d}", (ch_no * 100));
                m_PresetValue[ch_no].Text = cstr;
                //-----------------------------------
                // Count value control
                //-----------------------------------
                m_CountValue[ch_no].Text = "-";
                //-----------------------------------
                // Count status control
                //-----------------------------------
                m_CountStatus[ch_no].Text = "-";
            }
        }

        //=========================================================
        // DioInit()
        //=========================================================
        private void button_DioInit_Click(object sender, EventArgs e)
        {
            int     ret;
            string  device_name;
            string  error_string;
            string  return_string;
            short   ch_no;

            //-----------------------------
            // Get device name
            //-----------------------------
            device_name = textBox_DeviceName.Text; 
            //-----------------------------
            // Initialization process, get device ID
            //-----------------------------
            ret = dio.Init(device_name, out dev_id);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Get number of channels
            //-----------------------------
            ret = dio.GetMaxCountChannels(dev_id, out count_ch_num);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
                textBox_ReturnCode.Text = return_string;
                return;
            }
            //-----------------------------------
            // Disable controls for channels not present in device
            //-----------------------------------
            for (ch_no = count_ch_num; ch_no < DISP_MAX_COUNT_CH; ch_no++)
            {
                //-----------------------------------
                // Count edge control
                //-----------------------------------
                m_CountEdge[ch_no].Enabled = false;
                //-----------------------------------
                // Comparison count value control
                //-----------------------------------
                m_CompareCountValue[ch_no].Enabled = false;
                //-----------------------------------
                // Preset value control
                //-----------------------------------
                m_PresetValue[ch_no].Enabled = false;
                //-----------------------------------
                // Count value control
                //-----------------------------------
                m_CountValue[ch_no].Enabled = false;
                //-----------------------------------
                // Count status control
                //-----------------------------------
                m_CountStatus[ch_no].Enabled = false;
            }
            //-----------------------------------
            // Correct the number of count channels to the maximum number of channels that can be displayed
            //-----------------------------------
            if (DISP_MAX_COUNT_CH < count_ch_num)
            {
                count_ch_num = DISP_MAX_COUNT_CH;
            }
        }

        //=========================================================
        // DioSetCountEdge()
        //=========================================================
        private void button_DioSetCountEdge_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array = new short[MAX_COUNT_CH];
            short[] count_edge  = new short[MAX_COUNT_CH];           
            string  cstr;

            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // Save channel number
                //-----------------------------------
                ch_no_array[ch_no] = ch_no;
                //-----------------------------------
                // Get the specified count edge value
                //-----------------------------------
                cstr = m_CountEdge[ch_no].Text;
                if(string.Compare(cstr, "Rising") == 0){
                    count_edge[ch_no]   = (int)CdioConst.DIO_COUNT_EDGE_UP;
                }else if(string.Compare(cstr, "Falling") == 0){
                    count_edge[ch_no]   = (int)CdioConst.DIO_COUNT_EDGE_DOWN;
                }else{
                    count_edge[ch_no]   = (int)CdioConst.DIO_COUNT_EDGE_UP | (int)CdioConst.DIO_COUNT_EDGE_DOWN;
                }
            }
            //-----------------------------
            // Set count edge
            //-----------------------------
            ret = dio.SetCountEdge(dev_id, ch_no_array, count_ch_num, count_edge);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioSetCountMatchValue
        //=========================================================
        private void button_DioSetCountMatchValue_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array             = new short[MAX_COUNT_CH];
            short[] count_match_register_no = new short[MAX_COUNT_CH];
            uint[]  count_match_value       = new uint [MAX_COUNT_CH];
            string  cstr;

            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // Save channel number
                //-----------------------------------
                ch_no_array[ch_no] = ch_no;
                //-----------------------------------
                // Save comparison register number
                //-----------------------------------
                count_match_register_no[ch_no] = 0;
                //-----------------------------------
                // Get the specified comparison count value
                //-----------------------------------
                cstr = m_CompareCountValue[ch_no].Text;
                count_match_value[ch_no]        = System.Convert.ToUInt32(cstr, 10);
            }
            //-----------------------------
            // Set comparison register
            //-----------------------------
            ret = dio.SetCountMatchValue(dev_id, ch_no_array, count_ch_num, count_match_register_no, count_match_value);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioStartCount()
        //=========================================================
        private void button_DioStartCount_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array  = new short[MAX_COUNT_CH];

            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // Save channel number
                //-----------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //-----------------------------
            // Start count
            //-----------------------------
            ret = dio.StartCount(dev_id, ch_no_array, count_ch_num);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioReadCount()
        //=========================================================
        private void button_DioReadCount_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array = new short[MAX_COUNT_CH];
            uint[]  count       = new uint [MAX_COUNT_CH];
            string  cstr;
            
            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // Save channel number
                //-----------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //-----------------------------
            // Read count value
            //-----------------------------
            ret = dio.ReadCount(dev_id, ch_no_array, count_ch_num, count);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------
                // Update the display of count value
                //-----------------------------
                cstr = System.Convert.ToString(count[ch_no], 10);
                m_CountValue[ch_no].Text = cstr;
            }
        }

        //=========================================================
        // DioGetCountStatus()
        //=========================================================
        private void button_DioGetCountStatus_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array  = new short[MAX_COUNT_CH];
            uint[]  count_status = new uint [MAX_COUNT_CH];
            string  cstr;

            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // Save channel number
                //-----------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //-----------------------------
            // Read count status
            //-----------------------------
            ret = dio.GetCountStatus(dev_id, ch_no_array, count_ch_num, count_status);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------
                // Update the display of count status
                //-----------------------------
                cstr = System.Convert.ToString(count_status[ch_no], 16);
                m_CountStatus[ch_no].Text = cstr;
            }
        }

        //=========================================================
        // DioCountPreset()
        //=========================================================
        private void button_DioCountPreset_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array  = new short[MAX_COUNT_CH];
            uint[]  preset_count = new uint [MAX_COUNT_CH];
            string  cstr;

            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // Save channel number
                //-----------------------------------
                ch_no_array[ch_no] = ch_no;
                //-----------------------------------
                // Get the specified preset count value
                //-----------------------------------
                cstr = m_PresetValue[ch_no].Text; 
                preset_count[ch_no]     = System.Convert.ToUInt32(cstr, 10);
            }
            //-----------------------------
            // Preset count value
            //-----------------------------
            ret = dio.CountPreset(dev_id, ch_no_array, count_ch_num, preset_count);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioStopCount()
        //=========================================================
        private void button_DioStopCount_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array = new short[MAX_COUNT_CH];

            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // Save channel number
                //-----------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //-----------------------------
            // Stop count
            //-----------------------------
            ret = dio.StopCount(dev_id, ch_no_array, count_ch_num);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioExit()
        //=========================================================
        private void button_DioExit_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;

            //-----------------------------
            // Exit process
            //-----------------------------
            ret = dio.Exit(dev_id);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // Application end
        //=========================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            //-----------------------------
            // Close the dialog
            //-----------------------------
            this.Close();
        }
    }
}