using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace CSHARP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int len = 0;
            int[] addr = new int[127]; ;
            int res = DDSCon.GetItemNum(ref addr[0], ref len);

            if (res == 0)
            {



                DDSCon.SetCON(0x00, 0);           //Constant  waveform,Timing Trigger,Fall Trigger,FRQ_TEST on,COUT_NOT LATCH
                DDSCon.SetRelay(0x80, 0);               //frequency DC coupling
                DDSCon.SetRelay(0x04, 0);                //output attenuate 20dB
                DDSCon.SetRelay(0x10, 0);               //normal scope
                DDSCon.SetRelay(0x01, 0);                //Filter cut-off frequency less than 5M
                DDSCon.SetFinSource(0x01, 0);            //Select low frequency input
                DDSCon.SetFir(0x04, 0);                  //Set output filter cut-off frequency  10KHz
                label2.Text = len.ToString();

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            uint temp = 0;
            uint dat = 256;
            DDSCon.Write24C01(0, dat, 0);
            Thread.Sleep(100);
            DDSCon.Read24C01(0, ref temp, 0);
            if (temp == dat)
            {
                MessageBox.Show("Read/Write OK!");
            }
            else
            {
                MessageBox.Show("Read/Write Error!");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DDSCon.SetRelay(0x08, 0);                                             //open DDS
        }

        private void button2_Click(object sender, EventArgs e)
        {
            uint Temp;
            byte[] Buf = new byte[2000];
            uint te;
            //Temp = 1000;
            double Freq = 1000;                      //Frequency=1KHz
            uint WavePointNumber = 1000;               //output sample Point Number=100
            if (Freq * 1000 > 50000000)
            {
                WavePointNumber = (uint)((double)(50000000) / Freq);
                te = 2147483647;
            }
            else
            {
                te = (uint)(Freq * WavePointNumber / 0.023283);
            }
            
            double m_EY = 1.0f;					//Offset
            double m_EA = 4.0f;				//Amplitude
            double fun = 0;
            DDSCon.SetAD9850(te, 0);              //Set frequency of output sample point
            for (byte i = 0; i < WavePointNumber; i++)                  //calculate sine wave                
            {
                fun = (m_EY * 1.0 / 10) + m_EA / 10 * Math.Sin(i * 1.0 / WavePointNumber * 2 * 3.14159265);
                Temp = (uint)(8192 - 8192 * fun);
                if (Temp > 16383)
                    Temp = 16383;
                else if (Temp < 0)
                    Temp = 0;
                Buf[2 * i] = (byte)(Temp % 256);
                Buf[2 * i + 1] = (byte)(Temp / 256);
            }
            DDSCon.SetEndAddr(WavePointNumber - 1, 0);                         //Set address counter's last address
            DDSCon.SendDataPackage64(ref Buf[0], WavePointNumber * 2, 0);      //Transfers Wave form data to the signal fountain
            DDSCon.FrameNegtivePulse(0);                     //Clear the address counter and start
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DDSCon.StartFrqMeasure(0);                                          //Reset counter and startup frequency one time test
            Thread.Sleep(1100);                                                 //Sleep more than 1 second
            textBoxFrq.Text = DDSCon.GetFrqMeasureResult(0).ToString() + "Hz";          //Read the value of the counter
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }
    }

    public class DDSCon  //DDS Dll
    {

        [DllImport("DDSCON.dll", EntryPoint = "SetCON", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool SetCON(byte d, int index);                          //Control memory setting

        [DllImport("DDSCON", EntryPoint = "DigitalInOut", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool DigitalInOut(byte InVal, ref byte OutVal, int index);//8 bits digital I/O control

        [DllImport("DDSCON", EntryPoint = "SetAD9850", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool SetAD9850(uint Frq, int index);                    //Set frequency of output sample point

        [DllImport("DDSCON", EntryPoint = "SetRelay", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool SetRelay(byte RelVal, int index);                   //Set relay state

        [DllImport("DDSCON", EntryPoint = "SetFinSource", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool SetFinSource(byte SVal, int index);                 //Select frequency counter input

        [DllImport("DDSCON", EntryPoint = "StartFrqMeasure", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool StartFrqMeasure(int index);                       //Reset counter and startup frequency one time test

        [DllImport("DDSCON", EntryPoint = "SetEndAddr", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool SetEndAddr(uint AddrVal, int index);               //Set address counter's last address

        [DllImport("DDSCON", EntryPoint = "SetFir", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool SetFir(byte FirVal, int index);                     // Set output filter cut-off frequency

        [DllImport("DDSCON", EntryPoint = "FrameNegtivePulse", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool FrameNegtivePulse(int index);                     //Clear the address counter and start

        [DllImport("DDSCON", EntryPoint = "GetFrqMeasureResult", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern ulong GetFrqMeasureResult(int index);                  //Read the value of the counter

        [DllImport("DDSCON", EntryPoint = "SendDataPackage64", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool SendDataPackage64(ref byte buf, uint n, int index);  //Transfers Wave form data to the signal fountain

        [DllImport("DDSCON", EntryPoint = "GetItemNum", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetItemNum(ref int addr, ref int len);  //Initlize USB Device

        [DllImport("DDSCON", EntryPoint = "Write24C01", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Write24C01(byte addr, uint dat, int index);  //Write Address

        [DllImport("DDSCON", EntryPoint = "Read24C01", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Read24C01(byte addr, ref uint dat, int index);  //Read Address
    }
}
