﻿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 CdioCs;

namespace pwm_output
{
    public partial class pwm_output : Form
    {
        //----------------------------------------
        // クラスCdioインスタンス作成
        //----------------------------------------
        Cdio dio = new Cdio();

        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int MAX_PWM_CH = 4;                                  // 最大PWMチャネル数

        //----------------------------------------
        // グローバル変数
        //----------------------------------------
        public short dev_id;                                              // デバイスID
        public ComboBox[] m_PeriodUnit = new ComboBox[MAX_PWM_CH];        // パルス周期単位コントロール
        public ComboBox[] m_PulsePhase = new ComboBox[MAX_PWM_CH];        // パルス位相シフトコントロール
        public TextBox[] m_PulsePeriodValue = new TextBox[MAX_PWM_CH];    // PWMパルス周期値コントロール
        public TextBox[] m_PulseDutyValue = new TextBox[MAX_PWM_CH];      // PWMパルスデューティ比値コントロール
        public TextBox[] m_PwmStatus = new TextBox[MAX_PWM_CH];           // PWMステータスコントロール
        public TextBox[] m_PwmError = new TextBox[MAX_PWM_CH];            // PWMエラーコントロール

        public pwm_output()
        {
            InitializeComponent();
        }

        //=========================================================
        // ダイアログ初期化
        //=========================================================
        private void pwm_output_Load(object sender, EventArgs e)
        {
            short ch_no;

            //----------------------------------------
            // コントロール配列設定
            //----------------------------------------
            //--------------------
            // パルス周期コントロール
            //--------------------
            m_PulsePeriodValue[0] = textBox_PulsePeriod0;
            m_PulsePeriodValue[1] = textBox_PulsePeriod1;
            m_PulsePeriodValue[2] = textBox_PulsePeriod2;
            m_PulsePeriodValue[3] = textBox_PulsePeriod3;
            //--------------------
            // パルス周期単位コントロール
            //--------------------
            m_PeriodUnit[0] = comboBox_PeriodUnit0;
            m_PeriodUnit[1] = comboBox_PeriodUnit1;
            m_PeriodUnit[2] = comboBox_PeriodUnit2;
            m_PeriodUnit[3] = comboBox_PeriodUnit3;
            //--------------------
            // パルスデューティ比コントロール
            //--------------------
            m_PulseDutyValue[0] = textBox_PulseDuty0;
            m_PulseDutyValue[1] = textBox_PulseDuty1;
            m_PulseDutyValue[2] = textBox_PulseDuty2;
            m_PulseDutyValue[3] = textBox_PulseDuty3;
            //--------------------
            // パルス位相シフトコントロール
            //--------------------
            m_PulsePhase[0] = comboBox_PulsePhase0;
            m_PulsePhase[1] = comboBox_PulsePhase1;
            m_PulsePhase[2] = comboBox_PulsePhase2;
            m_PulsePhase[3] = comboBox_PulsePhase3;
            //--------------------
            // PWMステータスコントロール
            //--------------------
            m_PwmStatus[0] = textBox_PwmStatus0;
            m_PwmStatus[1] = textBox_PwmStatus1;
            m_PwmStatus[2] = textBox_PwmStatus2;
            m_PwmStatus[3] = textBox_PwmStatus3;
            //--------------------
            // PWMエラーコントロール
            //--------------------
            m_PwmError[0] = textBox_PwmError0;
            m_PwmError[1] = textBox_PwmError1;
            m_PwmError[2] = textBox_PwmError2;
            m_PwmError[3] = textBox_PwmError3;
            //----------------------------------------
            // 画面表示の初期化
            //----------------------------------------
            //--------------------
            // デバイス名
            //--------------------
            textBox_DeviceName.Text = "DIO000";
            //--------------------
            // チャネル数
            //--------------------
            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 (ch_no = 0; ch_no < MAX_PWM_CH; ch_no++)
            {
                //--------------------
                // パルス周期コントロール
                //--------------------
                m_PulsePeriodValue[ch_no].Text = ((ch_no + 1) * 100).ToString();
                //--------------------
                // パルス周期単位コントロール
                //--------------------
                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";
                //--------------------
                // パルスデューティ比コントロール
                //--------------------
                m_PulseDutyValue[ch_no].Text = "32767";
                //--------------------
                // パルス位相シフトコントロール
                //--------------------
                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ステータスコントロール
                //--------------------
                m_PwmStatus[ch_no].Text = "";
                //--------------------
                // PWMエラーコントロール
                //--------------------
                m_PwmError[ch_no].Text = "";
            }
        }

        //=========================================================
        // DioInitボタンをクリックした際の処理になります
        // デバイスの初期化処理を行います
        //=========================================================
        private void button_DioInit_Click(object sender, EventArgs e)
        {
            string device_name;
            string error_string;
            int    ret;

            //----------------------------------------
            // デバイス名取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // 初期化処理、デバイスID取得
            //----------------------------------------
            ret = dio.Init(device_name, out dev_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.Init = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioSetPwmPulsePeriodボタンをクリックした際の処理になります
        // デバイスのPWMパルス周期設定を行います
        //=========================================================
        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;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pwm_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // PWMパルス周期指定値取得
                //----------------------------------------
                if (!ulong.TryParse(m_PulsePeriodValue[ch_no].Text, out pulse_period_value[ch_no]))
                {
                    pulse_period_value[ch_no] = (ulong)(ch_no + 1) * 100;
                }
                //----------------------------------------
                // パルス周期単位指定値取得
                //----------------------------------------
                cstr = m_PeriodUnit[ch_no].SelectedItem.ToString();
                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パルス周期設定処理
            //----------------------------------------
            ret = dio.SetPwmPulsePeriod(dev_id, ch_no_array, pwm_ch_num, pulse_period_value, pulse_period_unit);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPwmPulsePeriod = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioSetPwmPulsePhaseボタンをクリックした際の処理になります
        // デバイスのPWMパルス位相シフト設定を行います
        //=========================================================
        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;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pwm_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // PWMパルス位相シフト指定値取得
                //----------------------------------------
                cstr = m_PulsePhase[ch_no].SelectedItem.ToString();
                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パルス位相シフト設定処理
            //----------------------------------------
            ret = dio.SetPwmPulsePhase(dev_id, ch_no_array, pwm_ch_num, pulse_phase);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPwmPulsePhase = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioSetPwmPulseDutyボタンをクリックした際の処理になります
        // デバイスのPWMパルスデューティ比設定を行います
        //=========================================================
        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;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pwm_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // PWMパルスデューティ比値取得
                //----------------------------------------
                if (!ushort.TryParse(m_PulseDutyValue[ch_no].Text, out pulse_duty[ch_no]))
                {
                    pulse_duty[ch_no] = 32767;
                }
            }
            //----------------------------------------
            // PWMパルスデューティ比設定処理
            //----------------------------------------
            ret = dio.SetPwmPulseDuty(dev_id, ch_no_array, pwm_ch_num, pulse_duty);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPwmPulseDuty = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioStartPwmボタンをクリックした際の処理になります
        // PWMパルス出力を開始します
        //=========================================================
        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;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pwm_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // PWMパルス出力開始処理
            //----------------------------------------
            ret = dio.StartPwm(dev_id, ch_no_array, pwm_ch_num);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.StartPwm = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioGetPwmStatusボタンをクリックした際の処理になります
        // PWMステータスを取得します
        //=========================================================
        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;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pwm_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // PWMステータス取得処理
            //----------------------------------------
            ret = dio.GetPwmStatus(dev_id, ch_no_array, pwm_ch_num, pwm_status, pwm_error);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.GetPwmStatus = {0} : {1}", ret, error_string);
            //----------------------------------------
            // エラーの場合処理を終了
            //----------------------------------------
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // PWMステータス表示更新
                //----------------------------------------
                m_PwmStatus[ch_no].Text = pwm_status[ch_no].ToString("X");
                //----------------------------------------
                // PWMエラー表示更新
                //----------------------------------------
                m_PwmError[ch_no].Text = pwm_error[ch_no].ToString("X");
            }
        }

        //=========================================================
        // DioStopPwmボタンをクリックした際の処理になります
        // PWMパルス出力を停止します
        //=========================================================
        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;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pwm_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pwm_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // PWMパルス出力停止処理
            //----------------------------------------
            ret = dio.StopPwm(dev_id, ch_no_array, pwm_ch_num);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.StopPwm = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioExitボタンをクリックした際の処理になります
        // デバイスの終了処理を行います
        //=========================================================
        private void button_DioExit_Click(object sender, EventArgs e)
        {
            string error_string;
            int    ret;

            //----------------------------------------
            // 終了処理
            //----------------------------------------
            ret = dio.Exit(dev_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.Exit = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // 閉じるをクリックした際の処理になります
        // デバイスの終了処理を行い、アプリケーションを終了します
        //=========================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // 終了処理
            //----------------------------------------
            dio.Exit(dev_id);
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            Close();
        }
    }
}
