using CWaveformGenerator;

namespace MultiChannelWave
{
    public partial class Form1 : Form
    {
        CCWaveformGeneratorClass c_fgen = null!;

        //----------------------------------------
        // }N`
        //----------------------------------------
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;      // CWaveformGeneratoȑ`l

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // C-WaveFormGeneratorSDKŌĂяo߂̏
            //----------------------------------------
            try
            {
                //----------------------------------------
                // C-WaveformGenerator̃NX쐬
                //----------------------------------------
                c_fgen = new CCWaveformGeneratorClass();
            }
            //----------------------------------------
            // G[̎AG[bZ[W\
            //----------------------------------------
            catch
            {
                MessageBox.Show("Unanticipated exception creating the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                this.Close();
                return;
            }

            //----------------------------------------
            // foCX̏\XV
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // ʃTCYR{{bNX̍쐬
            //----------------------------------------
            comboBox_OpenMode.Items.Add("ŏ");
            comboBox_OpenMode.Items.Add("ʏ");
            comboBox_OpenMode.Items.Add("ő剻");
            //----------------------------------------
            // ʃTCYpR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_OpenMode.SelectedIndex = 0;
            //----------------------------------------
            // g`^CvR{{bNX̍쐬
            //----------------------------------------
            comboBox_WaveType.Items.Add("");
            comboBox_WaveType.Items.Add("Œ");
            comboBox_WaveType.Items.Add("Op");
            comboBox_WaveType.Items.Add("̂");
            comboBox_WaveType.Items.Add("`");
            comboBox_WaveType.Items.Add("mCY");
            comboBox_WaveType.Items.Add("C");        //f[^t@C̓ǂݕ̃TvfileTvցB
            //----------------------------------------
            // g`^CvR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_WaveType.SelectedIndex = 0;
            //----------------------------------------
            // `lԍR{{bNX̍쐬
            // C-WaveformGeneratoȑ`l
            //----------------------------------------
            for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++) {
                comboBox_AoChannel.Items.Add(i.ToString());
            }
            //----------------------------------------
            // `lԍR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_AoChannel.SelectedIndex = 0;
        }

        //================================================================================
        // OpenApplication{^NbNۂ̏ɂȂ܂
        // Av̏s܂
        //================================================================================
        private void button_OpenApplicaion_Click(object sender, EventArgs e)
        {
            string  device_name;        // foCX
            string  arg1;               // \
            short   openmode;           // ʃTCY
            long    ret;                // ߂l擾p

            //----------------------------------------
            // eLXg{bNXfoCX擾
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // R{{bNXʃTCY擾
            //----------------------------------------
            openmode = (short)comboBox_OpenMode.SelectedIndex;
            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = "";
            //----------------------------------------
            // Av̏s
            //----------------------------------------
            try
            {
                ret = c_fgen.OpenApplication(openmode, device_name, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("OpenApplication = {0} ", ret);
        }

        //================================================================================
        // DeviceSetting{^NbNۂ̏ɂȂ܂
        // foCXݒ̃_CAOJ܂
        //================================================================================
        private void button_DeviceSetting_Click(object sender, EventArgs e)
        {
            DeviceSetting   deviceinfodlg       = new DeviceSetting();
            short           aochannels          = 1;    // `l
            float           frequency           = 1;    // g
            short           unit_window         = 0;    // gPʃR{{bNX
            short           frequency_type      = 0;    // gor
            short           frequency_unit      = 0;    // Hz(sec) or kHz(msec)
            short           oscilation_type     = 0;    // U^Cv
            short           oscilation_start    = 1;    // Ug
            float           oscilation_stop     = 1;    // U~gorU~
            short           arg1                = 0;    // \
            long            ret;                        // ߂l擾p

            //----------------------------------------
            // ߂l󔒂ɂĂ
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // foCX̎擾s
            //----------------------------------------
            try
            {
                ret = c_fgen.GetDeviceSetting(ref aochannels, ref frequency, ref frequency_type, ref frequency_unit, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            unit_window = (short)((frequency_type << 1) + frequency_unit);
            //----------------------------------------
            // U̎擾s
            //----------------------------------------
            try
            {
                ret = c_fgen.GetOscilation(ref oscilation_type, ref oscilation_start, ref oscilation_stop, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // foCXݒ̃_CAO
            //----------------------------------------
            deviceinfodlg.devinfo_data_set(aochannels, frequency, unit_window, oscilation_type, oscilation_start, oscilation_stop);
            //----------------------------------------
            // OKꂽꍇAs
            //----------------------------------------
            if (deviceinfodlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // foCXݒ̃_CAO
                //----------------------------------------
                deviceinfodlg.devinfo_data_get(ref aochannels, ref frequency, ref unit_window, ref oscilation_type, ref oscilation_start, ref oscilation_stop);
                //----------------------------------------
                // gPʂA^CvƒPʂɕύX
                //----------------------------------------
                frequency_type = (short)((unit_window & 0x02) >> 1);
                frequency_unit = (short)(unit_window & 0x01);
                //----------------------------------------
                // arg1i\j
                //----------------------------------------
                arg1 = 0;
                //----------------------------------------
                // foCX̐ݒs
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetDeviceSetting(aochannels, frequency, frequency_type, frequency_unit, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Ił͂ȂꍇAG[R[h\
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetDeviceSetting = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // U̐ݒs
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetOscilation(oscilation_type, oscilation_start, oscilation_stop, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Ił͂ȂꍇAG[R[h\
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetOscilation = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // ݒ芮̃bZ[W\
                //----------------------------------------
                textBox_ReturnNum.Text = "foCXݒFI";
            }
            deviceinfodlg.Dispose();
        }

        //================================================================================
        // ChannelSetting{^NbNۂ̏ɂȂ܂
        // `lݒ̃_CAOJ܂
        //================================================================================
        private void button_ChannelSetting_Click(object sender, EventArgs e)
        {
            ChannelSetting  channelinfodlg      = new ChannelSetting();
            short           aochannel           = 0;    // `lԍ
            float           amplitude           = 0;    // U
            float           offset              = 0;    // ItZbg
            short           amplitude_type      = 0;    // U^Cv
            short           amplitude_unit      = 0;    // UP
            float           duty                = 0;    // Duty
            float           phase               = 0;    // ʑ
            short           sweep_type          = 0;    // ȈՃXC[v
            short           oscilation_time     = 1;    // XC[v
            float           oscilation_start    = 0;    // XC[vωO
            float           oscilation_stop     = 0;    // XC[vω
            short           arg1                = 0;    // \
            long            ret;                        // ߂l擾p
            short           wave_type           = 0;    // g`^Cv

            //----------------------------------------
            // ߂l󔒂ɂĂ
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // R{{bNX`lԍ擾
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // g`^Cv擾s
            //----------------------------------------
            try
            {
                ret = c_fgen.GetWaveType(aochannel, ref wave_type, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // `l̎擾s
            //----------------------------------------
            try
            {
                ret = c_fgen.GetChannelSetting(aochannel, ref amplitude, ref offset, ref amplitude_type, ref amplitude_unit, ref duty, ref phase, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // ȈՃXC[v̎擾s
            //----------------------------------------
            try
            {
                ret = c_fgen.GetAoSweep(aochannel, ref sweep_type, ref oscilation_time, ref oscilation_start, ref oscilation_stop, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // `lݒ̃_CAO
            //----------------------------------------
            channelinfodlg.chinfo_data_set(aochannel, amplitude, offset, amplitude_type,
                                           amplitude_unit, duty, phase, sweep_type,
                                           oscilation_time, oscilation_start, oscilation_stop, wave_type);
            //----------------------------------------
            // OKꂽꍇAs
            //----------------------------------------
            if (channelinfodlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // `lݒ̃_CAO
                //----------------------------------------
                channelinfodlg.chinfo_data_get(ref aochannel, ref amplitude, ref offset, ref amplitude_type,
                                               ref amplitude_unit, ref duty, ref phase, ref sweep_type,
                                               ref oscilation_time, ref oscilation_start, ref oscilation_stop);
                //----------------------------------------
                // arg1i\j
                //----------------------------------------
                arg1 = 0;
                //----------------------------------------
                // `l̐ݒs
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetChannelSetting(aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Ił͂ȂꍇAG[R[h\
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetChannelSetting = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // ȈՃXC[v̐ݒs
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetAoSweep(aochannel, sweep_type, oscilation_time, oscilation_start, oscilation_stop, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Ił͂ȂꍇAG[R[h\
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetAoSweep = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // ݒ芮̃bZ[W\
                //----------------------------------------
                textBox_ReturnNum.Text = "`lݒFI";
            }
            channelinfodlg.Dispose();
        }

        //================================================================================
        // SetWaveType{^NbNۂ̏ɂȂ܂
        // g`^Cv̐ݒs܂
        //================================================================================
        private void button_SetWaveType_Click(object sender, EventArgs e)
        {
            short   wave_type;          // g`^Cv
            short   aochannel;          // `lԍ
            short   arg1;               // \
            long    ret;                // ߂l擾p

            //----------------------------------------
            // R{{bNXg`^Cv擾
            //----------------------------------------
            wave_type = (short)comboBox_WaveType.SelectedIndex;
            //----------------------------------------
            // R{{bNX`lԍ擾
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // g`^Cv̐ݒs
            //----------------------------------------
            try
            {
                ret = c_fgen.SetWaveType(aochannel, wave_type, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetWaveType = {0} ", ret);
        }

        //================================================================================
        // StartOperation{^NbNۂ̏ɂȂ܂
        // Av̏o͊Jns܂
        //================================================================================
        private void button_StartOperation_Click(object sender, EventArgs e)
        {
            short   arg1;               // \
            long    ret;                // ߂l擾p

            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Av̏o͊Jns
            //----------------------------------------
            try
            {
                ret = c_fgen.StartOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StartOperation = {0} ", ret);
        }

        //================================================================================
        // GetApplicationStatus{^NbNۂ̏ɂȂ܂
        // 󋵂̎擾s܂
        //================================================================================
        private void button_GetApplicationStatus_Click(object sender, EventArgs e)
        {
            int     status = -1;        // Xe[^X
            long    ret;                // ߂l擾p

            //----------------------------------------
            // 󋵂̎擾s
            //----------------------------------------
            try
            {
                ret = c_fgen.GetApplicationStatus(ref status);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GetApplicationStatus = {0} ", ret);
            //----------------------------------------
            // Xe[^X\
            //----------------------------------------
            if (status == 0)
            {
                textBox_Status.Text = "00H(~)";
            }
            else if (status == 1)
            {
                textBox_Status.Text = "01H(쒆)";
            }
            else if (status == 2)
            {
                textBox_Status.Text = "02H(JngK҂)";
            }
            else if (status == 3)
            {
                textBox_Status.Text = "03H(쒆AJngK҂)";
            }
            else
            {
                textBox_Status.Text = status.ToString();
            }
        }

        //================================================================================
        // StopOperation{^NbNۂ̏ɂȂ܂
        // Av̏o͒~s܂
        //================================================================================
        private void button_StopOperation_Click(object sender, EventArgs e)
        {
            short   arg1;               // \
            long    ret;                // ߂l擾p

            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Av̏o͒~s
            //----------------------------------------
            try
            {
                ret = c_fgen.StopOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StopOperation = {0} ", ret);
        }

        //================================================================================
        // CloseApplication{^NbNۂ̏ɂȂ܂
        // Av̏Is܂
        //================================================================================
        private void button_CloseApplicaion_Click(object sender, EventArgs e)
        {
            short   arg1;               // \
            long    ret;                // ߂l擾p

            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Av̏Is
            //----------------------------------------
            try
            {
                ret = c_fgen.CloseApplication(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("CloseApplication = {0} ", ret);
        }

        //================================================================================
        // {^NbNۂ̏ɂȂ܂
        // AvP[V̏Is܂
        //================================================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            short arg1;             // \

            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // foCX̏Is
            //----------------------------------------
            try
            {
                c_fgen.CloseApplication(arg1);
            }
            catch
            {
            }
            //----------------------------------------
            // _CAOI
            //----------------------------------------
            this.Close();
        }
    }
}
