using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using CyUSB; namespace CyControl { public partial class Form1 : Form { USBDeviceList usbDevices; CyUSBEndPoint curEndpt; CyHidDevice curHidDev; CyHidReport curHidReport; App_PnP_Callback evHandler; string dataCaption; public Form1() { InitializeComponent(); evHandler = new App_PnP_Callback(PnP_Event_Handler); // This call instantiates usbDevices per the driver classes selected CyUSBDeviceBox_CheckedChanged(this, null); Form1_Resize(this, null); } public void PnP_Event_Handler(IntPtr pnpEvent, IntPtr hRemovedDevice) { if (pnpEvent.Equals(CyConst.DBT_DEVICEREMOVECOMPLETE)) { usbDevices.Remove(hRemovedDevice); RefreshDeviceTree(); } if (pnpEvent.Equals(CyConst.DBT_DEVICEARRIVAL)) { usbDevices.Add(); RefreshDeviceTree(); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (usbDevices != null) usbDevices.Dispose(); } private void RefreshDeviceTree() { DeviceTreeView.Nodes.Clear(); DescText.Text = ""; foreach (USBDevice dev in usbDevices) DeviceTreeView.Nodes.Add(dev.Tree); } private void DeviceTreeView_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode selNode = DeviceTreeView.SelectedNode; string nodeText = selNode.Text; CyUSBInterface curIntfc = selNode.Tag as CyUSBInterface; curEndpt = selNode.Tag as CyUSBEndPoint; curHidDev = null; curHidReport = null; if (curIntfc != null) { CyUSBDevice curDev = selNode.Parent.Parent.Tag as CyUSBDevice; curDev.AltIntfc = curIntfc.bAlternateSetting; } else if (curEndpt != null) { int minXfer = curEndpt.MaxPktSize; if (curEndpt.Attributes == 1) minXfer *= 8; NumBytesBox.Text = minXfer.ToString(); // Set the AltSetting if (curEndpt.Address != 0) // Only if we're not on the Control Endpoint { CyUSBDevice curDev = selNode.Parent.Parent.Parent.Tag as CyUSBDevice; curIntfc = selNode.Parent.Tag as CyUSBInterface; curDev.AltIntfc = curIntfc.bAlternateSetting; } } else if ((selNode.Tag is CyHidButton) || (selNode.Tag is CyHidValue)) { curHidDev = selNode.Parent.Parent.Tag as CyHidDevice; curHidReport = selNode.Parent.Tag as CyHidReport; NumBytesBox.Text = curHidReport.RptByteLen.ToString(); nodeText = selNode.Parent.Text; } else if (selNode.Tag is CyHidReport) { curHidDev = selNode.Parent.Tag as CyHidDevice; curHidReport = selNode.Tag as CyHidReport; NumBytesBox.Text = curHidReport.RptByteLen.ToString(); } else if (selNode.Tag is CyHidDevice) curHidDev = selNode.Tag as CyHidDevice; ConfigDataXferBtn(nodeText); DescText.Text = selNode.Tag.ToString(); Form1_Resize(sender, null); } private void ConfigDataXferBtn(string nodeTxt) { FileXferBtn.Visible = false; FileXferBtn.Enabled = false; if (nodeTxt.Contains("Feature")) { DataXferBtn.Text = "Get Feature"; FileXferBtn.Text = "Set Feature"; FileXferBtn.Visible = true; FileXferBtn.Enabled = true; } else if (nodeTxt.Contains("Input")) DataXferBtn.Text = "Get Input"; else if (nodeTxt.Contains("Output")) DataXferBtn.Text = "Set Output"; else DataXferBtn.Text = "Transfer Data"; if (curHidDev != null) { TreeNode selNode = DeviceTreeView.SelectedNode; DataXferBtn.Enabled = curHidDev.RwAccessible; } else DataXferBtn.Enabled = true; } private void AboutMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(Util.Assemblies, Text); } private void DoHidXfer(object sender, EventArgs e) { bool bResult = false; if (DataXferBtn.Text.Contains("Feature")) { if (sender == FileXferBtn) { dataCaption = "Set feature "; OutputBox.Text += dataCaption; LoadHidReport(); bResult = curHidDev.SetFeature(curHidReport.ID); } else { dataCaption = "Get feature "; OutputBox.Text += dataCaption; bResult = curHidDev.GetFeature(curHidReport.ID); } } else if (DataXferBtn.Text.Contains("Input")) { dataCaption = "Get input "; OutputBox.Text += dataCaption; bResult = curHidDev.GetInput(curHidReport.ID); } else if (DataXferBtn.Text.Contains("Output")) { dataCaption = "Set output "; OutputBox.Text += dataCaption; LoadHidReport(); bResult = curHidDev.SetOutput(curHidReport.ID); } if (bResult) DisplayXferData(curHidReport.DataBuf, curHidReport.RptByteLen); else { OutputBox.Text += string.Format("\r\n{0}failed\r\n\r\n",dataCaption); OutputBox.SelectionStart = OutputBox.Text.Length; OutputBox.ScrollToCaret(); } } private void LoadHidReport() { if (curHidReport == null) return; curHidReport.Clear(); // Load the report buffer with the hex bytes in XferDataBox string[] separators = { " " }; string[] hexVals = XferDataBox.Text.Split(separators, StringSplitOptions.RemoveEmptyEntries); int i = 1; foreach (string s in hexVals) if (i <= curHidReport.RptByteLen) curHidReport.DataBuf[i++] = (byte)Convert.ToInt32(s, 16); } private void FileXferBtn_Click(object sender, EventArgs e) { if (curHidReport != null) { DoHidXfer(sender, e); return; } } private void DataXferBtn_Click(object sender, EventArgs e) { if (curHidReport != null) { DoHidXfer(sender, e); return; } if (curHidDev != null) { MessageBox.Show("Select a HID feature, input or output in the device tree.", "No report selected"); return; } if (curEndpt == null) { MessageBox.Show("Select an endpoint in the device tree.", "No endpoint selected"); return; } int bytes = 0; try { bytes = Convert.ToInt32(NumBytesBox.Text); } catch (Exception exc) { if (bytes < 1) { MessageBox.Show("Enter a valid number of bytes to transfer.", "Invalid Byte Count"); return; } } byte[] buffer = new byte[bytes]; bool bXferCompleted = false; // Setting control endpt direction needs to occur before BuildDataCaption call CyControlEndPoint ctrlEpt = curEndpt as CyControlEndPoint; if (ctrlEpt != null) ctrlEpt.Direction = DirectionBox.Text.Equals("In") ? CyConst.DIR_FROM_DEVICE : CyConst.DIR_TO_DEVICE; // Stuff the output buffer if (!curEndpt.bIn) { int i = 0; foreach (char c in XferTextBox.Text) buffer[i++] = Convert.ToByte(c); } BuildDataCaption(); OutputBox.Text += dataCaption; OutputBox.SelectionStart = OutputBox.Text.Length; OutputBox.ScrollToCaret(); curEndpt.TimeOut = 2000; if (ctrlEpt != null) { if (TargetBox.Text.Equals("Device")) ctrlEpt.Target = CyConst.TGT_DEVICE; else if (TargetBox.Text.Equals("Interface")) ctrlEpt.Target = CyConst.TGT_INTFC; else if (TargetBox.Text.Equals("Endpoint")) ctrlEpt.Target = CyConst.TGT_ENDPT; else if (TargetBox.Text.Equals("Other")) ctrlEpt.Target = CyConst.TGT_OTHER; if (ReqTypeBox.Text.Equals("Standard")) ctrlEpt.ReqCode = CyConst.REQ_STD; else if (ReqTypeBox.Text.Equals("Class")) ctrlEpt.ReqCode = CyConst.REQ_CLASS; else if (ReqTypeBox.Text.Equals("Vendor")) ctrlEpt.ReqCode = CyConst.REQ_VENDOR; ctrlEpt.Direction = DirectionBox.Text.Equals("In") ? CyConst.DIR_FROM_DEVICE : CyConst.DIR_TO_DEVICE; ctrlEpt.ReqCode = (byte)Util.HexToInt(ReqCodeBox.Text); ctrlEpt.Value = (ushort)Util.HexToInt(wValueBox.Text); ctrlEpt.Index = (ushort)Util.HexToInt(wIndexBox.Text); bXferCompleted = ctrlEpt.XferData(ref buffer, ref bytes); } CyBulkEndPoint bulkEpt = curEndpt as CyBulkEndPoint; if (bulkEpt != null) bXferCompleted = bulkEpt.XferData(ref buffer, ref bytes); CyIsocEndPoint isocEpt = curEndpt as CyIsocEndPoint; if (isocEpt != null) bXferCompleted = isocEpt.XferData(ref buffer, ref bytes); DisplayXferData(buffer, bytes); } private void BuildDataCaption() { StringBuilder dataStr = new StringBuilder(); switch (curEndpt.Attributes) { case 0: dataStr.Append("CONTROL "); break; case 1: dataStr.Append("ISOC "); break; case 2: dataStr.Append("BULK "); break; case 3: dataStr.Append("INTERRUPT "); break; } if (curEndpt.bIn) dataStr.Append("IN transfer "); else dataStr.Append("OUT transfer "); dataCaption = dataStr.ToString(); } private void DisplayXferData(byte[] buf, int bCnt) { StringBuilder dataStr = new StringBuilder(); string resultStr = ""; if (bCnt > 0) resultStr = dataCaption + "completed\r\n"; else resultStr = dataCaption + "failed\r\n"; for (int i = 0; i < bCnt; i++) { if ((i % 16) == 0) dataStr.Append(string.Format("\r\n{0:X4}", i)); dataStr.Append(string.Format(" {0:X2}", buf[i])); } OutputBox.Text += dataStr.ToString() + "\r\n" + resultStr + "\r\n"; OutputBox.SelectionStart = OutputBox.Text.Length; OutputBox.ScrollToCaret(); } private void Form1_Resize(object sender, EventArgs e) { bool bControlEpt = ((curEndpt != null) && (curEndpt.Attributes == 0)); DirectionBox.Visible = bControlEpt; DirectionLabel.Visible = bControlEpt; ReqTypeBox.Visible = bControlEpt; ReqTypeLabel.Visible = bControlEpt; TargetBox.Visible = bControlEpt; TargetLabel.Visible = bControlEpt; ReqCodeBox.Visible = bControlEpt; ReqCodeLabel.Visible = bControlEpt; wValueBox.Visible = bControlEpt; wValueLabel.Visible = bControlEpt; wIndexBox.Visible = bControlEpt; wIndexLabel.Visible = bControlEpt; int oBoxAdj = bControlEpt ? 200 : 100; OutputBox.SetBounds(0, 0, 5, XferTab.Size.Height - oBoxAdj); } private void XferTextBox_KeyUp(object sender, KeyEventArgs e) { string txt = XferTextBox.Text; StringBuilder hexData = new StringBuilder(); foreach (char c in txt) { hexData.Append(string.Format("{0:X2} ", Convert.ToByte(c))); } XferDataBox.Text = hexData.ToString(); NumBytesBox.Text = XferTextBox.Text.Length.ToString(); HidLimitXferLen(); } private void XferDataBox_KeyUp(object sender, KeyEventArgs e) { string hexData = XferDataBox.Text; string[] hexTokens = hexData.Split(' '); StringBuilder txt = new StringBuilder(); foreach (string tok in hexTokens) { if (tok.Length > 0) { int n = (int)Util.HexToInt(tok); if (n > 0) txt.Append(Convert.ToChar(n)); else txt.Append(' '); } } XferTextBox.Text = txt.ToString(); NumBytesBox.Text = XferTextBox.Text.Length.ToString(); HidLimitXferLen(); } private void HidLimitXferLen() { TreeNode selNode = DeviceTreeView.SelectedNode; if (curHidDev != null) NumBytesBox.Text = curHidReport.RptByteLen.ToString(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } private CyFX2Device Fx2DeviceSelected() { TreeNode selNode = DeviceTreeView.SelectedNode; if (selNode == null) { MessageBox.Show("Select an FX2 device in the device tree.", "Non-FX2 device selected"); return null; } // Climb to the top of the tree while (selNode.Parent != null) selNode = selNode.Parent; CyFX2Device fx2 = selNode.Tag as CyFX2Device; if (fx2 == null) MessageBox.Show("Select an FX2 device in the device tree.", "Non-FX2 device selected"); return fx2; } private void ProgE2Item_Click(object sender, EventArgs e) { CyFX2Device fx2 = Fx2DeviceSelected(); string tmpFilter = FOpenDialog.Filter; if (sender == ProgE2Item) FOpenDialog.Filter = "Firmware Image files (*.iic) | *.iic"; if ((fx2 != null) && (FOpenDialog.ShowDialog() == DialogResult.OK)) { bool bResult = false; if (sender == ProgE2Item) { StatLabel.Text = "Programming EEPROM of " + fx2.FriendlyName; Refresh(); bResult = fx2.LoadEEPROM(FOpenDialog.FileName); } else { StatLabel.Text = "Programming RAM of " + fx2.FriendlyName; Refresh(); bResult = fx2.LoadRAM(FOpenDialog.FileName); } StatLabel.Text = "Programming " + (bResult ? "succeeded." : "failed."); Refresh(); } FOpenDialog.Filter = tmpFilter; } private void HaltItem_Click(object sender, EventArgs e) { CyFX2Device fx2 = Fx2DeviceSelected(); if (fx2 != null) if (sender == HaltItem) fx2.Reset(1); else fx2.Reset(0); } private void CyUSBDeviceBox_CheckedChanged(object sender, EventArgs e) { byte DeviceMask = 0; DeviceMask |= CyUSBDeviceBox.Checked ? CyConst.DEVICES_CYUSB : (byte)0; DeviceMask |= MSCDeviceBox.Checked ? CyConst.DEVICES_MSC : (byte)0; DeviceMask |= HIDDeviceBox.Checked ? CyConst.DEVICES_HID : (byte)0; if (usbDevices != null) usbDevices.Dispose(); usbDevices = new USBDeviceList(DeviceMask, evHandler); RefreshDeviceTree(); } } }