using CdioCs;

namespace pwm_output
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // NXCdioCX^X쐬
        //----------------------------------------
        Cdio dio = new Cdio();

        //----------------------------------------
        // }N`
        //----------------------------------------
        public const int MAX_PWM_CH = 4;                                  // őPWM`l

        //----------------------------------------
        // O[oϐ
        //----------------------------------------
        public short dev_id;                                              // foCXID
        public ComboBox[] m_PeriodUnit = new ComboBox[MAX_PWM_CH];        // pXPʃRg[
        public ComboBox[] m_PulsePhase = new ComboBox[MAX_PWM_CH];        // pXʑVtgRg[
        public TextBox[] m_PulsePeriodValue = new TextBox[MAX_PWM_CH];    // PWMpXlRg[
        public TextBox[] m_PulseDutyValue = new TextBox[MAX_PWM_CH];      // PWMpXf[eBlRg[
        public TextBox[] m_PwmStatus = new TextBox[MAX_PWM_CH];           // PWMXe[^XRg[
        public TextBox[] m_PwmError = new TextBox[MAX_PWM_CH];            // PWMG[Rg[

        public Form1()
        {
            InitializeComponent();
        }

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

            //----------------------------------------
            // Rg[zݒ
            //----------------------------------------
            //--------------------
            // pXRg[
            //--------------------
            m_PulsePeriodValue[0] = textBox_PulsePeriod0;
            m_PulsePeriodValue[1] = textBox_PulsePeriod1;
            m_PulsePeriodValue[2] = textBox_PulsePeriod2;
            m_PulsePeriodValue[3] = textBox_PulsePeriod3;
            //--------------------
            // pXPʃRg[
            //--------------------
            m_PeriodUnit[0] = comboBox_PeriodUnit0;
            m_PeriodUnit[1] = comboBox_PeriodUnit1;
            m_PeriodUnit[2] = comboBox_PeriodUnit2;
            m_PeriodUnit[3] = comboBox_PeriodUnit3;
            //--------------------
            // pXf[eBRg[
            //--------------------
            m_PulseDutyValue[0] = textBox_PulseDuty0;
            m_PulseDutyValue[1] = textBox_PulseDuty1;
            m_PulseDutyValue[2] = textBox_PulseDuty2;
            m_PulseDutyValue[3] = textBox_PulseDuty3;
            //--------------------
            // pXʑVtgRg[
            //--------------------
            m_PulsePhase[0] = comboBox_PulsePhase0;
            m_PulsePhase[1] = comboBox_PulsePhase1;
            m_PulsePhase[2] = comboBox_PulsePhase2;
            m_PulsePhase[3] = comboBox_PulsePhase3;
            //--------------------
            // PWMXe[^XRg[
            //--------------------
            m_PwmStatus[0] = textBox_PwmStatus0;
            m_PwmStatus[1] = textBox_PwmStatus1;
            m_PwmStatus[2] = textBox_PwmStatus2;
            m_PwmStatus[3] = textBox_PwmStatus3;
            //--------------------
            // PWMG[Rg[
            //--------------------
            m_PwmError[0] = textBox_PwmError0;
            m_PwmError[1] = textBox_PwmError1;
            m_PwmError[2] = textBox_PwmError2;
            m_PwmError[3] = textBox_PwmError3;
            //----------------------------------------
            // ʕ\̏
            //----------------------------------------
            //--------------------
            // foCX
            //--------------------
            textBox_DeviceName.Text = "DIO000";
            //--------------------
            // `l
            //--------------------
            comboBox_ChannelNum.Items.Add("1");
            comboBox_ChannelNum.Items.Add("2");
            comboBox_ChannelNum.Items.Add("3");
            comboBox_ChannelNum.Items.Add("4");
            comboBox_ChannelNum.SelectedItem = "4";
            //--------------------
            // e`lƂ
            //--------------------
            for (ch_no = 0; ch_no < MAX_PWM_CH; ch_no++)
            {
                //--------------------
                // pXRg[
                //--------------------
                m_PulsePeriodValue[ch_no].Text = ((ch_no + 1) * 100).ToString();
                //--------------------
                // pXPʃRg[
                //--------------------
                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";
                //--------------------
                // pXf[eBRg[
                //--------------------
                m_PulseDutyValue[ch_no].Text = "32767";
                //--------------------
                // pXʑVtgRg[
                //--------------------
                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";
                //--------------------
                // PWMXe[^XRg[
                //--------------------
                m_PwmStatus[ch_no].Text = "";
                //--------------------
                // PWMG[Rg[
                //--------------------
                m_PwmError[ch_no].Text = "";
            }
        }

        //=========================================================
        // DioInit{^NbNۂ̏ɂȂ܂
        // foCX̏s܂
        //=========================================================
        private void button_DioInit_Click(object sender, EventArgs e)
        {
            string device_name;
            string error_string;
            int    ret;

            //----------------------------------------
            // foCX擾
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // AfoCXID擾
            //----------------------------------------
            ret = dio.Init(device_name, out dev_id);
            //----------------------------------------
            // G[
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.Init = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioSetPwmPulsePeriod{^NbNۂ̏ɂȂ܂
        // foCXPWMpXݒs܂
        //=========================================================
        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;

            //----------------------------------------
            // `l̎擾
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // e`lƂ
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // `lԍi[
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // PWMpXwl擾
                //----------------------------------------
                if (!ulong.TryParse(m_PulsePeriodValue[ch_no].Text, out pulse_period_value[ch_no]))
                {
                    pulse_period_value[ch_no] = (ulong)(ch_no + 1) * 100;
                }
                //----------------------------------------
                // pXPʎwl擾
                //----------------------------------------
                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;
                }
            }
            //----------------------------------------
            // PWMpXݒ菈
            //----------------------------------------
            ret = dio.SetPwmPulsePeriod(dev_id, ch_no_array, pwm_ch_num, pulse_period_value, pulse_period_unit);
            //----------------------------------------
            // G[
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPwmPulsePeriod = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioSetPwmPulsePhase{^NbNۂ̏ɂȂ܂
        // foCXPWMpXʑVtgݒs܂
        //=========================================================
        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;

            //----------------------------------------
            // `l̎擾
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // e`lƂ
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // `lԍi[
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // PWMpXʑVtgwl擾
                //----------------------------------------
                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;
                }
            }
            //----------------------------------------
            // PWMpXʑVtgݒ菈
            //----------------------------------------
            ret = dio.SetPwmPulsePhase(dev_id, ch_no_array, pwm_ch_num, pulse_phase);
            //----------------------------------------
            // G[
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPwmPulsePhase = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioSetPwmPulseDuty{^NbNۂ̏ɂȂ܂
        // foCXPWMpXf[eBݒs܂
        //=========================================================
        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;

            //----------------------------------------
            // `l̎擾
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // e`lƂ
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // `lԍi[
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // PWMpXf[eBl擾
                //----------------------------------------
                if (!ushort.TryParse(m_PulseDutyValue[ch_no].Text, out pulse_duty[ch_no]))
                {
                    pulse_duty[ch_no] = 32767;
                }
            }
            //----------------------------------------
            // PWMpXf[eBݒ菈
            //----------------------------------------
            ret = dio.SetPwmPulseDuty(dev_id, ch_no_array, pwm_ch_num, pulse_duty);
            //----------------------------------------
            // G[
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPwmPulseDuty = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioStartPwm{^NbNۂ̏ɂȂ܂
        // PWMpXo͂Jn܂
        //=========================================================
        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;

            //----------------------------------------
            // `l̎擾
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // e`lƂ
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // `lԍi[
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // PWMpXo͊Jn
            //----------------------------------------
            ret = dio.StartPwm(dev_id, ch_no_array, pwm_ch_num);
            //----------------------------------------
            // G[
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.StartPwm = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioGetPwmStatus{^NbNۂ̏ɂȂ܂
        // PWMXe[^X擾܂
        //=========================================================
        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;

            //----------------------------------------
            // `l̎擾
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // e`lƂ
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // `lԍi[
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // PWMXe[^X擾
            //----------------------------------------
            ret = dio.GetPwmStatus(dev_id, ch_no_array, pwm_ch_num, pwm_status, pwm_error);
            //----------------------------------------
            // G[
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.GetPwmStatus = {0} : {1}", ret, error_string);
            //----------------------------------------
            // G[̏ꍇI
            //----------------------------------------
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //----------------------------------------
            // e`lƂ
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // PWMXe[^X\XV
                //----------------------------------------
                m_PwmStatus[ch_no].Text = pwm_status[ch_no].ToString("X");
                //----------------------------------------
                // PWMG[\XV
                //----------------------------------------
                m_PwmError[ch_no].Text = pwm_error[ch_no].ToString("X");
            }
        }

        //=========================================================
        // DioStopPwm{^NbNۂ̏ɂȂ܂
        // PWMpXo͂~܂
        //=========================================================
        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;

            //----------------------------------------
            // `l̎擾
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.Text, out pwm_ch_num);
            //----------------------------------------
            // e`lƂ
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // `lԍi[
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // PWMpXo͒~
            //----------------------------------------
            ret = dio.StopPwm(dev_id, ch_no_array, pwm_ch_num);
            //----------------------------------------
            // G[
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.StopPwm = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioExit{^NbNۂ̏ɂȂ܂
        // foCX̏Is܂
        //=========================================================
        private void button_DioExit_Click(object sender, EventArgs e)
        {
            string error_string;
            int    ret;

            //----------------------------------------
            // I
            //----------------------------------------
            ret = dio.Exit(dev_id);
            //----------------------------------------
            // G[
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.Exit = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // NbNۂ̏ɂȂ܂
        // foCX̏IsAAvP[VI܂
        //=========================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // I
            //----------------------------------------
            dio.Exit(dev_id);
            //----------------------------------------
            // _CAOI
            //----------------------------------------
            Close();
        }
    }
}
