From 3820cbc13ebdcfdd40e9d60d49c46e10a6358421 Mon Sep 17 00:00:00 2001 From: Maff Date: Thu, 22 Aug 2019 01:45:15 +0100 Subject: [PATCH] idgaf how weird this code is right now --- libsharperang/usb.cs | 27 +++++++++++++++++++++++---- sharperang/MainWindow.xaml.cs | 2 ++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/libsharperang/usb.cs b/libsharperang/usb.cs index 4076ee3..a99aaa9 100644 --- a/libsharperang/usb.cs +++ b/libsharperang/usb.cs @@ -21,31 +21,50 @@ namespace libsharperang { // the printer from usbprint to WinUSB if (UsbDevice.AllWinUsbDevices.Count == 0) return false; //genuinely just having some fun with Linq, don't judge me. + //TODO: work out a way of distinguishing multiple devices with same GUID + // which happens when two of the same device are connected + // the only thing that differs seemingly is the Address pInstances = (from d in UsbDevice.AllWinUsbDevices where d.Vid == idVendor && d.Pid == idProduct select d) .ToList(); pIds = (from d in pInstances select d.DeviceInterfaceGuids[0]) - //.Distinct() + .Distinct() .ToList(); return (pIds.Count > 0); } + public List GetAddressesFromGuid(Guid deviceId) { + return (from d in UsbDevice.AllWinUsbDevices + where d.DeviceInterfaceGuids.Contains(deviceId) + select d.DeviceProperties.Where(k => k.Key=="Address").FirstOrDefault().Value).ToList().ConvertAll(v => (int)v); + } public bool OpenUSB() { if (UsbDevice.AllWinUsbDevices.Count == 0) return false; if (pIds.Count == 0) return false; return OpenUSB(pIds.FirstOrDefault()); } public bool OpenUSB(Guid deviceId) { - bool OpenResult = UsbDevice.OpenUsbDevice( - ref deviceId, out UsbDevice handle); + return OpenUSB(deviceId, GetAddressesFromGuid(deviceId).FirstOrDefault()); + } + public bool OpenUSB(Guid deviceId, int deviceIndex) { + //first thought is to say "forgive me lord for i have sinned" but i am absolutely not repentant for this + bool OpenResult = (from d in UsbDevice.AllWinUsbDevices + where d.DeviceInterfaceGuids.Contains(deviceId) && + d.DeviceProperties.Where(k=>k.Key=="Address" && (int)k.Value==deviceIndex).Count() > 0 + select d) + .FirstOrDefault() + .Open(out UsbDevice handle); printer=handle; return OpenResult; } public string FoundPrinterGuids() => pIds - .ConvertAll(p => p.ToString()) + .ConvertAll(p => p.ToString()) .Aggregate((a, b) => a+","+b); + public string FoundPrinterGuidAddrs(Guid deviceId) => GetAddressesFromGuid(deviceId) + .ConvertAll(a => a.ToString()) + .Aggregate((a, b) => a+","+b); public string FoundProdIds() => printer?.Info.ProductString; } } diff --git a/sharperang/MainWindow.xaml.cs b/sharperang/MainWindow.xaml.cs index 97484ac..cbd812d 100644 --- a/sharperang/MainWindow.xaml.cs +++ b/sharperang/MainWindow.xaml.cs @@ -25,6 +25,8 @@ namespace sharperang { printer = new libsharperang.USB(); logger.Debug("libusb init gave "+printer.InitUSB()); logger.Debug("found Guids are "+printer.FoundPrinterGuids()); + printer.pIds.ForEach(p => logger.Debug("found Addrs for guid "+p.ToString()+" are "+printer.FoundPrinterGuidAddrs(p))); + logger.Debug("open gave "+printer.OpenUSB(printer.pIds[0])); } } }