using CdigCs;

namespace Infinity
{
    public partial class Form1 : Form
    {
        Cdig dig = new Cdig();
        public const int MAX_SAMPLES = 10000;           // The definition of the maximum number of acquiring samples at one time : 1000

        public short m_Id;                              // Device ID
        public float[] m_Data = new float[MAX_SAMPLES]; // Array of retrieving data
        public int m_Samples;                           // Number of retrieved samples
        public int m_TotalSamples;                      // Total number of samples

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Dialog init
        /// </summary>
        private void Form1_Load(object sender, System.EventArgs e)
        {
            // Initialize input range
            comboBoxRange.ResetText();
            comboBoxRange.Items.AddRange(new object[]
                {
                    "-20 - +20V",
                    "-10 - +10V",
                    "-5 - +5V",
                    "-2 - +2V",
                    "-1 - +1V",
                    "-0.5 - +0.5V",
                    "-0.2 - +0.2V",
                    "-0.1 - +0.1V",
                    "-0.05 - +0.05V",
                    "-0.02 - +0.02V"}
                );
        }

        /// <summary>
        /// Init handling
        /// </summary>
        private void buttonInit_Click(object sender, System.EventArgs e)
        {
            int Ret;
            string ErrorString;

            //--------------------------------
            // Init handling
            //--------------------------------
            Ret = dig.Init(textBoxDevice.Text, out m_Id);
            if (Ret != 0)
            {
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.Init = " + Ret.ToString() + " : " + ErrorString;
                return;
            }

            //--------------------------------
            // Reset device
            //--------------------------------
            Ret = dig.ResetDevice(m_Id);
            if (Ret != 0)
            {
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.ResetDevice = " + Ret.ToString() + " : " + ErrorString;
                return;
            }

            textBoxInfo.Text = "Init : Normality completion";
        }

        /// <summary>
        /// Set input range
        /// </summary>
        private void comboBoxRange_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int Index;          // The selected index value of input range combobox
            float Range;        // Input range
            Index = comboBoxRange.SelectedIndex;
            switch (Index)
            {
                case 0: Range = Cdig.DIG_RANGE_40VPP; break;
                case 1: Range = Cdig.DIG_RANGE_20VPP; break;
                case 2: Range = Cdig.DIG_RANGE_10VPP; break;
                case 3: Range = Cdig.DIG_RANGE_4VPP; break;
                case 4: Range = Cdig.DIG_RANGE_2VPP; break;
                case 5: Range = Cdig.DIG_RANGE_1VPP; break;
                case 6: Range = Cdig.DIG_RANGE_400MVPP; break;
                case 7: Range = Cdig.DIG_RANGE_200MVPP; break;
                case 8: Range = Cdig.DIG_RANGE_100MVPP; break;
                case 9: Range = Cdig.DIG_RANGE_40MVPP; break;
                default: Range = 0; break;
            }

            //--------------------------
            // Get the input range from combobox
            //--------------------------
            int Ret;
            Ret = dig.SetRange(m_Id, Cdig.DIG_CHANNEL0, Range);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetRange = " + Ret.ToString() + " : " + ErrorString;
                return;
            }

            textBoxInfo.Text = "Set input range : Normality completion";
        }

        /// <summary>
        /// Start acquisition
        /// </summary>
        private void buttonStart_Click(object sender, System.EventArgs e)
        {
            int Ret;        // Return value
            //---------------------------------
            // Initialize variable
            //---------------------------------
            m_Samples = 0;
            m_TotalSamples = 0;

            //---------------------------------
            // Set operation mode : Data logger mode
            //---------------------------------
            Ret = dig.SetDigitizerMode(m_Id, Cdig.DIG_MODE_LOGGER);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetDigitizerMode = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            //---------------------------------
            // Set sampling clock source : Crystal oscillator of board 
            //---------------------------------
            Ret = dig.SetClockSource(m_Id, Cdig.DIG_ECU_SRC_CLOCK);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetClockSource = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            //---------------------------------
            // Set sampling clock : 1000usec  (set value * 10 + 10 (nsec))
            //---------------------------------
            Ret = dig.SetSampleClock(m_Id, 99999);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetSampleClock = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            //---------------------------------
            // Set the number of acquiring records : 1
            //---------------------------------
            Ret = dig.SetRecordNumber(m_Id, 1);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetRecordNumber = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            //---------------------------------
            // Set the number of pre-trigger samples : 1000
            //---------------------------------
            Ret = dig.SetPreSampleTimes(m_Id, MAX_SAMPLES);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetPreSampleTimes = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            //---------------------------------
            // Set the number of post-trigger samples : 2
            //---------------------------------
            Ret = dig.SetPostSampleTimes(m_Id, 2);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetPostSampleTimes = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            //---------------------------------
            // Set channel : Enable channel0 only
            //---------------------------------
            // Enable channel0
            Ret = dig.SetChannel(m_Id, Cdig.DIG_CHANNEL0, Cdig.DIG_ENABLE);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetChannel = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            // Disable channel1
            Ret = dig.SetChannel(m_Id, Cdig.DIG_CHANNEL1, Cdig.DIG_DISABLE);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetChannel = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            //---------------------------------
            // Set the type of pre-trigger : Auto
            //---------------------------------
            Ret = dig.SetPreTriggerType(m_Id, Cdig.DIG_TRIGGERTYPE_AUTO);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetPreTritterType = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            //---------------------------------
            // Set the type of post-trigger : Software
            //---------------------------------
            Ret = dig.SetPostTriggerType(m_Id, Cdig.DIG_TRIGGERTYPE_SOFT);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.SetPostTritterType = " + Ret.ToString() + " : " + ErrorString;
                return;
            }

            //---------------------------------
            // Operation start
            //---------------------------------
            Ret = dig.Start(m_Id);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.Start = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            textBoxInfo.Text = "Start acquisition : Normality completion";

            //---------------------------------
            // Polling until the operation status is changed to be OFF
            //---------------------------------
            int Status;
            int TriggerPoint;           // Position of the first data after the establishment of the post-trigger

            do
            {
                //---------------------------------
                // In order to enable the dialog operation
                //---------------------------------
                System.Windows.Forms.Application.DoEvents();
                //---------------------------------
                // Get the number of samples stored in driver memory
                //---------------------------------
                Ret = dig.GetDriverMemorySamples(m_Id, out m_Samples);
                if (Ret != 0)
                {
                    string ErrorString;
                    dig.GetErrorString(Ret, out ErrorString);
                    textBoxInfo.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
                    return;
                }
                labelCount.Text = m_Samples.ToString();
                //---------------------------------
                // Get the total number of acquired samples from start
                //---------------------------------
                Ret = dig.GetDigitizedSamples(m_Id, out m_TotalSamples);
                if (Ret != 0)
                {
                    string ErrorString;
                    dig.GetErrorString(Ret, out ErrorString);
                    textBoxInfo.Text = "dig.GetDigitizedSamples = " + Ret.ToString() + " : " + ErrorString;
                    return;
                }
                labelTotal.Text = m_TotalSamples.ToString();
                //---------------------------------
                // Retrieve the acquisition data
                //---------------------------------
                if (m_Samples > 0)
                {
                    // Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
                    if (m_Samples > MAX_SAMPLES)
                    {
                        m_Samples = MAX_SAMPLES;
                    }
                    Ret = dig.ReadSamples(m_Id, ref m_Samples, ref m_Data, out TriggerPoint);
                    if (Ret != 0)
                    {
                        string ErrorString;
                        dig.GetErrorString(Ret, out ErrorString);
                        textBoxInfo.Text = "dig.ReadSamples = " + Ret.ToString() + " : " + ErrorString;
                        return;
                    }
                }
                //---------------------------------
                // Get the operation status
                //---------------------------------
                Ret = dig.GetStatus(m_Id, out Status);
                if (Ret != 0)
                {
                    string ErrorString;
                    dig.GetErrorString(Ret, out ErrorString);
                    textBoxInfo.Text = "dig.GetStatus = " + Ret.ToString() + " : " + ErrorString;
                    return;
                }
                labelStatus.Text = Status.ToString("X") + "H";

            } while ((Status & Cdig.DIG_STATUS_BUSY) == Cdig.DIG_STATUS_BUSY);
            //---------------------------------
            // After operation stop, get the number of samples which are being stored in driver memory
            //---------------------------------
            Ret = dig.GetDriverMemorySamples(m_Id, out m_Samples);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            labelCount.Text = m_Samples.ToString();
            //---------------------------------
            // Get the total number of acquired samples from start
            //---------------------------------
            Ret = dig.GetDigitizedSamples(m_Id, out m_TotalSamples);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetDigitizedSamples = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            labelTotal.Text = m_TotalSamples.ToString();
            //---------------------------------
            // Retrieve the acquisition data
            //---------------------------------
            // Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
            if (m_Samples > 0)
            {
                if (m_Samples > MAX_SAMPLES)
                {
                    m_Samples = MAX_SAMPLES;
                }
                Ret = dig.ReadSamples(m_Id, ref m_Samples, ref m_Data, out TriggerPoint);
                if (Ret != 0)
                {
                    string ErrorString;
                    dig.GetErrorString(Ret, out ErrorString);
                    textBoxInfo.Text = "dig.ReadSamples = " + Ret.ToString() + " : " + ErrorString;
                    return;
                }
            }
        }

        /// <summary>
        /// Get status
        /// </summary>
        private void buttonInfo_Click(object sender, System.EventArgs e)
        {
            int Ret;        // Return value
            int Status;     // Status
            int Samples;    // Number of retrieved samples
            int Total;      // Total number of samples
            //---------------------------------
            // Get the number of samples stored in driver memory
            //---------------------------------
            Ret = dig.GetDriverMemorySamples(m_Id, out Samples);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            labelCount.Text = Samples.ToString();
            //---------------------------------
            // Get the total number of acquired samples from start
            //---------------------------------
            Ret = dig.GetDigitizedSamples(m_Id, out Total);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetDigitizedSamples = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            labelTotal.Text = Total.ToString();
            //---------------------------------
            // Get the operation status
            //---------------------------------
            Ret = dig.GetStatus(m_Id, out Status);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetStatus = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            labelStatus.Text = Status.ToString("X") + "H";
            textBoxInfo.Text = "Get status : Normality completion";
        }

        /// <summary>
        /// Stop acquisition
        /// </summary>
        private void buttonStop_Click(object sender, System.EventArgs e)
        {
            int Ret;        // Return value
            Ret = dig.Stop(m_Id);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.Stop = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            textBoxInfo.Text = "Stop acquisition : Normality completion";
        }

        /// <summary>
        /// Exit handling
        /// </summary>
        private void buttonExit_Click(object sender, System.EventArgs e)
        {
            int Ret;    // Return value
            Ret = dig.Exit(m_Id);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.Exit = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            textBoxInfo.Text = "Exit : Normality completion";
        }

        /// <summary>
        /// Close Form
        /// </summary>
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Exit handling
            dig.Exit(m_Id);
        }
    }
}