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

namespace MultiChannelWave
{
    public partial class ChannelSetting : Form
    {
        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int CWAVEFORMGENERATOR_SWEEPTIME = 10;
        public const int WAVETYPE_SINE                = 0;
        public const int WAVETYPE_SQUARE              = 4;
        //----------------------------------------
        // Variable definition
        //----------------------------------------
        ushort m_aochannel;
        float  m_amplitude;
        float  m_offset;
        ushort m_amplitude_type;
        ushort m_amplitude_unit;
        float  m_duty;
        float  m_phase;
        ushort m_sweep_type;
        ushort m_sweep_time;
        float  m_sweep_start;
        float  m_sweep_stop;
        ushort m_wave_type;

        public ChannelSetting()
        {
            InitializeComponent();
        }

        //================================================================================
        // Data transfer (Set)
        //================================================================================
        internal void chinfo_data_set(ushort aochannel, float amplitude, float offset, ushort amplitudetype,
                                       ushort amplitudeunit, float duty, float phase, ushort sweeptype,
                                       ushort sweeptime, float sweepstart, float sweepstop, ushort wavetype)
        {
            m_aochannel      = aochannel;
            m_amplitude      = amplitude;
            m_offset         = offset;
            m_amplitude_type = amplitudetype;
            m_amplitude_unit = amplitudeunit;
            m_duty           = duty;
            m_phase          = phase;
            m_sweep_type     = sweeptype;
            m_sweep_time     = sweeptime;
            m_sweep_start    = sweepstart;
            m_sweep_stop     = sweepstop;
            m_wave_type      = wavetype;
        }

        //================================================================================
        // Data transfer (Get)
        //================================================================================
        internal void chinfo_data_get(ref ushort aochannel, ref float amplitude, ref float offset, ref ushort amplitudetype,
                                       ref ushort amplitudeunit, ref float duty, ref float phase, ref ushort sweeptype,
                                       ref ushort sweeptime, ref float sweepstart, ref float sweepstop)
        {
            aochannel     = m_aochannel;
            amplitude     = m_amplitude;
            offset        = m_offset;
            amplitudetype = m_amplitude_type;
            amplitudeunit = m_amplitude_unit;
            duty          = m_duty;
            phase         = m_phase;
            sweeptype     = m_sweep_type;
            sweeptime     = m_sweep_time;
            sweepstart    = m_sweep_start;
            sweepstop     = m_sweep_stop;
        }

        //================================================================================
        // Initial display
        // Value is current setting value
        //================================================================================
        private void ChannelSetting_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Update the display of channel number
            //----------------------------------------
            label_ChannelNo.Text = m_aochannel.ToString();
            //----------------------------------------
            // Update the initial display of amplitude
            //----------------------------------------
            textBox_Amplitude.Text = string.Format("{0:F3}", m_amplitude);
            //----------------------------------------
            // Update the initial display of offset
            //----------------------------------------
            textBox_Offset.Text = string.Format("{0:F3}", m_offset);
            //----------------------------------------
            // Update the initial display of duty
            //----------------------------------------
            textBox_Duty.Text = string.Format("{0:F2}", m_duty);
            //----------------------------------------
            // Update the initial display of phase
            //----------------------------------------
            textBox_Phase.Text = string.Format("{0:F2}", m_phase);
            //----------------------------------------
            // Update the initial display of sweep before change
            //----------------------------------------
            textBox_SweepStart.Text = string.Format("{0:F2}", m_sweep_start);
            //----------------------------------------
            // Update the initial display of sweep after change
            //----------------------------------------
            textBox_SweepStop.Text = string.Format("{0:F2}", m_sweep_stop);
            //----------------------------------------
            // Create the combobox for amplitude type
            //----------------------------------------
            comboBox_AmplitudeType.Items.Add("Amplitude");
            comboBox_AmplitudeType.Items.Add("Vp-p");
            //----------------------------------------
            // Change the selection of amplitude type combobox
            //----------------------------------------
            comboBox_AmplitudeType.SelectedIndex = m_amplitude_type;
            //----------------------------------------
            // Create the combobox for amplitude unit
            //----------------------------------------
            comboBox_AmplitudeUnit.Items.Add("V or mA");
            comboBox_AmplitudeUnit.Items.Add("LSB");
            //----------------------------------------
            // Change the selection of amplitude unit combobox
            //----------------------------------------
            comboBox_AmplitudeUnit.SelectedIndex = m_amplitude_unit;
            //----------------------------------------
            // Create the combobox for simple sweep
            //----------------------------------------
            comboBox_SweepType.Items.Add("None");
            //----------------------------------------
            // For Sinewave, create options for [Frequency sweep], [Amplitude sweep], and [DC offset sweep]
            //----------------------------------------
            if (m_wave_type == WAVETYPE_SINE)
            {
                comboBox_SweepType.Items.Add("Frequency sweep");
                comboBox_SweepType.Items.Add("Amplitude sweep");
                comboBox_SweepType.Items.Add("DC offset sweep");
            }
            //----------------------------------------
            // For Square, create options for [Duty ratio sweep]
            //----------------------------------------
            else if (m_wave_type == WAVETYPE_SQUARE)
            {
                comboBox_SweepType.Items.Add("Duty ratio sweep");
            }
            //----------------------------------------
            // Change the selection of simple sweep combobox
            //----------------------------------------
            comboBox_SweepType.SelectedIndex = m_sweep_type;
            //----------------------------------------
            // Create the combobox for sweep time
            //----------------------------------------
            for (int i = 0; i < CWAVEFORMGENERATOR_SWEEPTIME; i++)
            {
                comboBox_SweepTime.Items.Add((i + 1).ToString());
            }
            //----------------------------------------
            // Change the selection of sweep time combobox
            //----------------------------------------
            comboBox_SweepTime.SelectedIndex = (m_sweep_time - 1);
        }

        //================================================================================
        // It is the process when the OK button is clicked
        //================================================================================
        private void button_Ok_Click(object sender, EventArgs e)
        {
            float  pre_amplitude;         // Amplitude
            float  pre_offset;            // Offset
            ushort pre_amplitude_unit;    // Amplitude unit
            ushort pre_amplitude_type;    // Amplitude type
            float  pre_duty;              // Duty ratio
            float  pre_phase;             // Phase
            ushort pre_sweep_type;        // Simple sweep
            ushort pre_sweep_time;        // Sweep time
            float  pre_sweep_start;       // Sweep before change
            float  pre_sweep_stop;        // Sweep after change

            //----------------------------------------
            // Get the amplitude from textbox
            //----------------------------------------
            if (float.TryParse(textBox_Amplitude.Text, out pre_amplitude) == false)
            {
                pre_amplitude = 0;
            }
            //----------------------------------------
            // Get the offset from textbox
            //----------------------------------------
            if (float.TryParse(textBox_Offset.Text, out pre_offset) == false)
            {
                pre_offset = 0;
            }
            //----------------------------------------
            // Get the duty ratio from textbox
            //----------------------------------------
            if (float.TryParse(textBox_Duty.Text, out pre_duty) == false)
            {
                pre_duty = 0;
            }
            //----------------------------------------
            // Get the phase from textbox
            //----------------------------------------
            if (float.TryParse(textBox_Phase.Text, out pre_phase) == false)
            {
                pre_phase = 0;
            }
            //----------------------------------------
            // Get the amplitude unit from combobox
            //----------------------------------------
            pre_amplitude_unit = (ushort)comboBox_AmplitudeUnit.SelectedIndex;
            //----------------------------------------
            // Get the amplitude type from combobox
            //----------------------------------------
            pre_amplitude_type = (ushort)comboBox_AmplitudeType.SelectedIndex;
            //----------------------------------------
            // Get the sweep before change from textbox
            //----------------------------------------
            if (float.TryParse(textBox_SweepStart.Text, out pre_sweep_start) == false)
            {
                pre_sweep_start = 0;
            }
            //----------------------------------------
            // Get the sweep after change from textbox
            //----------------------------------------
            if (float.TryParse(textBox_SweepStop.Text, out pre_sweep_stop) == false)
            {
                pre_sweep_stop = 0;
            }
            //----------------------------------------
            // Get the simple sweep from combobox
            //----------------------------------------
            pre_sweep_type = (ushort)comboBox_SweepType.SelectedIndex;
            //----------------------------------------
            // Get the sweep time from combobox
            //----------------------------------------
            pre_sweep_time = (ushort)(comboBox_SweepTime.SelectedIndex + 1);
            //----------------------------------------
            // Transfer the value set in the child dialog to the main
            // Set on main
            //----------------------------------------
            m_amplitude      = pre_amplitude;
            m_offset         = pre_offset;
            m_amplitude_type = pre_amplitude_type;
            m_amplitude_unit = pre_amplitude_unit;
            m_duty           = pre_duty;
            m_phase          = pre_phase;
            m_sweep_type     = pre_sweep_type;
            m_sweep_time     = pre_sweep_time;
            m_sweep_start    = pre_sweep_start;
            m_sweep_stop     = pre_sweep_stop;
            this.Close();
        }

        //================================================================================
        // It is the process when the Cancel button is clicked
        //================================================================================
        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
