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 easyUSBHidNetClass;       //easyUSBHIDNetClass.dll 

namespace USBHID_Projetc
{
    public partial class Form1 : Form
    {
        byte[] DataWrite = new byte[64];      //Buffer USB write 
        byte[] DataRead = new byte[64];       //Buffer USB Read  

       
    /****************************************************************************
          Function:
            public Form1()

          Summary:
            The main contructor for the Form1 class.

          Description:
            The main contructor for the Form1 class.  This function creates and
            initializes all of the form objects.
         
          Precondition:
            None

          Parameters:
            None

          Return Values:
            None

          Remarks:
            None
          ***************************************************************************/
        public Form1()
        {
            InitializeComponent(); 
            easyUSBHidNetClass1.SerialNumber = "DEMO VERSION"; //Put your USBHIDNetClass 'license code here
            //if you don't have, you can to buy for a few U.S.dollars here: http://www.USBHIDNetClass.org (by PayPal).
        }

        //Event Form1_Activated 
        //Configuration of the VID and PID  - Use the format: 0xNUMBER where NUMBER is a 16-bit hexadecimal number.
        private void Form1_Activated(object sender, EventArgs e)
        {
            easyUSBHidNetClass1.DeviceUSB_Config_VID_PID(0x1234, 0x0001); //VID and PID
        }


        //This is necessary to register the this.Handle 
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            easyUSBHidNetClass1.RegisterHandle(Handle);
        }


        //This is a callback function that gets called when a Windows message is received by the form.
        //We will receive various different types of messages, but the ones we really want to use are the WM_DEVICECHANGE messages.
        protected override void WndProc(ref Message m)
        {
            easyUSBHidNetClass1.USBHidMensagens(ref m);
            base.WndProc(ref m);

        }


        //This function is called when your device is found
        private void easyUSBHidNetClass1_DeviceUSB_found(object sender, EventArgs e)
        {
            lblStatus.Text = "Device USB found";
        }


        //This function is called when you send data to especific device
        private void easyUSBHidNetClass1_DeviceUSB_dataSent(object sender, EventArgs e)
        {
            lblStatus.Text = "Data sent successfully!";
        }

        // This funtion is called when USB device was removed. 
        private void easyUSBHidNetClass1_DeviceUSB_removed(object sender, EventArgs e)
        {
            lblStatus.Text = "Device USB not found";
        }

        /****************************************************************************
          Function:
            private void microgeniosUsbHID_Port1_microgeniosDeviceUsbHID_ReceberDados(object sender, DataRecievedEventArgs args)

          Summary:
            This function prints any data received on the USB.

          Description:
            This function is called when the data is received on the USB.  This
            function attempts to write that data to the txtDataReceived textbox.  
         
          Precondition:
            None

          Parameters:
            object sender               - Sender of the event (this form)
            DataRecievedEventArgs args  - The event arguments

          Return Values:
            None

          Remarks:
            None
          ***************************************************************************/
        //this funtion is called when any data USB is received. The variable args received the Data
        private void easyUSBHidNetClass1_DeviceUSB_dataReceived(object sender, DataRecievedEventArgs args)
        {
            //InvokeRequired required compares the thread ID of the
            //  calling thread to the thread ID of the creating thread.
            //  If these threads are different, it returns true.  We can
            //  use this attribute to determine if we can append text
            //  directly to the textbox or if we must launch an a delegate
            //  function instance to write to the textbox.

            if (InvokeRequired)  
            {
                //InvokeRequired returned TRUE meaning that this function
                //  was called from a thread different than the current
                //  thread.  We must launch a deleage function.
                //  Invoke the new delegate sending the same Data to the
                //  delegate that was passed into this function from the
                //  other thread.

                Invoke(new DataRecievedEventHandler(easyUSBHidNetClass1_DeviceUSB_dataReceived), new object[] { sender, args });
         
            }
            else
            {
                //  If this function was called from the same thread that 
                //  holds the required objects then just add the text.
                ////***********************************
                //Example: 
                // args.data contains the value received on the USB.
                // Buffer PC        Buffer Microcontroller
                // args.data[0] <=> writebuff[0] 
                // args.data[1] <=> writebuff[1]
                // args.data[2] <=> writebuff[2]
                // ...
                // args.data[63] <=> writebuff[63]
                //************************************
                try
                {
                    //lblResult.Text = args.reportID.ToString();                
                    lblResult.Text = args.data[0].ToString(); 
                }
                catch { }
                

            }
        }

        //OPTIONAL FUNCTION 
        // This funtion is called when "ANY" USB HID device found. 
        private void easyUSBHidNetClass1_anyDeviceUSB_found(object sender, EventArgs e)
        {
            lblStatus.Text = "Any Device USB HID found";
        }


        //OPTIONAL FUNCTION 
        // This funtion is called when "ANY" USB HID device removed. 
        private void easyUSBHidNetClass1_anyDeviceUSB_removed(object sender, EventArgs e)
        {
            //InvokeRequired required compares the thread ID of the
            //  calling thread to the thread ID of the creating thread.
            //  If these threads are different, it returns true.  We can
            //  use this attribute to determine if we can append text
            //  directly to the label or if we must launch an a delegate
            //  function instance to write to the label.

            if (InvokeRequired)
            {
                //InvokeRequired returned TRUE meaning that this function
                //  was called from a thread different than the current
                //  thread.  We must launch a deleage function.
                //  Invoke the new delegate sending the same Data to the
                //  delegate that was passed into this function from the
                //  other thread.
                Invoke(new EventHandler(easyUSBHidNetClass1_anyDeviceUSB_removed), new object[] { sender, e });
            }
            else
            {
                lblStatus.Text = "Any Device USB HID not found";
            }

        }


      //******************APLICATION*********************************************

        //button 
        private void btnLED_D0_Click(object sender, EventArgs e)
        {
           
            DataWrite[63] ^= (byte)1;     //any byte Data (0 ~ 63) - toggle LED
            EnviarDadosUSB(DataWrite);    //Send to Microcontroller the USB Buffer 
  
        }


        // This function is responsible for sending USB data
        public void EnviarDadosUSB(byte[] Data)
        {
            try
            {
                if (this.easyUSBHidNetClass1.DeviceUsbHID != null)          //Verify USB conection
                {
                    this.easyUSBHidNetClass1.DeviceUsbHID.writeData(0, Data);  //reportID and Data -> Send to Microcontroller the USB Writebuffer 
                                                                               
                }
                else
                {
                    MessageBox.Show("Was not possible to send data!");
                }
            }
            catch (Exception w) //error Exception
            {
                MessageBox.Show(w.ToString()); //show error message
            }

        }


       
             
    } //end Form class 
}//end namespace
