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 System.Threading;
namespace Osciloscop
{
    public partial class Form1 : Form
    {

        byte[] recv = new byte[2101];
        byte[] recv2 = new byte[2101];
        int l;
        private int[] SignalRecv = new int[2100];

        private int[] SignalShow = new int[1600];
        private int[] TriggerShow = new int[1600];
        private bool Slope;

        private bool recvStarted;
        private bool recvEnded;

        private byte[] recvFullline = new byte[5000];
        private int recvIndex;

        private byte XVal;
        int x = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var ports = System.IO.Ports.SerialPort.GetPortNames();
            COMPorts.DataSource = ports;

            for (int i = 0; i < COMPorts.Items.Count; i++)
            {
                if (COMPorts.Items[i].ToString() == Properties.Settings.Default["com_port"].ToString())
                    COMPorts.SelectedIndex = i;
            }

            XVal = 0;
            Slope = false;
            TriggerTrackbar.Value = 0;
            TriggerTrackbar_ValueChanged(sender, e);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default["com_port"] = COMPorts.Text;
            Properties.Settings.Default.Save();
        }

        private void UpdateChart()
        {
         

            chart1.Series["ValueLine"].Points.Clear();
            for (int i = 0; i < 2099; i = i + 2)
            {

                switch (recv2[i + 1])
                {
                    case 0:
                        SignalRecv[j] = recv2[i] + 0;
                        break;
                    case 1:
                        SignalRecv[j] = recv2[i] + 256;
                        break;
                    case 2:
                        SignalRecv[j] = recv2[i] + 0;
                        break;
                    case 3:
                        SignalRecv[j] = recv2[i] + 256;
                        break;

                }


                j++;
            }

            for (int i = 0; i < 1010; i++)
            {

                chart1.Series["ValueLine"].Points.AddXY(i, SignalRecv[i]);
            }
            j = 0;
        }

        private void COMConnectBtn_Click(object sender, EventArgs e)
        {
            if (serialPort.IsOpen == false)
            {
                chart1.Series["ValueLine"].Points.Clear();
                //connect the unit
                serialPort.BaudRate = 115200;
                serialPort.Parity = System.IO.Ports.Parity.None;
                serialPort.StopBits = System.IO.Ports.StopBits.One;
                serialPort.RtsEnable = false;
                serialPort.PortName = COMPorts.Items[COMPorts.SelectedIndex].ToString();
                serialPort.DtrEnable = false;
                serialPort.DataBits = 8;
                serialPort.Open();
                if (serialPort.IsOpen)
                {
                    COMConnectBtn.Text = "Disconnect";
                
                }
            }
            else
            {
                serialPort.Close();
            }
        }

        private void InterfaceTimer_Tick(object sender, EventArgs e)
        {
            if (serialPort.IsOpen)
            {
                COMConnectBtn.Text = "Disconnect";
             //   SnapshotTimer.Enabled = true;
            }
            else
                COMConnectBtn.Text = "Connect";

            chart1.ChartAreas[0].AxisX.Title = "Samples at " + (1000 / (1 << XVal ) ).ToString() + " KSPS";
        }

    

        private void SnapshotTimer_Tick(object sender, EventArgs e)
        {
            byte[] command = new byte[1];
            command[0] = 80;
            serialPort.Write(command, 0, 1);
            Thread.Sleep(100);
            serialPort.Write(command, 0, 1);
            Thread.Sleep(100);
            serialPort.Write(command, 0, 1);

            if (serialPort.IsOpen == false)
                return;

            try
            {
                l = serialPort.Read(recv, 0, 2099);
            }
            catch (TimeoutException) { l = 0; }
            for (int i = 0; i < 2099; i = i + 1)
            {

                recv2[i] = recv[i];

            }
            UpdateChart();
        }

        private void SlopeLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if(Slope == false)
            {
                Slope = true;
                SlopeLink.Text = "Negative";
            }
            else
            {
                Slope = false;
                SlopeLink.Text = "Positive";
            }
        }

        private void TriggerTrackbar_ValueChanged(object sender, EventArgs e)
        {
           
        }

        private void XValPlus_Click(object sender, EventArgs e)
        {
            if (XVal > 0)
                XVal--;
        }

        private void XValMinus_Click(object sender, EventArgs e)
        {
            if (XVal < 6)
                XVal++;
        }

        private void HorizScale_ValueChanged(object sender, EventArgs e)
        {
            chart1.ChartAreas[0].AxisX.Maximum = HorizScale.Value;
        }

        private void chart1_Click(object sender, EventArgs e)
        {
             
        }

        private void button1_Click(object sender, EventArgs e)
        {
          //serialTimer.Enabled=true;
            label1.Visible = true;
            Enable.BackColor = System.Drawing.Color.DarkGreen;
         SnapshotTimer.Enabled=true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (serialPort.IsOpen == false)
                return;

            try
            {
                l = serialPort.Read(recv, 0, 2099);
            }
            catch (TimeoutException) { l = 0; }
            for (int i = 0; i < 2099; i = i + 1)
            {

                recv2[i] = recv[i];
                
            }
        }
        int j = 0;
        private void button3_Click(object sender, EventArgs e)
        {
            chart1.Series["ValueLine"].Points.Clear();
            for (int i = 0; i < 2099; i=i+2)
            {

                switch (recv2[i+1])
                {
                    case 0:
                        SignalRecv[j] = recv2[i] + 0;
                        break;
                    case 1:
                        SignalRecv[j] = recv2[i] + 256;
                        break;
                    case 2:
                        SignalRecv[j] = recv2[i] + 0;
                        break;
                    case 3:
                        SignalRecv[j] = recv2[i] + 256;
                        break;
                    
                }


                j++;
           }

            for (int i = 0; i < 1010; i++)
            {


                chart1.Series["ValueLine"].Points.AddXY(i, SignalRecv[i]);
            }
            j = 0;

        }

        private void COMPorts_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void button4_Click(object sender, EventArgs e)
        {
            int value = recv[x+10];
            int value2 = recv[x+11];
            string binary = Convert.ToString(recv[x], 2);
            string binary2 = Convert.ToString(recv[x+1], 2);
            bin.Text = binary2 + " ";
            bin2.Text = binary + " ";
        }

        private void numericUpDown1_MouseDown(object sender, MouseEventArgs e)
        {
            x = (int)num.Value;
            int value = recv[x + 10];
            int value2 = recv[x + 11];
            string binary = Convert.ToString(recv[x], 2);
            string binary2 = Convert.ToString(recv[x + 1], 2);
            bin.Text = binary2 + " ";
            bin2.Text = binary + " ";
            string binary3 = Convert.ToString(SignalRecv[x], 2);
            bx.Text = recv[x+1] + " ";
        
        }

        private void serialTimer_Tick(object sender, EventArgs e)
        {
            if (serialPort.IsOpen == false)
                return;

            try
            {
                l = serialPort.Read(recv, 0, 2099);
            }
            catch (TimeoutException) { l = 0; }
            for (int i = 0; i < 2099; i = i + 1)
            {

                recv2[i] = recv[i];

            }
            UpdateChart();
        }

        private void HorizScale_Scroll(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }
    }
}
