idgaf how weird this code is right now

This commit is contained in:
Maff 2019-08-22 01:45:15 +01:00
parent 3a554ebe4b
commit 3820cbc13e
2 changed files with 25 additions and 4 deletions

View File

@ -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<UsbRegistry>();
pIds = (from d in pInstances
select d.DeviceInterfaceGuids[0])
//.Distinct()
.Distinct()
.ToList<Guid>();
return (pIds.Count > 0);
}
public List<int> 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<string>(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;
}
}

View File

@ -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]));
}
}
}