using CdioCs;

namespace pwm_output
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // Create instance Cdio class
        //----------------------------------------
        Cdio dio = new Cdio();

        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int MAX_PWM_CH = 4;                                  // Maximum number of PWM channels

        //----------------------------------------
        // Global variable
        //----------------------------------------
        public short dev_id;                                              // Device ID
        public ComboBox[] m_PeriodUnit = new ComboBox[MAX_PWM_CH];        // Pulse period unit control
        public ComboBox[] m_PulsePhase = new ComboBox[MAX_PWM_CH];        // Pulse phase shift control
        public TextBox[] m_PulsePeriodValue = new TextBox[MAX_PWM_CH];    // PWM pulse period value control
        public TextBox[] m_PulseDutyValue = new TextBox[MAX_PWM_CH];      // PWM pulse duty ratio control
        public TextBox[] m_PwmStatus = new TextBox[MAX_PWM_CH];           // PWM status control
        public TextBox[] m_PwmError = new TextBox[MAX_PWM_CH];            // PWM error control

        public Form1()
        {
            InitializeComponent();
        }

        //=========================================================
        // Dialog initialization
        //=========================================================
        private void pwm_output_Load(object sender, EventArgs e)
        {
            short ch_no;

            //----------------------------------------
            // Set control array
            //----------------------------------------
            //--------------------
            // Pulse period control
            //--------------------
            m_PulsePeriodValue[0] = textBox_PulsePeriod0;
            m_PulsePeriodValue[1] = textBox_PulsePeriod1;
            m_PulsePeriodValue[2] = textBox_PulsePeriod2;
            m_PulsePeriodValue[3] = textBox_PulsePeriod3;
            //--------------------
            // Pulse period unit control
            //--------------------
            m_PeriodUnit[0] = comboBox_PeriodUnit0;
            m_PeriodUnit[1] = comboBox_PeriodUnit1;
            m_PeriodUnit[2] = comboBox_PeriodUnit2;
            m_PeriodUnit[3] = comboBox_PeriodUnit3;
            //--------------------
            // Pulse duty ratio control
            //--------------------
            m_PulseDutyValue[0] = textBox_PulseDuty0;
            m_PulseDutyValue[1] = textBox_PulseDuty1;
            m_PulseDutyValue[2] = textBox_PulseDuty2;
            m_PulseDutyValue[3] = textBox_PulseDuty3;
            //--------------------
            // Pulse phase shift control
            //--------------------
            m_PulsePhase[0] = comboBox_PulsePhase0;
            m_PulsePhase[1] = comboBox_PulsePhase1;
            m_PulsePhase[2] = comboBox_PulsePhase2;
            m_PulsePhase[3] = comboBox_PulsePhase3;
            //--------------------
            // PWM status control
            //--------------------
            m_PwmStatus[0] = textBox_PwmStatus0;
            m_PwmStatus[1] = textBox_PwmStatus1;
            m_PwmStatus[2] = textBox_PwmStatus2;
            m_PwmStatus[3] = textBox_PwmStatus3;
            //--------------------
            // PWM error control
            //--------------------
            m_PwmError[0] = textBox_PwmError0;
            m_PwmError[1] = textBox_PwmError1;
            m_PwmError[2] = textBox_PwmError2;
            m_PwmError[3] = textBox_PwmError3;
            //----------------------------------------
            // Initialize the display
            //----------------------------------------
            //--------------------
            // Device name
            //--------------------
            textBox_DeviceName.Text = "DIO000";
            //--------------------
            // Number of channels
            //--------------------
            comboBox_ChannelNum.Items.Add("1");
            comboBox_ChannelNum.Items.Add("2");
            comboBox_ChannelNum.Items.Add("3");
            comboBox_ChannelNum.Items.Add("4");
            comboBox_ChannelNum.SelectedItem = "4";
            //--------------------
            // For each channel
            //--------------------
            for (ch_no = 0; ch_no < MAX_PWM_CH; ch_no++)
            {
                //--------------------
                // Pulse period control
                //--------------------
                m_PulsePeriodValue[ch_no].Text = ((ch_no + 1) * 100).ToString();
                //--------------------
                // Pulse period unit control
                //--------------------
                m_PeriodUnit[ch_no].Items.Add("sec");
                m_PeriodUnit[ch_no].Items.Add("msec");
                m_PeriodUnit[ch_no].Items.Add("usec");
                m_PeriodUnit[ch_no].Items.Add("nsec");
                m_PeriodUnit[ch_no].SelectedItem = "msec";
                //--------------------
                // Pulse duty ratio control
                //--------------------
                m_PulseDutyValue[ch_no].Text = "32767";
                //--------------------
                // Pulse phase shift control
                //--------------------
                m_PulsePhase[ch_no].Items.Add("0");
                m_PulsePhase[ch_no].Items.Add("90");
                m_PulsePhase[ch_no].Items.Add("180");
                m_PulsePhase[ch_no].SelectedItem = "0";
                //--------------------
                // PWM status control
                //--------------------
                m_PwmStatus[ch_no].Text = "";
                //--------------------
                // PWM error control
                //--------------------
                m_PwmError[ch_no].Text = "";
            }
        }

        //=========================================================
        // It is the process when the DioInit button is clicked
        // Initialize the device
        //=========================================================
        private void button_DioInit_Click(object sender, EventArgs e)
        {
            string device_name;
            string error_string;
            int    ret;

            //----------------------------------------
            // Get device name
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // Initialization and get device ID
            //----------------------------------------
            ret = dio.Init(device_name, out dev_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.Init = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // It is the process when the DioSetPwmPulsePeriod button is clicked
        // Set the PWM pulse period of the device
        //=========================================================
        private void button_DioSetPwmPulsePeriod_Click(object sender, EventArgs e)
        {
            string   cstr;
            string   error_string;
            ulong[]  pulse_period_value = new ulong[MAX_PWM_CH];
            ushort[] pulse_period_unit = new ushort[MAX_PWM_CH];
            short[]  ch_no_array = new short[MAX_PWM_CH];
            int      ret;
            short    ch_no;
            short    pwm_ch_num;

            //----------------------------------------
            // Get number of channels
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // For each channel
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // Store channel number
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // Get specified value of PWM pulse period
                //----------------------------------------
                if (!ulong.TryParse(m_PulsePeriodValue[ch_no].Text, out pulse_period_value[ch_no]))
                {
                    pulse_period_value[ch_no] = (ulong)(ch_no + 1) * 100;
                }
                //----------------------------------------
                // Get specified value of pulse period unit
                //----------------------------------------
                cstr = m_PeriodUnit[ch_no].Text;
                if (string.Equals(cstr, "sec"))
                {
                    pulse_period_unit[ch_no] = (ushort)CdioConst.DIO_PWM_PULSE_UNIT_S;
                }
                else if (string.Equals(cstr, "msec"))
                {
                    pulse_period_unit[ch_no] = (ushort)CdioConst.DIO_PWM_PULSE_UNIT_MS;
                }
                else if (string.Equals(cstr, "usec"))
                {
                    pulse_period_unit[ch_no] = (ushort)CdioConst.DIO_PWM_PULSE_UNIT_US;
                }
                else
                {
                    pulse_period_unit[ch_no] = (ushort)CdioConst.DIO_PWM_PULSE_UNIT_NS;
                }
            }
            //----------------------------------------
            // PWM pulse period setting
            //----------------------------------------
            ret = dio.SetPwmPulsePeriod(dev_id, ch_no_array, pwm_ch_num, pulse_period_value, pulse_period_unit);
            //----------------------------------------
            // Error process
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPwmPulsePeriod = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // It is the process when the DioSetPwmPulsePhase button is clicked
        // Set the PWM pulse phase shift of the device
        //=========================================================
        private void button_DioSetPwmPulsePhase_Click(object sender, EventArgs e)
        {
            string  cstr;
            string  error_string;
            short[] pulse_phase = new short[MAX_PWM_CH];
            short[] ch_no_array = new short[MAX_PWM_CH];
            int     ret;
            short   ch_no;
            short   pwm_ch_num;

            //----------------------------------------
            // Get number of channels
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // For each channel
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // Store channel number
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // Get specified value of PWM pulse phase shift
                //----------------------------------------
                cstr = m_PulsePhase[ch_no].Text;
                if (string.Equals(cstr, "0"))
                {
                    pulse_phase[ch_no] = (short)CdioConst.DIO_PWM_PULSE_PHASE_0_DEGREES;
                }
                else if (string.Equals(cstr, "90"))
                {
                    pulse_phase[ch_no] = (short)CdioConst.DIO_PWM_PULSE_PHASE_90_DEGREES;
                }
                else
                {
                    pulse_phase[ch_no] = (short)CdioConst.DIO_PWM_PULSE_PHASE_180_DEGREES;
                }
            }
            //----------------------------------------
            // PWM pulse phase shift setting
            //----------------------------------------
            ret = dio.SetPwmPulsePhase(dev_id, ch_no_array, pwm_ch_num, pulse_phase);
            //----------------------------------------
            // Error process
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPwmPulsePhase = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // It is the process when the DioSetPwmPulseDuty button is clicked
        // Set the PWM pulse duty ratio of the device
        //=========================================================
        private void button_DioSetPwmPulseDuty_Click(object sender, EventArgs e)
        {
            string   error_string;
            ushort[] pulse_duty = new ushort[MAX_PWM_CH];
            short[]  ch_no_array = new short[MAX_PWM_CH];
            int      ret;
            short    ch_no;
            short    pwm_ch_num;

            //----------------------------------------
            // Get number of channels
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // For each channel
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // Store channel number
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // Get specified value of PWM pulse duty ratio
                //----------------------------------------
                if (!ushort.TryParse(m_PulseDutyValue[ch_no].Text, out pulse_duty[ch_no]))
                {
                    pulse_duty[ch_no] = 32767;
                }
            }
            //----------------------------------------
            // PWM pulse duty ratio setting
            //----------------------------------------
            ret = dio.SetPwmPulseDuty(dev_id, ch_no_array, pwm_ch_num, pulse_duty);
            //----------------------------------------
            // Error process
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPwmPulseDuty = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // It is the process when the DioStartPwm button is clicked
        // Start the PWM pulse output
        //=========================================================
        private void button_DioStartPwm_Click(object sender, EventArgs e)
        {
            string  error_string;
            short[] ch_no_array = new short[MAX_PWM_CH];
            int     ret;
            short   ch_no;
            short   pwm_ch_num;

            //----------------------------------------
            // Get number of channels
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // For each channel
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // Store channel number
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // Start PWM pulse output
            //----------------------------------------
            ret = dio.StartPwm(dev_id, ch_no_array, pwm_ch_num);
            //----------------------------------------
            // Error process
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.StartPwm = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // It is the process when the DioGetPwmStatus button is clicked
        // Get the PWM status
        //=========================================================
        private void button_DioGetPwmStatus_Click(object sender, EventArgs e)
        {
            string   error_string;
            ushort[] pwm_status = new ushort[MAX_PWM_CH];
            ushort[] pwm_error = new ushort[MAX_PWM_CH];
            short[]  ch_no_array = new short[MAX_PWM_CH];
            int      ret;
            short    ch_no;
            short    pwm_ch_num;

            //----------------------------------------
            // Get number of channels
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // For each channel
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // Store channel number
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // Get PWM status
            //----------------------------------------
            ret = dio.GetPwmStatus(dev_id, ch_no_array, pwm_ch_num, pwm_status, pwm_error);
            //----------------------------------------
            // Error process
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.GetPwmStatus = {0} : {1}", ret, error_string);
            //----------------------------------------
            // Terminate process in case of error
            //----------------------------------------
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //----------------------------------------
            // For each channel
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // Update the display of PWM status
                //----------------------------------------
                m_PwmStatus[ch_no].Text = pwm_status[ch_no].ToString("X");
                //----------------------------------------
                // Update the display of PWM error
                //----------------------------------------
                m_PwmError[ch_no].Text = pwm_error[ch_no].ToString("X");
            }
        }

        //=========================================================
        // It is the process when the DioStopPwm button is clicked
        // Stop the PWM pulse output
        //=========================================================
        private void button_DioStopPwm_Click(object sender, EventArgs e)
        {
            string  error_string;
            short[] ch_no_array = new short[MAX_PWM_CH];
            int     ret;
            short   ch_no;
            short   pwm_ch_num;

            //----------------------------------------
            // Get number of channels
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // For each channel
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // Store channel number
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // Stop PWM pulse output
            //----------------------------------------
            ret = dio.StopPwm(dev_id, ch_no_array, pwm_ch_num);
            //----------------------------------------
            // Error process
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.StopPwm = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // It is the process when the DioExit button is clicked
        // Perform the exit process of device
        //=========================================================
        private void button_DioExit_Click(object sender, EventArgs e)
        {
            string error_string;
            int    ret;

            //----------------------------------------
            // Exit process
            //----------------------------------------
            ret = dio.Exit(dev_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.Exit = {0} : {1}", ret, 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_Exit_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Exit process
            //----------------------------------------
            dio.Exit(dev_id);
            //----------------------------------------
            // Close dialog
            //----------------------------------------
            Close();
        }
    }
}
