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.IO.Ports;
using System.Threading;
using System.IO;
using Renci.SshNet;

// Based on, see below
// https://www.codeproject.com/Articles/678025/Serial-Comms-in-Csharp-for-Beginners


namespace uartTestGUI
{
    public partial class Form1 : Form
    {


        bool system_on = false;

        SshClient ssh_client;

        public Form1()
        {
            InitializeComponent();

            if (cw_w1.Checked == true)
            {
                end_freq_w1.ReadOnly = true;
            }
            if (cw_w2.Checked == true)
            {
                end_freq_w2.ReadOnly = true;
            }
            if (cw_w3.Checked == true)
            {
                end_freq_w3.ReadOnly = true;
            }
            if (cw_w4.Checked == true)
            {
                end_freq_w4.ReadOnly = true;
            }



            //            Thread oThread = new Thread(new ThreadStart(backgroundTask));
            //            oThread.IsBackground = true;
            //            oThread.Start();


        }

        public void backgroundTask()
        {
            // while (true == status_update_en.Checked)
            while (true)
            {

                if (true == system_on)
                {

                }

                Thread.Sleep(1000);
            }

        }


        private void w0_send_button_Click(object sender, EventArgs e)
        {
            send_button_w1.Text = "Loading...";
            send_button_w1.BackColor = Color.LightGreen;

            double actual_start_freq;
            double actual_end_freq;
            double actual_duration;
            double actual_idle_samples;

            string str = String.Empty;
            str += "************************";
            str += "\n";
            richTextBox0.Text = str;

            double start_freq = (Convert.ToDouble(start_freq_w1.Text)) * 1e6;
            double end_freq = (Convert.ToDouble(end_freq_w1.Text)) * 1e6;
            double duration = (Convert.ToDouble(duration_w1.Text)) * 1e-6;
            double idle_samples = (Convert.ToDouble(idle_samples_w1.Text)) * 1e-6;
            uint phase_inc_dwell = (Convert.ToUInt32(phase_inc_dwell_w1.Text, 10));

            ComputeSend_PDW(1, chirp_w1.Checked, start_freq, end_freq, duration, idle_samples, phase_inc_dwell, out actual_start_freq, out actual_end_freq, out actual_duration, out actual_idle_samples);
            actual_start_freq_w1.Text = actual_start_freq.ToString("G6");
            actual_end_freq_w1.Text = actual_end_freq.ToString("G6");
            actual_duration_w1.Text = actual_duration.ToString("G6");
            actual_idle_samples_w1.Text = actual_idle_samples.ToString("G6");

            double scale_factor = (double)scale_factor_w1.Value;
            Set_ScaleFactor(1, scale_factor, iq_swap_w1.Checked);

            ssh_WriteReg("0x83000028", "00000000");// phase_off_1
            dds_reg28.Text = "0x00000000";

            send_button_w1.Text = "Load";
            send_button_w1.BackColor = Color.Gainsboro;
            dds_send_w1.BackColor = Color.Yellow;

        }

        private void SendWord_Decimal(string cmd, int word)
        {
            string str = String.Empty;

            str += cmd;
            str += word.ToString("D");
            str += "\n";

            richTextBox0.AppendText(str);

        }

        private void Set_ScaleFactor(uint chan, double scale_db, bool flag)
        {
            string str = String.Empty;

            uint scale_factor = (uint)(Math.Pow(2.0, scale_db / 6.0) * 0x8000);
            uint swap_sf;

            Console.WriteLine("scale_db: {0} ", scale_db);
            str += "scale_db: ";
            str += scale_db.ToString("N1");
            str += "\n";
            richTextBox0.AppendText(str);

            swap_sf = scale_factor;

            if (flag == true)
            {
                swap_sf = swap_sf | 0x10000;
            }
            else
            {
                swap_sf = swap_sf & 0xFFFEFFFF;
            }

            str = String.Empty;
            str += "scale_factor: ";
            str += scale_factor.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);

            if (chan == 1)
            {
                ssh_WriteReg("0x83000010", "0x" + scale_factor.ToString("X8"));
                dds_reg10.Text = scale_factor.ToString("X8");

                ssh_WriteReg("0x8300002C", "0x" + swap_sf.ToString("X8"));
                dds_reg2C.Text = swap_sf.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x83000030", "0x" + scale_factor.ToString("X8"));
                dds_reg30.Text = scale_factor.ToString("X8");

                ssh_WriteReg("0x8300004C", "0x" + swap_sf.ToString("X8"));
                dds_reg4C.Text = swap_sf.ToString("X8");
            }
            else if (chan == 3)
            {
                ssh_WriteReg("0x83000050", "0x" + scale_factor.ToString("X8"));
                dds_reg50.Text = scale_factor.ToString("X8");

                ssh_WriteReg("0x8300006C", "0x" + swap_sf.ToString("X8"));
                dds_reg6C.Text = swap_sf.ToString("X8");
            }
            else if (chan == 4)
            {
                ssh_WriteReg("0x83000070", "0x" + scale_factor.ToString("X8"));
                dds_reg70.Text = scale_factor.ToString("X8");

                ssh_WriteReg("0x8300008C", "0x" + swap_sf.ToString("X8"));
                dds_reg8C.Text = swap_sf.ToString("X8");
            }

        }


        private void dac_on_Click(object sender, EventArgs e)
        {
            //            send_button_w1.Text = "Send";

            ssh_WriteReg("0x83000004", "0x00000000");  // set bit0 - 0 = RF On

            dac_off.BackColor = Color.LightGray;
            dac_on.BackColor = Color.LightGreen;
            system_status.BackColor = Color.LightGreen;
            if (tx_fiber_adcs_src.Checked == true)
            {
                system_status.Text = "ARMED";

            }
            else if (tx_fiber_dds_src.Checked == true)
            {
                system_status.Text = "ARMED";
            }

            system_on = true;
        }

        private void dac_off_Click(object sender, EventArgs e)
        {
            //            send_button_w1.Text = "Load";

            ssh_WriteReg("0x83000004", "0x00000001");  // set bit0 - 1 = RF Off

            system_status.Text = "OFF";
            dac_on.BackColor = Color.LightGray;
            dac_off.BackColor = Color.Red;
            system_status.BackColor = Color.LightGray;
            system_on = false;
        }

        private void chirp_w0_Click(object sender, EventArgs e)
        {
            end_freq_w1.ReadOnly = false;
        }

        private void cw_w0_Click(object sender, EventArgs e)
        {
            end_freq_w1.ReadOnly = true;
        }

        private void chirp_w1_Click(object sender, EventArgs e)
        {
            end_freq_w2.ReadOnly = false;
        }

        private void cw_w1_Click(object sender, EventArgs e)
        {
            end_freq_w2.ReadOnly = true;
        }


        private void chirp_w2_Click(object sender, EventArgs e)
        {
            end_freq_w3.ReadOnly = false;
        }

        private void cw_w2_Click(object sender, EventArgs e)
        {
            end_freq_w3.ReadOnly = true;
        }



        private void save_pdw_file_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Save PDW File";
            saveFileDialog.Filter = "PDW Files|*.txt";
            saveFileDialog.RestoreDirectory = true;

            string pdw_data = String.Empty;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = saveFileDialog.FileName;

                Console.WriteLine("filename: {0} ", filename);

                try
                {
                    using (StreamWriter writer = new StreamWriter(filename))
                    {
                        pdw_data = idx_w1.Text;
                        pdw_data += ",";
                        pdw_data += (chirp_w1.Checked == true) ? "1" : "0";
                        pdw_data += ",";
                        pdw_data += start_freq_w1.Text;
                        pdw_data += ",";
                        pdw_data += end_freq_w1.Text;
                        pdw_data += ",";
                        pdw_data += duration_w1.Text;
                        pdw_data += ",";
                        pdw_data += idle_samples_w1.Text;
                        pdw_data += ",";
                        pdw_data += phase_inc_dwell_w1.Text;
                        pdw_data += ",";
                        pdw_data += scale_factor_w1.Value.ToString("N1");
                        pdw_data += ",";
                        pdw_data += (iq_swap_w1.Checked == true) ? "1" : "0";
                        writer.WriteLine(pdw_data);

                        pdw_data = String.Empty;
                        pdw_data = idx_w2.Text;
                        pdw_data += ",";
                        pdw_data += (chirp_w2.Checked == true) ? "1" : "0";
                        pdw_data += ",";
                        pdw_data += start_freq_w2.Text;
                        pdw_data += ",";
                        pdw_data += end_freq_w2.Text;
                        pdw_data += ",";
                        pdw_data += duration_w2.Text;
                        pdw_data += ",";
                        pdw_data += idle_samples_w2.Text;
                        pdw_data += ",";
                        pdw_data += phase_inc_dwell_w2.Text;
                        pdw_data += ",";
                        pdw_data += scale_factor_w2.Value.ToString("N1");
                        pdw_data += ",";
                        pdw_data += (iq_swap_w2.Checked == true) ? "1" : "0";
                        writer.WriteLine(pdw_data);

                        pdw_data = String.Empty;
                        pdw_data = idx_w3.Text;
                        pdw_data += ",";
                        pdw_data += (chirp_w3.Checked == true) ? "1" : "0";
                        pdw_data += ",";
                        pdw_data += start_freq_w3.Text;
                        pdw_data += ",";
                        pdw_data += end_freq_w3.Text;
                        pdw_data += ",";
                        pdw_data += duration_w3.Text;
                        pdw_data += ",";
                        pdw_data += idle_samples_w3.Text;
                        pdw_data += ",";
                        pdw_data += phase_inc_dwell_w3.Text;
                        pdw_data += ",";
                        pdw_data += scale_factor_w3.Value.ToString("N1");
                        pdw_data += ",";
                        pdw_data += (iq_swap_w3.Checked == true) ? "1" : "0";
                        writer.WriteLine(pdw_data);

                        pdw_data = String.Empty;
                        pdw_data = idx_w4.Text;
                        pdw_data += ",";
                        pdw_data += (chirp_w4.Checked == true) ? "1" : "0";
                        pdw_data += ",";
                        pdw_data += start_freq_w4.Text;
                        pdw_data += ",";
                        pdw_data += end_freq_w4.Text;
                        pdw_data += ",";
                        pdw_data += duration_w4.Text;
                        pdw_data += ",";
                        pdw_data += idle_samples_w4.Text;
                        pdw_data += ",";
                        pdw_data += phase_inc_dwell_w4.Text;
                        pdw_data += ",";
                        pdw_data += scale_factor_w4.Value.ToString("N1");
                        pdw_data += ",";
                        pdw_data += (iq_swap_w4.Checked == true) ? "1" : "0";
                        writer.WriteLine(pdw_data);



                    }
                }
                catch (FileNotFoundException ioEx)
                {
                    Console.WriteLine(ioEx.Message);
                }
            }
        }

        private void load_pdw_file_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "Read PDW File";
            openFileDialog.Filter = "PDW Files|*.txt";
            openFileDialog.RestoreDirectory = true;

            string pdw_data = String.Empty;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog.FileName;

                Console.WriteLine("filename: {0} ", filename);

                try
                {
                    using (StreamReader reader = new StreamReader(filename))
                    {
                        pdw_data = reader.ReadLine();  //         ReadLine(pdw_data); 0
                        if (pdw_data == null)
                        {
                            richTextBox0.AppendText("**End of File\n");
                            return;
                        }
                        richTextBox0.AppendText(pdw_data + "\n");
                        string[] data_item = pdw_data.Split(',');

                        for (int i = 0; i < data_item.Length; ++i)
                        {
                            richTextBox0.AppendText(i.ToString() + ":" + data_item[i] + "\n");
                        }

                        idx_w1.Text = data_item[0];
                        chirp_w1.Checked = (data_item[1] == "1") ? true : false;
                        cw_w1.Checked = (data_item[1] == "0") ? true : false;
                        start_freq_w1.Text = data_item[2];
                        end_freq_w1.Text = data_item[3];
                        duration_w1.Text = data_item[4];
                        idle_samples_w1.Text = data_item[5];
                        phase_inc_dwell_w1.Text = data_item[6];
                        scale_factor_w1.Value = Convert.ToDecimal(data_item[7]);
                        iq_swap_w1.Checked = (data_item[8] == "1") ? true : false;

                        pdw_data = reader.ReadLine();  //         ReadLine(pdw_data);1
                        if (pdw_data == null)
                        {
                            richTextBox0.AppendText("**End of File\n");
                            return;
                        }
                        richTextBox0.AppendText(pdw_data + "\n");
                        data_item = pdw_data.Split(',');
                        for (int i = 0; i < data_item.Length; ++i)
                        {
                            richTextBox0.AppendText(i.ToString() + ":" + data_item[i] + "\n");
                        }

                        idx_w2.Text = data_item[0];
                        chirp_w2.Checked = (data_item[1] == "1") ? true : false;
                        cw_w2.Checked = (data_item[1] == "0") ? true : false;
                        start_freq_w2.Text = data_item[2];
                        end_freq_w2.Text = data_item[3];
                        duration_w2.Text = data_item[4];
                        idle_samples_w2.Text = data_item[5];
                        phase_inc_dwell_w2.Text = data_item[6];
                        scale_factor_w2.Value = Convert.ToDecimal(data_item[7]);
                        iq_swap_w2.Checked = (data_item[8] == "1") ? true : false;

                        pdw_data = reader.ReadLine();  //         ReadLine(pdw_data);2
                        if (pdw_data == null)
                        {
                            richTextBox0.AppendText("**End of File\n");
                            return;
                        }
                        richTextBox0.AppendText(pdw_data + "\n");
                        data_item = pdw_data.Split(',');
                        for (int i = 0; i < data_item.Length; ++i)
                        {
                            richTextBox0.AppendText(i.ToString() + ":" + data_item[i] + "\n");
                        }

                        idx_w3.Text = data_item[0];
                        chirp_w3.Checked = (data_item[1] == "1") ? true : false;
                        cw_w3.Checked = (data_item[1] == "0") ? true : false;
                        start_freq_w3.Text = data_item[2];
                        end_freq_w3.Text = data_item[3];
                        duration_w3.Text = data_item[4];
                        idle_samples_w3.Text = data_item[5];
                        phase_inc_dwell_w3.Text = data_item[6];
                        scale_factor_w3.Value = Convert.ToDecimal(data_item[7]);
                        iq_swap_w3.Checked = (data_item[8] == "1") ? true : false;

                        pdw_data = reader.ReadLine();  //         ReadLine(pdw_data);3
                        if (pdw_data == null)
                        {
                            richTextBox0.AppendText("**End of File\n");
                            return;
                        }
                        richTextBox0.AppendText(pdw_data + "\n");
                        data_item = pdw_data.Split(',');
                        for (int i = 0; i < data_item.Length; ++i)
                        {
                            richTextBox0.AppendText(i.ToString() + ":" + data_item[i] + "\n");
                        }

                        idx_w4.Text = data_item[0];
                        chirp_w4.Checked = (data_item[1] == "1") ? true : false;
                        cw_w4.Checked = (data_item[1] == "0") ? true : false;
                        start_freq_w4.Text = data_item[2];
                        end_freq_w4.Text = data_item[3];
                        duration_w4.Text = data_item[4];
                        idle_samples_w4.Text = data_item[5];
                        phase_inc_dwell_w4.Text = data_item[6];
                        scale_factor_w4.Value = Convert.ToDecimal(data_item[7]);
                        iq_swap_w4.Checked = (data_item[8] == "1") ? true : false;

                        pdw_data = reader.ReadLine();  //         ReadLine(pdw_data);4
                        if (pdw_data == null)
                        {
                            richTextBox0.AppendText("**End of File\n");
                            return;
                        }
                        richTextBox0.AppendText(pdw_data + "\n");
                        data_item = pdw_data.Split(',');
                        for (int i = 0; i < data_item.Length; ++i)
                        {
                            richTextBox0.AppendText(i.ToString() + ":" + data_item[i] + "\n");
                        }


                    }
                }
                catch (FileNotFoundException ioEx)
                {
                    Console.WriteLine(ioEx.Message);
                }
            }
        }


        private void chirp_w3_Click(object sender, EventArgs e)
        {
            end_freq_w4.ReadOnly = false;
        }
        private void cw_w3_Click(object sender, EventArgs e)
        {
            end_freq_w4.ReadOnly = true;
        }


 








        private void ComputeSend_PDW(uint chan, bool wave_type, double start_freq, double end_freq, double duration, double idle_samples, uint phase_inc_dwell, out double actual_start_freq, out double actual_end_freq, out double actual_duration, out double actual_idle_samples)
        {
            double sample_rate = 250e6;
            string str = String.Empty;
            double MULT = 4294967296.0;
            int step_size_t;

            str += "************************";
            str += "\n";
            richTextBox0.AppendText(str); //.Text = str;            //  

            str = String.Empty;
            str += "chan: ";
            str += chan.ToString("X1");
            str += "\n";
            richTextBox0.AppendText(str);

            Console.WriteLine("start_freq: {0} ", start_freq);
            str = String.Empty;
            str += "start_freq: ";
            str += start_freq.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            Console.WriteLine("end_freq: {0} ", end_freq);
            str = String.Empty;
            str += "end_freq: ";
            str += end_freq.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            Console.WriteLine("duration: {0} ", duration);
            str = String.Empty;
            str += "duration: ";
            str += duration.ToString("G6");
            str += "\n\n";
            richTextBox0.AppendText(str);

            Console.WriteLine("idle_samples: {0} ", idle_samples);
            str = String.Empty;
            str += "idle_samples: ";
            str += idle_samples.ToString("G6");
            str += "\n";
            richTextBox0.AppendText(str);

            double num_samples = duration / (1 / sample_rate); // num_samples = duration / (1/sample_rate)
            Console.WriteLine("num_samples: {0} ", num_samples);
            str = String.Empty;
            str += "num_samples: ";
            str += num_samples.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);
            uint num_samples_t = (uint)Math.Round(num_samples);/////////////////////////////////////
            str = String.Empty;
            str += "num_samples_t: ";
            str += num_samples_t.ToString("D");
            str += "\n\n";
            richTextBox0.AppendText(str);

            actual_duration = num_samples / sample_rate;
            str = String.Empty;
            str += "actual_duration: ";
            str += actual_duration.ToString("G6");
            str += "\n\n";
            richTextBox0.AppendText(str);

            double start_freq_phase_inc = MULT * (start_freq / sample_rate); // PhaseInc = 2^32 * (desired freq / sample rate)
            Console.WriteLine("start_freq_phase_inc: {0} ", start_freq_phase_inc);
            str = String.Empty;
            str += "start_freq_phase_inc: ";
            str += start_freq_phase_inc.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            double t_double = Math.Round(start_freq_phase_inc, MidpointRounding.AwayFromZero);//////////////////////////////
            str = String.Empty;
            str += "t_double: ";
            str += t_double.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            int start_freq_phase_inc_t = (int)t_double;
            str = String.Empty;
            str += "start_freq_phase_inc_t: ";
            str += start_freq_phase_inc_t.ToString("D");
            str += "\n";
            richTextBox0.AppendText(str);
            actual_start_freq = start_freq_phase_inc_t * sample_rate / MULT;   // desired freq = PhaseInc * sample rate / 2^32
            str = String.Empty;
            str += "actual_start_freq: ";
            str += actual_start_freq.ToString("G6");
            str += "\n\n";
            richTextBox0.AppendText(str);

            if (wave_type == true)
            {
                double end_freq_phase_inc = MULT * (end_freq / sample_rate);  // PhaseInc = 2^32 * (desired freq / sample rate)
                Console.WriteLine("end_freq_phase_inc: {0} ", end_freq_phase_inc);
                str = String.Empty;
                str += "end_freq_phase_inc: ";
                str += end_freq_phase_inc.ToString("F");
                str += "\n";
                richTextBox0.AppendText(str);
                int end_freq_phase_inc_t = (int)Math.Round(end_freq_phase_inc);//////////////////////////////////////
                str = String.Empty;
                str += "end_freq_phase_inc_t: ";
                str += end_freq_phase_inc_t.ToString("D");
                str += "\n";
                richTextBox0.AppendText(str);
                actual_end_freq = end_freq_phase_inc_t * sample_rate / MULT;   // desired freq = PhaseInc * sample rate / 2^32

                double step_size = (double)(end_freq_phase_inc - start_freq_phase_inc) / num_samples; // step_size = start_phase_inc - end_phase_inc / num_samples
                Console.WriteLine("step_size: {0} ", step_size);
                str = String.Empty;
                str += "step_size: ";
                str += step_size.ToString("F");
                str += "\n";
                richTextBox0.AppendText(str);
                step_size_t = (int)Math.Round(step_size);///////////////////////////
            }
            else
            {
                str = String.Empty;
                str += "*****CW******: ";
                str += "\n";
                richTextBox0.AppendText(str);
                step_size_t = 0;
                actual_end_freq = actual_start_freq;
            }

            double idle_samples_d = idle_samples / (1 / sample_rate);
            uint idle_samples_t = (uint)Math.Round(idle_samples_d);
            actual_idle_samples = idle_samples_t * (1 / sample_rate);
            str = String.Empty;
            str += "actual_idle_samples: ";
            str += actual_idle_samples.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            str = String.Empty;
            str += "step_size_t: ";
            str += step_size_t.ToString("D");
            str += "\n";
            richTextBox0.AppendText(str);

            str = String.Empty;
            str += "actual_end_freq: ";
            str += actual_end_freq.ToString("F");
            str += "\n";
            richTextBox0.AppendText(str);

            str = String.Empty;
            str += "--------------------------";
            str += "\n";
            richTextBox0.AppendText(str);

            str = String.Empty;
            str += "phase_inc_dwell: ";
            str += phase_inc_dwell.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x83000014", "0x" + phase_inc_dwell.ToString("X8"));
                dds_reg14.Text = phase_inc_dwell.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x83000034", "0x" + phase_inc_dwell.ToString("X8"));
                dds_reg34.Text = phase_inc_dwell.ToString("X8");
            }
            else if (chan == 3)
            {
                ssh_WriteReg("0x83000054", "0x" + phase_inc_dwell.ToString("X8"));
                dds_reg54.Text = phase_inc_dwell.ToString("X8");
            }
            else if (chan == 4)
            {
                ssh_WriteReg("0x83000074", "0x" + phase_inc_dwell.ToString("X8"));
                dds_reg74.Text = phase_inc_dwell.ToString("X8");
            }


            str = String.Empty;
            str += "step_size_t: ";
            str += step_size_t.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x83000018", "0x" + step_size_t.ToString("X8"));
                dds_reg18.Text = step_size_t.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x83000038", "0x" + step_size_t.ToString("X8"));
                dds_reg38.Text = step_size_t.ToString("X8");
            }
            else if (chan == 3)
            {
                ssh_WriteReg("0x83000058", "0x" + step_size_t.ToString("X8"));
                dds_reg58.Text = step_size_t.ToString("X8");
            }
            else if (chan == 4)
            {
                ssh_WriteReg("0x83000078", "0x" + step_size_t.ToString("X8"));
                dds_reg78.Text = step_size_t.ToString("X8");
            }


            str = String.Empty;
            str += "idle_samples_t: ";
            str += idle_samples_t.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x8300001C", "0x" + idle_samples_t.ToString("X8"));
                dds_reg1C.Text = idle_samples_t.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x8300003C", "0x" + idle_samples_t.ToString("X8"));
                dds_reg3C.Text = idle_samples_t.ToString("X8");
            }
            else if (chan == 3)
            {
                ssh_WriteReg("0x8300005C", "0x" + idle_samples_t.ToString("X8"));
                dds_reg5C.Text = idle_samples_t.ToString("X8");
            }
            else if (chan == 4)
            {
                ssh_WriteReg("0x8300007C", "0x" + idle_samples_t.ToString("X8"));
                dds_reg7C.Text = idle_samples_t.ToString("X8");
            }


            str = String.Empty;
            str += "num_samples_t: ";
            str += num_samples_t.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x83000020", "0x" + num_samples_t.ToString("X8"));
                dds_reg20.Text = num_samples_t.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x83000040", "0x" + num_samples_t.ToString("X8"));
                dds_reg40.Text = num_samples_t.ToString("X8");
            }
            else if (chan == 3)
            {
                ssh_WriteReg("0x83000060", "0x" + num_samples_t.ToString("X8"));
                dds_reg60.Text = num_samples_t.ToString("X8");
            }
            else if (chan == 4)
            {
                ssh_WriteReg("0x83000080", "0x" + num_samples_t.ToString("X8"));
                dds_reg80.Text = num_samples_t.ToString("X8");
            }

            str = String.Empty;
            str += "start_freq_phase_inc_t: ";
            str += start_freq_phase_inc_t.ToString("X8");
            str += "\n";
            richTextBox0.AppendText(str);
            if (chan == 1)
            {
                ssh_WriteReg("0x83000024", "0x" + start_freq_phase_inc_t.ToString("X8"));
                dds_reg24.Text = start_freq_phase_inc_t.ToString("X8");
            }
            else if (chan == 2)
            {
                ssh_WriteReg("0x83000044", "0x" + start_freq_phase_inc_t.ToString("X8"));
                dds_reg44.Text = start_freq_phase_inc_t.ToString("X8");
            }
            else if (chan == 3)
            {
                ssh_WriteReg("0x83000064", "0x" + start_freq_phase_inc_t.ToString("X8"));
                dds_reg64.Text = start_freq_phase_inc_t.ToString("X8");
            }
            else if (chan == 4)
            {
                ssh_WriteReg("0x83000084", "0x" + start_freq_phase_inc_t.ToString("X8"));
                dds_reg84.Text = start_freq_phase_inc_t.ToString("X8");
            }




        }

        private void duration_mode_Click(object sender, EventArgs e)
        {
            string str = String.Empty;

            if (duration_mode.Checked == true)
            {
                int duration_ms_cnt_int = (Convert.ToInt32(duration_ms_cnt.Text, 10));
                SendWord_Decimal("G", duration_ms_cnt_int);
                Thread.Sleep(500);

                str += "M3";
                str += "\n";
            }
            else
            {
                str += "M2";
                str += "\n";
            }


        }


        string ssh_ReadReg(string addr)
        {
            SshCommand cmd;
            string str = String.Empty;
            string str1 = String.Empty;
            if (true == ssh_client.IsConnected)
            {
                str1 = "devmem " + addr;
                Console.WriteLine("ssh_ReadReg(): Command: " + str1);
                //               Console.WriteLine(str1); 


                cmd = ssh_client.RunCommand(str1);
                str = cmd.Result;
                Console.WriteLine("ssh_ReadReg(): Response:" + str);
                //                Console.WriteLine(str);
                return str.Substring(2, 8);
            }
            return "FFFFFFFF";
        }

        void ssh_WriteReg(string addr, string val)
        {
            SshCommand cmd;
            string str = String.Empty;

            if (true == ssh_client.IsConnected)
            {
                str = "devmem " + addr + " 32 " + val;
                Console.WriteLine("ssh_WriteReg(): Command:" + str);
                //         Console.WriteLine(str); // Get the command output

                cmd = ssh_client.RunCommand(str);// Execute a command

                Console.WriteLine("ssh_WriteReg(): Response:" + cmd.Result);
                //               Console.WriteLine(cmd.Result); // Get the command output
            }
        }


        private void ssh_connect_Click(object sender, EventArgs e)
        {
 //           if (false == ssh_client.IsConnected)
//            {
                try
                {

                    ssh_connect.Text = "Connecting...";
                    ssh_client = new SshClient(ipAddress.Text, "root", "analog");
                    ssh_client.Connect();
                    Console.WriteLine($"Connected to {ssh_client.ConnectionInfo.Host} successfully.");

                    ssh_connect.BackColor = Color.LightGreen;
                //                  SshCommand cmd = ssh_client.RunCommand("devmem 0x80000000"); // Execute a command
                //                  Console.WriteLine("Command Output:");
                //                   Console.WriteLine(cmd.Result); // Get the command output
                //                  reg_0x00.Text = cmd.Result;
                //                    read_all_regs.PerformClick();
                //                    cmac_read_all_regs.PerformClick();

                read_all_regs.PerformClick();
//                    reg00.Text = ssh_ReadReg("0x80000000");
//                   cmac_0_reg_0x124.Text = ssh_ReadReg("0x81010024");
//                   cmac_4_reg_0x124.Text = ssh_ReadReg("0x82010024");
                ssh_connect.Text = "Connected";

                    //                ssh_client.Disconnect();
                    //                Thread oThread = new Thread(new ThreadStart(backgroundTask));
                    //                oThread.IsBackground = true;
                    //                oThread.Start();

                }
                catch (Exception ex)
                {
                    Console.WriteLine($"An error occurred: {ex.Message}");
                    ssh_connect.Text = "Connect";
                    ssh_connect.BackColor = Color.Red;
                }
 //           }
 //           else
 //           {
//
//            }
        }

        private void reg_read_Click(object sender, EventArgs e)
        {
            reg_data.Text = ssh_ReadReg("0x" + reg_addr.Text);
        }

        private void reg_write_Click(object sender, EventArgs e)
        {
            ssh_WriteReg("0x" + reg_addr.Text, "0x" + reg_data.Text);
        }

        private void dac_data_src_quadsend_rcv_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FCFF;   // bit9:8 = 00 <= quadsendrecv

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void dac_data_src_dds_pulse_gen_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FCFF;   // 

            cntrl_reg28 |= 0x300;   // bit9:8 = 1 <= QSFP

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void dac_data_src_adc_chan0_loopback_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FCFF;   // 

            cntrl_reg28 |= 0x200;   // bit9:8 = 1 <= DDS

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void dac_data_src_fiber_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FCFF;   // 

            cntrl_reg28 |= 0x100;   // bit9:8 = 1 <= fiber

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void RegDump()
        {
            if (true == ssh_client.IsConnected)
            {
                string str;

                reg00.Text = "";
                reg04.Text = "";
                reg14.Text = "";
                reg20.Text = "";
                reg28.Text = "";
                reg44.Text = "";
                reg2C.Text = "";
                reg4C.Text = "";
                reg34.Text = "";
                reg3C.Text = "";
                reg40.Text = "";
                regE8.Text = "";
                regDC.Text = "";
                regD4.Text = "";
                regE4.Text = "";
                regF4.Text = "";
                regE0.Text = "";
                regF0.Text = "";

                reg00.Text = ssh_ReadReg("0x80000000");
                //
                str = ssh_ReadReg("0x80000004");
                uint val = (Convert.ToUInt32(str, 16));
                reg04.Text = str;
                //
                if (0x1 == (val & 0x01))
                {
                    qsfp1_intfc_rdy.BackColor = Color.LightGreen;
                }
                else
                {
                    qsfp1_intfc_rdy.BackColor = Color.Red;
                }
                if (0x2 == (val & 0x02))
                {
                    qsfp1_present.BackColor = Color.Red;
                }
                else
                {
                    qsfp1_present.BackColor = Color.LightGreen;
                }
                if (0x4 == (val & 0x04))
                {
                    qsfp1_intl.BackColor = Color.Red;
                }
                else
                {
                    qsfp1_intl.BackColor = Color.LightGreen;
                }


                //
                if (0x10 == (val & 0x010))
                {
                    qsfp2_intfc_rdy.BackColor = Color.LightGreen;
                }
                else
                {
                    qsfp2_intfc_rdy.BackColor = Color.Red;
                }
                if (0x20 == (val & 0x020))
                {
                    qsfp2_present.BackColor = Color.Red;
                }
                else
                {
                    qsfp2_present.BackColor = Color.LightGreen;
                }

                if (0x40 == (val & 0x040))
                {
                    qsfp2_intl.BackColor = Color.Red;
                }
                else
                {
                    qsfp2_intl.BackColor = Color.LightGreen;
                }


                //
                if (0x100 == (val & 0x0100))
                {
                    qsfp3_intfc_rdy.BackColor = Color.LightGreen;
                }
                else
                {
                    qsfp3_intfc_rdy.BackColor = Color.Red;
                }
                if (0x200 == (val & 0x0200))
                {
                    qsfp3_present.BackColor = Color.Red;
                }
                else
                {
                    qsfp3_present.BackColor = Color.LightGreen;
                }
                if (0x400 == (val & 0x0400))
                {
                    qsfp3_intl.BackColor = Color.Red;
                }
                else
                {
                    qsfp3_intl.BackColor = Color.LightGreen;
                }


                //
                if (0x1000 == (val & 0x01000))
                {
                    qsfp4_intfc_rdy.BackColor = Color.LightGreen;
                }
                else
                {
                    qsfp4_intfc_rdy.BackColor = Color.Red;
                }
                if (0x2000 == (val & 0x02000))
                {
                    qsfp4_present.BackColor = Color.Red;
                }
                else
                {
                    qsfp4_present.BackColor = Color.LightGreen;
                }
                if (0x4000 == (val & 0x04000))
                {
                    qsfp4_intl.BackColor = Color.Red;
                }
                else
                {
                    qsfp4_intl.BackColor = Color.LightGreen;
                }



                reg14.Text = ssh_ReadReg("0x80000014");
                reg20.Text = ssh_ReadReg("0x80000020");

                str = ssh_ReadReg("0x80000028");
                val = (Convert.ToUInt32(str, 16));
                reg28.Text = str;

                if (0 == (val & 0x300)) // bit9:8 dac_src_data_sel
                {
                    dac_data_src_quadsend_rcv.Checked = true;
                }
                else if (0x100 == (val & 0x300))
                {
                    dac_data_src_fiber.Checked = true;
                }
                else if (0x200 == (val & 0x300))
                {
                    dac_data_src_adc_chan0_loopback.Checked = true;
                }
                else if (0x300 == (val & 0x300))
                {
                    dac_data_src_dds_pulse_gen.Checked = true;
                }

                if (0 == (val & 0x8000_0000)) // bit31 qsfp4_playback_path_data_enable_n
                {
                    tx_fiber_data_on.Checked = true;
                }
                else if (0x8000_0000 == (val & 0x8000_0000))
                {
                    tx_fiber_data_off.Checked = true;
                }

                if (0 == (val & 0x0000_8000)) // bit15 qsfp1_capture_en_n
                {
                    rx_fiber_data_on.Checked = true;
                }
                else if (0x0000_8000 == (val & 0x0000_8000))
                {
                    rx_fiber_data_off.Checked = true;
                }

                if (0 == (val & 0x0000_0001)) // bit0 fiber_tx_src_data_sel
                {
                    tx_fiber_adcs_src.Checked = true;
                }
                else if (0x0000_0001 == (val & 0x0000_0001))
                {
                    tx_fiber_dds_src.Checked = true;
                }

                if (0 == (val & 0x0000_1000))  // bit12 chan1to4_mode_sel
                {
                    adc_llopback_mode_chan_all.Checked = true;
                }
                else if (0x0000_1000 == (val & 0x0000_1000))
                {
                    adc_llopback_mode_chan_0.Checked = true;
                }
                

                //clk125
                str = ssh_ReadReg("0x80000044");
                val = (Convert.ToUInt32(str, 16));
                reg44.Text = val.ToString("D");

                //      qsfp1         
                str = ssh_ReadReg("0x8000002C");
                val = (Convert.ToUInt32(str, 16));
                reg2C.Text = val.ToString("D");

                //clk250
                str = ssh_ReadReg("0x8000004C");
                val = (Convert.ToUInt32(str, 16));
                reg4C.Text = val.ToString("D");

                //qsfp4
                str = ssh_ReadReg("0x80000034");
                val = (Convert.ToUInt32(str, 16));
                reg34.Text = val.ToString("D");

                //jesd_rx
                str = ssh_ReadReg("0x8000003C");
                val = (Convert.ToUInt32(str, 16));
                reg3C.Text = val.ToString("D");

                //jesd_tx
                str = ssh_ReadReg("0x80000040");
                val = (Convert.ToUInt32(str, 16));
                reg40.Text = val.ToString("D");


                regE8.Text = ssh_ReadReg("0x800000E8");
                regDC.Text = ssh_ReadReg("0x800000DC");
                regD4.Text = ssh_ReadReg("0x800000D4");
                regE4.Text = ssh_ReadReg("0x800000E4");
                regF4.Text = ssh_ReadReg("0x800000F4");
                regE0.Text = ssh_ReadReg("0x800000E0");
                regF0.Text = ssh_ReadReg("0x800000F0");

                dds_reg_dump();


            }
        }

        private void dds_reg_dump()
        {
            dds_reg00.Text = ssh_ReadReg("0x83000000");
            dds_reg04.Text = ssh_ReadReg("0x83000004");
            dds_reg08.Text = ssh_ReadReg("0x83000008");
            dds_reg0C.Text = ssh_ReadReg("0x8300000C");

            string str = ssh_ReadReg("0x83000004");
            uint val = (Convert.ToUInt32(str, 16));
            dds_reg04.Text = str;

            if (1 == (val & 0x1))
            {
                dac_off.PerformClick();
            }
            else
            {
                dac_on.PerformClick();
            }

            dds_reg90.Text = ssh_ReadReg("0x83000090");
            dds_reg94.Text = ssh_ReadReg("0x83000094");
            dds_reg98.Text = ssh_ReadReg("0x83000098");
            dds_reg9C.Text = ssh_ReadReg("0x8300009C");
        }

        private void read_all_regs_Click(object sender, EventArgs e)
        {
            read_all_regs.Text = "Refreshing...";
            read_all_regs.BackColor = Color.LightGreen;
            RegDump();
            read_all_regs.Text = "Refresh";
            read_all_regs.BackColor = Color.Gainsboro;
        }

        private void dds_cnt_reset_Click(object sender, EventArgs e)
        {
            ssh_WriteReg("0x8300000C", "0x00000001"); //bit 0
            ssh_WriteReg("0x8300000C", "0x00000000");
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            dds_reg_dump();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ssh_WriteReg("0x80000020", "0x80000000");  // set bit31
            ssh_WriteReg("0x80000020", "0x00000000");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            regE8.Text = ssh_ReadReg("0x800000E8");
            regDC.Text = ssh_ReadReg("0x800000DC");
            regD4.Text = ssh_ReadReg("0x800000D4");
            regE4.Text = ssh_ReadReg("0x800000E4");
            regF4.Text = ssh_ReadReg("0x800000F4");
            regE0.Text = ssh_ReadReg("0x800000E0");
            regF0.Text = ssh_ReadReg("0x800000F0");

            dds_reg90.Text = ssh_ReadReg("0x83000090");
            dds_reg94.Text = ssh_ReadReg("0x83000094");

        }

        private void read_dds_regs_Click(object sender, EventArgs e)
        {
            dds_reg10.Text = ssh_ReadReg("0x83000010");
            dds_reg14.Text = ssh_ReadReg("0x83000014");
            dds_reg18.Text = ssh_ReadReg("0x83000018");
            dds_reg1C.Text = ssh_ReadReg("0x8300001C");
            dds_reg20.Text = ssh_ReadReg("0x83000020");
            dds_reg24.Text = ssh_ReadReg("0x83000024");
            dds_reg28.Text = ssh_ReadReg("0x83000028");
            dds_reg2C.Text = ssh_ReadReg("0x8300002C");

            dds_reg30.Text = ssh_ReadReg("0x83000030");
            dds_reg34.Text = ssh_ReadReg("0x83000034");
            dds_reg38.Text = ssh_ReadReg("0x83000038");
            dds_reg3C.Text = ssh_ReadReg("0x8300003C");
            dds_reg40.Text = ssh_ReadReg("0x83000040");
            dds_reg44.Text = ssh_ReadReg("0x83000044");
            dds_reg48.Text = ssh_ReadReg("0x83000048");
            dds_reg4C.Text = ssh_ReadReg("0x8300004C");

            dds_reg50.Text = ssh_ReadReg("0x83000050");
            dds_reg54.Text = ssh_ReadReg("0x83000054");
            dds_reg58.Text = ssh_ReadReg("0x83000058");
            dds_reg5C.Text = ssh_ReadReg("0x8300005C");
            dds_reg60.Text = ssh_ReadReg("0x83000060");
            dds_reg64.Text = ssh_ReadReg("0x83000064");
            dds_reg68.Text = ssh_ReadReg("0x83000068");
            dds_reg6C.Text = ssh_ReadReg("0x8300006C");

            dds_reg70.Text = ssh_ReadReg("0x83000070");
            dds_reg74.Text = ssh_ReadReg("0x83000074");
            dds_reg78.Text = ssh_ReadReg("0x83000078");
            dds_reg7C.Text = ssh_ReadReg("0x8300007C");
            dds_reg80.Text = ssh_ReadReg("0x83000080");
            dds_reg84.Text = ssh_ReadReg("0x83000084");
            dds_reg88.Text = ssh_ReadReg("0x83000088");
            dds_reg8C.Text = ssh_ReadReg("0x8300008C");

        }

        private void button4_Click(object sender, EventArgs e)
        {
            dds_send_w1.Text = "Sending...";
            dds_send_w1.BackColor = Color.LightGreen;


            ssh_WriteReg("0x83000000", "0x00000001"); //bit 0  self clearing bit
            dds_send_w1.Text = "Send";
            dds_send_w1.BackColor = Color.Gainsboro;
        }

        private void dds_reset_Click(object sender, EventArgs e)
        {
            ssh_WriteReg("0x8300000C", "0"); //bit 31
            Thread.Sleep(500);
            ssh_WriteReg("0x8300000C", "0x80000000"); //bit 31
        }








        private void send_button_w2_Click(object sender, EventArgs e)
        {
            send_button_w2.Text = "Loading...";
            send_button_w2.BackColor = Color.LightGreen;

            string str = String.Empty;
            double actual_start_freq;
            double actual_end_freq;
            double actual_duration;
            double actual_idle_samples;

            str += "************************";
            str += "\n";
            richTextBox0.AppendText(str);

            double start_freq = (double)(Convert.ToDouble(start_freq_w2.Text)) * 1e6;
            double end_freq = (double)(Convert.ToDouble(end_freq_w2.Text)) * 1e6;
            double duration = (double)(Convert.ToDouble(duration_w2.Text)) * 1e-6;
            double idle_samples = (Convert.ToDouble(idle_samples_w2.Text)) * 1e-6;
            uint phase_inc_dwell = (Convert.ToUInt32(phase_inc_dwell_w2.Text, 10));

            ComputeSend_PDW(2, chirp_w2.Checked, start_freq, end_freq, duration, idle_samples, phase_inc_dwell, out actual_start_freq, out actual_end_freq, out actual_duration, out actual_idle_samples);
            actual_start_freq_w2.Text = actual_start_freq.ToString("F");
            actual_end_freq_w2.Text = actual_end_freq.ToString("F");
            actual_duration_w2.Text = actual_duration.ToString("G6");
            actual_idle_samples_w2.Text = actual_idle_samples.ToString("G6");

            double scale_factor = (double)scale_factor_w2.Value;
            Set_ScaleFactor(2, scale_factor, iq_swap_w2.Checked);

            ssh_WriteReg("0x83000048", "00000000");// phase_off_2
            dds_reg48.Text = "0x00000000";

            send_button_w2.Text = "Load";
            send_button_w2.BackColor = Color.Gainsboro;
            dds_send_w2.BackColor = Color.Yellow;
        }

        private void send_button_w3_Click(object sender, EventArgs e)
        {
            send_button_w3.Text = "Loading...";
            send_button_w3.BackColor = Color.LightGreen;

            string str = String.Empty;
            double actual_start_freq;
            double actual_end_freq;
            double actual_duration;
            double actual_idle_samples;

            str += "************************";
            str += "\n";
            richTextBox0.AppendText(str);

            double start_freq = (double)(Convert.ToDouble(start_freq_w3.Text)) * 1e6;
            double end_freq = (double)(Convert.ToDouble(end_freq_w3.Text)) * 1e6;
            double duration = (double)(Convert.ToDouble(duration_w3.Text)) * 1e-6;
            double idle_samples = (Convert.ToDouble(idle_samples_w3.Text)) * 1e-6;
            uint phase_inc_dwell = (Convert.ToUInt32(phase_inc_dwell_w3.Text, 10));

            ComputeSend_PDW(3, chirp_w3.Checked, start_freq, end_freq, duration, idle_samples, phase_inc_dwell, out actual_start_freq, out actual_end_freq, out actual_duration, out actual_idle_samples);
            actual_start_freq_w3.Text = actual_start_freq.ToString("F");
            actual_end_freq_w3.Text = actual_end_freq.ToString("F");
            actual_duration_w3.Text = actual_duration.ToString("G6");
            actual_idle_samples_w3.Text = actual_idle_samples.ToString("G6");

            double scale_factor = (double)scale_factor_w3.Value;
            Set_ScaleFactor(3, scale_factor, iq_swap_w3.Checked);

            ssh_WriteReg("0x83000068", "00000000");// phase_off_3
            dds_reg68.Text = "0x00000000";

            send_button_w3.Text = "Load";
            send_button_w3.BackColor = Color.Gainsboro;
            dds_send_w3.BackColor = Color.Yellow;
        }

        private void send_button_w4_Click(object sender, EventArgs e)
        {
            send_button_w4.Text = "Loading...";
            send_button_w4.BackColor = Color.LightGreen;

            string str = String.Empty;
            double actual_start_freq;
            double actual_end_freq;
            double actual_duration;
            double actual_idle_samples;

            str += "************************";
            str += "\n";
            richTextBox0.AppendText(str);

            double start_freq = (double)(Convert.ToDouble(start_freq_w4.Text)) * 1e6;
            double end_freq = (double)(Convert.ToDouble(end_freq_w4.Text)) * 1e6;
            double duration = (double)(Convert.ToDouble(duration_w4.Text)) * 1e-6;
            double idle_samples = (Convert.ToDouble(idle_samples_w4.Text)) * 1e-6;
            uint phase_inc_dwell = (Convert.ToUInt32(phase_inc_dwell_w4.Text, 10));

            ComputeSend_PDW(4, chirp_w4.Checked, start_freq, end_freq, duration, idle_samples, phase_inc_dwell, out actual_start_freq, out actual_end_freq, out actual_duration, out actual_idle_samples);
            actual_start_freq_w4.Text = actual_start_freq.ToString("F");
            actual_end_freq_w4.Text = actual_end_freq.ToString("F");
            actual_duration_w4.Text = actual_duration.ToString("G6");
            actual_idle_samples_w4.Text = actual_idle_samples.ToString("G6");

            double scale_factor = (double)scale_factor_w4.Value;
            Set_ScaleFactor(4, scale_factor, iq_swap_w4.Checked);

            ssh_WriteReg("0x83000088", "00000000");// phase_off_4
            dds_reg88.Text = "0x00000000";

            send_button_w4.Text = "Load";
            send_button_w4.BackColor = Color.Gainsboro;
            dds_send_w4.BackColor = Color.Yellow;
        }

        private void dds_send_w2_Click(object sender, EventArgs e)
        {
            dds_send_w2.Text = "Sending...";
            dds_send_w2.BackColor = Color.LightGreen;


            ssh_WriteReg("0x83000000", "0x00000010"); //bit 4 self clearing bit
            dds_send_w2.Text = "Send";
            dds_send_w2.BackColor = Color.Gainsboro;
        }

        private void dds_send_w3_Click(object sender, EventArgs e)
        {
            dds_send_w3.Text = "Sending...";
            dds_send_w3.BackColor = Color.LightGreen;


            ssh_WriteReg("0x83000000", "0x00000100"); //bit 8 self clearing bit
            dds_send_w3.Text = "Send";
            dds_send_w3.BackColor = Color.Gainsboro;
        }

        private void dds_send_w4_Click(object sender, EventArgs e)
        {
            dds_send_w4.Text = "Sending...";
            dds_send_w4.BackColor = Color.LightGreen;


            ssh_WriteReg("0x83000000", "0x00001000"); //bit 12 self clearing bit
            dds_send_w4.Text = "Send";
            dds_send_w4.BackColor = Color.Gainsboro;
        }

        private void tx_fiber_adcs_src_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 &= 0xFFFF_FFFE;   // bit0  0=ADCs, 1=DDS

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void tx_fiber_dds_src_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 |= 0x1;   // bit0  0=ADCs, 1=DDS

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void tx_fiber_data_on_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 &= 0x7FFF_FFFF;   // bit31  0=On, 1=Off

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void tx_fiber_data_off_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 |= 0x8000_0000;   // bit31  0=On, 1=Off

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void rx_fiber_data_on_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 &= 0xFFFF_7FEF;   // bit15  0=On   bit4  0=normal

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void rx_fiber_data_off_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 |= 0x0000_8010;   // bit15  1=Off  bit4  1=reset FIFOs

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void qsfp4_reg_refresh_Click(object sender, EventArgs e)
        {
            reg28_1.Text = "";
            regE8_1.Text = "";
            regD0_1.Text = "";
            regEC_1.Text = "";
            regDC_1.Text = "";
            regD4_1.Text = "";
            regCC_1.Text = "";

            reg28_1.Text = ssh_ReadReg("0x80000028");
            regE8_1.Text = ssh_ReadReg("0x800000E8");
            regD0_1.Text = ssh_ReadReg("0x800000D0");
            regEC_1.Text = ssh_ReadReg("0x800000EC");
            regDC_1.Text = ssh_ReadReg("0x800000DC");
            regD4_1.Text = ssh_ReadReg("0x800000D4");
            regCC_1.Text = ssh_ReadReg("0x800000CC");

        }

        private void qsfp1_reg_refresh_Click(object sender, EventArgs e)
        {
            regE4_1.Text = "";
            regF4_1.Text = "";
            regD8_1.Text = "";
            regE0_1.Text = "";
            regF8_1.Text = "";
            regF0_1.Text = "";
            regFC_1.Text = "";
            regC8_1.Text = "";
            regC4_1.Text = "";

            regE4_1.Text = ssh_ReadReg("0x800000E4");
            regF4_1.Text = ssh_ReadReg("0x800000F4");
            regD8_1.Text = ssh_ReadReg("0x800000D8");
            regE0_1.Text = ssh_ReadReg("0x800000E0");
            regF8_1.Text = ssh_ReadReg("0x800000F8");
            regF0_1.Text = ssh_ReadReg("0x800000F0");
            regFC_1.Text = ssh_ReadReg("0x800000FC");
            regC8_1.Text = ssh_ReadReg("0x800000C8");
            regC4_1.Text = ssh_ReadReg("0x800000C4");
        }

        private void adc_llopback_mode_chan_0_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 |= 0x0000_1000;   // bit12   1=chan 0

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void adc_llopback_mode_chan_all_Click(object sender, EventArgs e)
        {
            reg28.Text = ssh_ReadReg("0x80000028");
            uint cntrl_reg28 = (Convert.ToUInt32(reg28.Text, 16));
            cntrl_reg28 &= 0xFFFF_EFFF;   // bit12   0=All channels

            ssh_WriteReg("0x80000028", "0x" + cntrl_reg28.ToString("X8"));
            reg28.Text = ssh_ReadReg("0x80000028");
        }

        private void reg28_TextChanged(object sender, EventArgs e)
        {
            reg28.BackColor = Color.Red;
            Thread.Sleep(500);
            reg28.BackColor = Color.White;
        }
    }
    }
