using CdigCs;

namespace LoadConfig
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// NXCdigCX^X쐬
        /// </summary>
        Cdig             dig         = new Cdig();
        public const int MAX_SAMPLES = 1024;                      // őTv̐ݒ

        public short     m_Id;                                    // foCXID
        public float[]   m_Data      = new float[MAX_SAMPLES];    // 擾f[^̔z
        public int       m_Records;                               // 擾R[h
        public float     m_Range;                                 // ͓dW
        public int       m_RecordLength;                          // R[h
        public short     m_TriggerChannel;                        // |XggK`l
        public short     m_Channels;                              // L`l
        public Bitmap    bmp;                                     // rbg}bv

        public Form1()
        {
            InitializeComponent();
            bmp = new Bitmap(Graph.Size.Width, Graph.Size.Height);    // OtƓTCỸrbg}bv𐶐
        }

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

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

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

            textBoxInfo.Text = " : I";
        }

        /// <summary>
        /// ݒt@Cǂݍ
        /// </summary>
        private void buttonLoad_Click(object sender, System.EventArgs e)
        {
            int    Ret;         // ߂l
            string FilePath;    // t@CpX

            //----------------------------------
            // t@CI[v_CAO\܂B
            //----------------------------------
            OpenFileDialog LoadDialog = new OpenFileDialog();

            // [t@C̎] {bNXɕ\Iݒ肷
            LoadDialog.Filter = "xml files (*.xml)|*.xml";
            if (LoadDialog.ShowDialog() == DialogResult.OK)
            {
                FilePath = LoadDialog.FileName;    // Iꂽt@CpX̎擾
                Ret = dig.LoadConfig(m_Id, FilePath);
                if (Ret != 0)
                {
                    string ErrorString;
                    dig.GetErrorString(Ret, out ErrorString);
                    textBoxInfo.Text = "dig.Start = " + Ret.ToString() + " : " + ErrorString;
                    return;
                }
                textBoxInfo.Text = "ݒt@Cǂݍ݁FI";
            }
        }

        /// <summary>
        /// WJn
        /// </summary>
        private void buttonStart_Click(object sender, System.EventArgs e)
        {
            int Ret;     // ߂l

            //---------------------------------
            // ϐ
            //---------------------------------
            m_Records = 0;

            //---------------------------------
            // ZbgAbv擾/\
            //---------------------------------
            buttonInfo_Click(buttonInfo, new System.EventArgs());

            //---------------------------------
            // Jn
            //---------------------------------
            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 = "WJn : I";

            //---------------------------------
            // 쒆Xe[^XOFFɂȂ܂Ń|[O
            //---------------------------------
            int Status;

            do
            {
                //---------------------------------
                // _CAOLɂ邽߂̏
                //---------------------------------
                System.Windows.Forms.Application.DoEvents();
                //---------------------------------
                // hCoɊi[ꂽR[h擾
                //---------------------------------
                Ret = dig.GetDriverMemoryRecords(m_Id, out m_Records);
                if (Ret != 0)
                {
                    string ErrorString;
                    dig.GetErrorString(Ret, out ErrorString);
                    textBoxInfo.Text = "dig.GetDriverMemoryRecords = " + Ret.ToString() + " : " + ErrorString;
                    return;
                }
                //---------------------------------
                // hCoɃR[h݂΁AVf[^擾
                //---------------------------------
                if (m_Records > 0 || m_Records == -1)
                {
                    Ret = dig.ReadLatestRecords(m_Id, Cdig.DIG_RECORD_DATA, 1, ref m_Data, 0, 0);
                    if (Ret != 0)
                    {
                        string ErrorString;
                        dig.GetErrorString(Ret, out ErrorString);
                        textBoxInfo.Text = "dig.ReadLatestRecordss = " + Ret.ToString() + " : " + ErrorString;
                        return;
                    }
                    //------------------------------------
                    // Ot̕`
                    //------------------------------------
                    DrawGraph();
                }
                //---------------------------------
                // Xe[^X̎擾
                //---------------------------------
                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);
            //---------------------------------
            // ~ɃhCoɊi[Ă郌R[h擾
            //---------------------------------
            Ret = dig.GetDriverMemoryRecords(m_Id, out m_Records);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetDriverMemoryRecords = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            //---------------------------------
            // hCoɃR[h݂΁AVf[^擾
            //---------------------------------
            if (m_Records > 0)
            {
                Ret = dig.ReadLatestRecords(m_Id, Cdig.DIG_RECORD_DATA, 1, ref m_Data, 0, 0);
                if (Ret != 0)
                {
                    string ErrorString;
                    dig.GetErrorString(Ret, out ErrorString);
                    textBoxInfo.Text = "dig.ReadLatestRecordss = " + Ret.ToString() + " : " + ErrorString;
                    return;
                }
                //------------------------------------
                // Ot̕`
                //------------------------------------
                DrawGraph();
            }
        }

        /// <summary>
        /// 擾
        /// </summary>
        private void buttonInfo_Click(object sender, System.EventArgs e)
        {
            int    Ret;           // ߂l
            float  SampleRate;    // Tv[g
            float  Duty;          // f[eB
            float  Level;         // gKx
            short  Slope;         // gK
            int    Count;         // gK
            int    Status;        // Xe[^X
            string Str = "";

            //---------------------------------
            // Xe[^X̎擾
            //---------------------------------
            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";

            //-------------------------------------------
            // DigLoadConfig֐ŎIɐݒ肳ɂĎ擾܂B
            //-------------------------------------------
            //---------------------------------
            // Tv[g̎擾
            //---------------------------------
            Ret = dig.GetSampleRate(m_Id, out SampleRate);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetSampleRate = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            Str = String.Format("Tv[gF{0:F3}Hz\r\n", SampleRate);
            //---------------------------------
            // R[h̎擾
            //---------------------------------
            Ret = dig.GetRecordLength(m_Id, out m_RecordLength);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetRecordLength = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            Str = Str + String.Format("R[hF{0:D}\r\n", m_RecordLength);
            //---------------------------------
            // f[eB̎擾
            //---------------------------------
            Ret = dig.GetDutyCycle(m_Id, out Duty);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetRecordLength = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            Str = Str + String.Format("f[eBF{0:F3}%\r\n", Duty);
            //---------------------------------
            // Lȃ`l̎擾
            //---------------------------------
            Ret = dig.GetChannels(m_Id, out m_Channels);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetChannels = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            Str = Str + String.Format("L`lF{0:D}\r\n", m_Channels);
            //---------------------------------
            // gK̎擾
            //---------------------------------
            Ret = dig.GetPostTriggerAiEdge(m_Id, out m_TriggerChannel, out Level, out Slope, out Count);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetPostTriggerAiEdge = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            Str = Str + String.Format("`lF{0:D}\r\n", m_TriggerChannel);
            Str = Str + String.Format("gKxF{0:F3}V\r\n", Level);
            //---------------------------------
            // W̎擾
            //---------------------------------
            Ret = dig.GetRange(m_Id, m_TriggerChannel, out m_Range);
            if (Ret != 0)
            {
                string ErrorString;
                dig.GetErrorString(Ret, out ErrorString);
                textBoxInfo.Text = "dig.GetRange = " + Ret.ToString() + " : " + ErrorString;
                return;
            }
            Str = Str + String.Format("̓WF{0:F3}VPP\r\n", m_Range);
            //---------------------------------
            // \
            //---------------------------------
            textBoxDeviceInfo.Text = Str;

            textBoxInfo.Text = "擾 : I";
        }

        /// <summary>
        /// W~
        /// </summary>
        private void buttonStop_Click(object sender, System.EventArgs e)
        {
            int Ret;    // ߂l
            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 = "W~ : I";
        }

        /// <summary>
        /// I
        /// </summary>
        private void buttonExit_Click(object sender, System.EventArgs e)
        {
            int Ret;    // ߂l
            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 = "I : I";
        }

        /// <summary>
        /// tH[I
        /// </summary>
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // I
            dig.Exit(m_Id);
        }

        /// <summary>
        /// Ot`
        /// </summary>
        private void DrawGraph()
        {
            float XUnit;    // X`C^[o
            float PosX;     // X`ʒu
            float PosY;     // Y`ʒu
            float OldX;     // XWޔp
            float OldY;     // YWޔp
            Pen pen;        // `py

            // ݂̓dOtɕ`悵܂B
            Graph.Image = bmp;
            Graphics grp = Graphics.FromImage(Graph.Image);                  // rbg}bv`ΏۃIuWFNg𐶐
            grp.Clear(Graph.BackColor);                                      // OtNA

            //----------------------------------------------------
            // L`lA`揈JԂ܂B
            //----------------------------------------------------
            for (int i = 0; i < m_Channels; i++)
            {
                //----------------------------------------------------
                // Wf[^A`揈JԂ܂B
                //----------------------------------------------------
                if (i == 0)
                {
                    pen = new Pen(System.Drawing.Color.Red, 1);     // ԐFy̍쐬
                }
                else
                {
                    pen = new Pen(System.Drawing.Color.Blue, 1);    // Fy̍쐬
                }
                XUnit = Graph.Size.Width / (float)(m_RecordLength);             // 1f[^̉ړsNZ
                OldY = Graph.Height * ((m_Range / 2) - m_Data[i]) / m_Range;    // Y̏lvZ
                                                                                // Y̍W`̈𒴂ꍇ
                if (OldY >= Graph.Height)
                {
                    OldY = Graph.Height - 1;    // `ʒȕC
                }
                // Y̍W`̈ꍇ
                else if (OldY < 0)
                {
                    OldY = 0;    // `ʒȕC
                }
                OldX = 0;
                for (int j = i; j < m_RecordLength * m_Channels; j += m_Channels)
                {
                    PosX = OldX + XUnit;                                            // X̍WvZ
                    PosY = Graph.Height * ((m_Range / 2) - m_Data[j]) / m_Range;    // Y̍WvZ
                                                                                    // Y̍W`̈𒴂ꍇ
                    if (PosY >= Graph.Height)
                    {
                        PosY = Graph.Height - 1;    // `ʒȕC
                    }
                    // Y̍W`̈ꍇ
                    else if (PosY < 0)
                    {
                        PosY = 0;    // `ʒȕC
                    }
                    grp.DrawLine(pen, OldX, OldY, PosX, PosY);    // `

                    OldX = PosX;    // X̍W`pɕێ
                    OldY = PosY;    // Y̍W`pɕێ
                }
                pen.Dispose();    // \[X̉
            }
            grp.Dispose();    // \[X̉
        }
    }
}