using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using USBComponent;
using LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;
using LibUsbDotNet.DeviceNotify;
using LibUsbDotNet.DeviceNotify.Linux;
using LibUsbDotNet.LibUsb;

namespace USBTest
{
    public partial class USBTest : Form
    {
        #region 跨线程调用和数据传递
        public delegate void setText(Control frm, string sTemp);
        void showText(Control obj, string text)
        {
            if (obj.InvokeRequired)
            {
                setText st = new setText(showText);
                this.Invoke(st, new object[] { obj, text });
            }
            else
            {
                obj.Text = text;
            }
        }
        #endregion

        #region USB参数设置
        int myPID = 0x0020;  //产品ID
        int myVID = 0x1500;  //供应商ID
        #endregion

        USB usbTest;
        public USBTest()
        {
            InitializeComponent();
            usbTest = new USB(myVID, myPID);
            usbTest.UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;
            usbTest.USBDataReceived += OnRxEndPointData;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            usbTest.sendData(sendData.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            recData.Text = usbTest.recData();
        }


        private void OnRxEndPointData(object sender, EndpointDataEventArgs e)
        {
            int recDataBuffCount = e.Count;
            byte[] recDataBuff = new byte[recDataBuffCount];
            for (int i = 0; i < recDataBuffCount; i++)
            {
                recDataBuff[i] = e.Buffer[i];
            }
            showText(recData, Encoding.Default.GetString(recDataBuff));
        }

        private void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
        {
            try
            {
                if (e.EventType == EventType.DeviceArrival)
                {
                    //看看目前新连接的USB设备是不是目标设备
                    if (e.Device.IdProduct == myPID && e.Device.IdVendor == myVID)
                    {
                        //发现目标设备并打开该设备
                        if (!usbTest.connectionStatus)
                        {
                            throw new Exception("USB设备打开失败");
                        }
                        throw new Exception("插入USB设备是目标设备");
                    }
                    else
                    {
                        throw new Exception("插入USB设备不是目标设备");
                    }
                }
                else if (e.EventType == EventType.DeviceRemoveComplete)
                {
                    //看看目前移除的USB设备是不是目标设备
                    if (e.Device.IdProduct == myPID && e.Device.IdVendor == myVID)
                    {
                        usbTest.CloseUSB();
                        throw new Exception("移除的USB设备是目标设备");
                    }
                    else
                    {
                        throw new Exception("移除的USB设备不是目标设备");
                    }
                }
            }
            catch(Exception ex)
            {
                labelUSB.Text = ex.Message;
            }
        }
    }
}